|
|
@ -60,8 +60,10 @@ class AnalyzeTxtFile { |
|
|
|
var allLength = 0 |
|
|
|
var allLength = 0 |
|
|
|
|
|
|
|
|
|
|
|
//获取文件中的数据到buffer,直到没有数据为止 |
|
|
|
//获取文件中的数据到buffer,直到没有数据为止 |
|
|
|
while (bookStream.read(buffer, 0, buffer.size).also { length = it } > 0) { |
|
|
|
while (bookStream.read(buffer).also { length = it } > 0) { |
|
|
|
blockPos++ |
|
|
|
blockPos++ |
|
|
|
|
|
|
|
//如果存在Chapter |
|
|
|
|
|
|
|
if (rulePattern != null) { //将数据转换成String |
|
|
|
var blockContent = String(buffer, charset) |
|
|
|
var blockContent = String(buffer, charset) |
|
|
|
val lastN = blockContent.lastIndexOf("\n") |
|
|
|
val lastN = blockContent.lastIndexOf("\n") |
|
|
|
if (lastN > 0) { |
|
|
|
if (lastN > 0) { |
|
|
@ -70,8 +72,6 @@ class AnalyzeTxtFile { |
|
|
|
allLength += length |
|
|
|
allLength += length |
|
|
|
bookStream.seek(allLength.toLong()) |
|
|
|
bookStream.seek(allLength.toLong()) |
|
|
|
} |
|
|
|
} |
|
|
|
//如果存在Chapter |
|
|
|
|
|
|
|
if (rulePattern != null) { //将数据转换成String |
|
|
|
|
|
|
|
//当前Block下使过的String的指针 |
|
|
|
//当前Block下使过的String的指针 |
|
|
|
var seekPos = 0 |
|
|
|
var seekPos = 0 |
|
|
|
//进行正则匹配 |
|
|
|
//进行正则匹配 |
|
|
@ -81,47 +81,62 @@ class AnalyzeTxtFile { |
|
|
|
val chapterStart = matcher.start() |
|
|
|
val chapterStart = matcher.start() |
|
|
|
//获取章节内容 |
|
|
|
//获取章节内容 |
|
|
|
val chapterContent = blockContent.substring(seekPos, chapterStart) |
|
|
|
val chapterContent = blockContent.substring(seekPos, chapterStart) |
|
|
|
if (chapterContent.length > 30000 && pattern == null) { |
|
|
|
val chapterLength = chapterContent.toByteArray(charset).size |
|
|
|
|
|
|
|
if (chapterLength > 30000 && pattern == null) { |
|
|
|
//移除不匹配的规则 |
|
|
|
//移除不匹配的规则 |
|
|
|
tocRules.remove(tocRule) |
|
|
|
tocRules.remove(tocRule) |
|
|
|
return analyze(bookStream, book, null) |
|
|
|
return analyze(bookStream, book, null) |
|
|
|
} |
|
|
|
} |
|
|
|
//如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 |
|
|
|
//如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 |
|
|
|
//第一种情况一定是序章 第二种情况可能是上一个章节的内容 |
|
|
|
//第一种情况一定是序章 第二种情况是上一个章节的内容 |
|
|
|
if (seekPos == 0 && chapterStart != 0) { //获取当前章节的内容 |
|
|
|
if (seekPos == 0 && chapterStart != 0) { //获取当前章节的内容 |
|
|
|
val contentSize = chapterContent.toByteArray(charset).size.toLong() |
|
|
|
if (toc.isEmpty()) { //如果当前没有章节,那么就是序章 |
|
|
|
if (toc.isEmpty()) { |
|
|
|
//加入简介 |
|
|
|
if (StringUtils.trim(chapterContent).isNotEmpty()) { |
|
|
|
if (StringUtils.trim(chapterContent).isNotEmpty()) { |
|
|
|
val chapter = BookChapter() |
|
|
|
val qyChapter = BookChapter() |
|
|
|
chapter.title = "前言" |
|
|
|
qyChapter.title = "前言" |
|
|
|
chapter.start = 0 |
|
|
|
qyChapter.start = 0 |
|
|
|
chapter.end = contentSize |
|
|
|
qyChapter.end = chapterLength.toLong() |
|
|
|
toc.add(chapter) |
|
|
|
toc.add(qyChapter) |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
toc.last().let { |
|
|
|
|
|
|
|
it.end = it.end!! + contentSize |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
//创建当前章节 |
|
|
|
//创建当前章节 |
|
|
|
val curChapter = BookChapter() |
|
|
|
val curChapter = BookChapter() |
|
|
|
curChapter.title = matcher.group() |
|
|
|
curChapter.title = matcher.group() |
|
|
|
curChapter.start = (toc.lastOrNull()?.end ?: 0) + contentSize |
|
|
|
curChapter.start = chapterLength.toLong() |
|
|
|
|
|
|
|
toc.add(curChapter) |
|
|
|
|
|
|
|
} else { //否则就block分割之后,上一个章节的剩余内容 |
|
|
|
|
|
|
|
//获取上一章节 |
|
|
|
|
|
|
|
val lastChapter = toc.last() |
|
|
|
|
|
|
|
//将当前段落添加上一章去 |
|
|
|
|
|
|
|
lastChapter.end = |
|
|
|
|
|
|
|
lastChapter.end!! + chapterLength.toLong() |
|
|
|
|
|
|
|
//创建当前章节 |
|
|
|
|
|
|
|
val curChapter = BookChapter() |
|
|
|
|
|
|
|
curChapter.title = matcher.group() |
|
|
|
|
|
|
|
curChapter.start = lastChapter.end |
|
|
|
toc.add(curChapter) |
|
|
|
toc.add(curChapter) |
|
|
|
} else { |
|
|
|
|
|
|
|
val lastChapter = toc.lastOrNull() |
|
|
|
|
|
|
|
lastChapter?.let { |
|
|
|
|
|
|
|
//上一章节结束等于这一章节开始 |
|
|
|
|
|
|
|
it.end = it.start!! + chapterContent.toByteArray(charset).size |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (toc.isNotEmpty()) { //获取章节内容 |
|
|
|
|
|
|
|
//获取上一章节 |
|
|
|
|
|
|
|
val lastChapter = toc.last() |
|
|
|
|
|
|
|
lastChapter.end = |
|
|
|
|
|
|
|
lastChapter.start!! + chapterContent.toByteArray(charset).size.toLong() |
|
|
|
//创建当前章节 |
|
|
|
//创建当前章节 |
|
|
|
val curChapter = BookChapter() |
|
|
|
val curChapter = BookChapter() |
|
|
|
curChapter.title = matcher.group() |
|
|
|
curChapter.title = matcher.group() |
|
|
|
curChapter.start = lastChapter?.end ?: 0 |
|
|
|
curChapter.start = lastChapter.end |
|
|
|
toc.add(curChapter) |
|
|
|
toc.add(curChapter) |
|
|
|
|
|
|
|
} else { //如果章节不存在则创建章节 |
|
|
|
|
|
|
|
val curChapter = BookChapter() |
|
|
|
|
|
|
|
curChapter.title = matcher.group() |
|
|
|
|
|
|
|
curChapter.start = 0 |
|
|
|
|
|
|
|
curChapter.end = 0 |
|
|
|
|
|
|
|
toc.add(curChapter) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//设置指针偏移 |
|
|
|
//设置指针偏移 |
|
|
|
seekPos += chapterContent.length |
|
|
|
seekPos += chapterLength |
|
|
|
} |
|
|
|
} |
|
|
|
} else { //进行本地虚拟分章 |
|
|
|
} else { //进行本地虚拟分章 |
|
|
|
//章节在buffer的偏移量 |
|
|
|
//章节在buffer的偏移量 |
|
|
@ -165,7 +180,8 @@ class AnalyzeTxtFile { |
|
|
|
//block的偏移点 |
|
|
|
//block的偏移点 |
|
|
|
curOffset += length.toLong() |
|
|
|
curOffset += length.toLong() |
|
|
|
|
|
|
|
|
|
|
|
if (rulePattern != null) { //设置上一章的结尾 |
|
|
|
if (rulePattern != null) { |
|
|
|
|
|
|
|
//设置上一章的结尾 |
|
|
|
val lastChapter = toc.last() |
|
|
|
val lastChapter = toc.last() |
|
|
|
lastChapter.end = curOffset |
|
|
|
lastChapter.end = curOffset |
|
|
|
} |
|
|
|
} |
|
|
@ -231,10 +247,9 @@ class AnalyzeTxtFile { |
|
|
|
val bookFile = getBookFile(App.INSTANCE, book) |
|
|
|
val bookFile = getBookFile(App.INSTANCE, book) |
|
|
|
//获取文件流 |
|
|
|
//获取文件流 |
|
|
|
val bookStream = RandomAccessFile(bookFile, "r") |
|
|
|
val bookStream = RandomAccessFile(bookFile, "r") |
|
|
|
bookStream.seek(bookChapter.start ?: 0) |
|
|
|
val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt()) |
|
|
|
val extent = (bookChapter.end!! - bookChapter.start!!).toInt() |
|
|
|
bookStream.seek(bookChapter.start!!) |
|
|
|
val content = ByteArray(extent) |
|
|
|
bookStream.read(content) |
|
|
|
bookStream.read(content, 0, extent) |
|
|
|
|
|
|
|
return String(content, book.fileCharset()) |
|
|
|
return String(content, book.fileCharset()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|