|
|
|
@ -51,10 +51,12 @@ class TextFile(private val book: Book) { |
|
|
|
|
} else { |
|
|
|
|
book.tocUrl.toPattern(Pattern.MULTILINE) |
|
|
|
|
} |
|
|
|
|
return analyze(rulePattern) |
|
|
|
|
return rulePattern?.let { |
|
|
|
|
analyze(rulePattern) |
|
|
|
|
} ?: analyze() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun analyze(pattern: Pattern?): ArrayList<BookChapter> { |
|
|
|
|
private fun analyze(pattern: Pattern): ArrayList<BookChapter> { |
|
|
|
|
val toc = arrayListOf<BookChapter>() |
|
|
|
|
LocalBook.getBookInputStream(book).use { bis -> |
|
|
|
|
var blockContent: String |
|
|
|
@ -76,8 +78,6 @@ class TextFile(private val book: Book) { |
|
|
|
|
.also { length = it } > 0 |
|
|
|
|
) { |
|
|
|
|
blockPos++ |
|
|
|
|
//如果存在Chapter |
|
|
|
|
if (pattern != null) { |
|
|
|
|
var end = bufferStart + length |
|
|
|
|
for (i in bufferStart + length - 1 downTo 0) { |
|
|
|
|
if (buffer[i] == blank) { |
|
|
|
@ -105,7 +105,9 @@ class TextFile(private val book: Book) { |
|
|
|
|
bis.close() |
|
|
|
|
//移除不匹配的规则 |
|
|
|
|
tocRules.removeFirstOrNull() |
|
|
|
|
return analyze(tocRules.firstOrNull()?.rule?.toPattern(Pattern.MULTILINE)) |
|
|
|
|
return tocRules.firstOrNull()?.let { |
|
|
|
|
analyze(it.rule.toPattern(Pattern.MULTILINE)) |
|
|
|
|
} ?: analyze() |
|
|
|
|
} |
|
|
|
|
//如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 |
|
|
|
|
//第一种情况一定是序章 第二种情况是上一个章节的内容 |
|
|
|
@ -162,10 +164,63 @@ class TextFile(private val book: Book) { |
|
|
|
|
bis.close() |
|
|
|
|
//移除不匹配的规则 |
|
|
|
|
tocRules.remove(tocRules.removeFirstOrNull()) |
|
|
|
|
return analyze(tocRules.firstOrNull()?.rule?.toPattern(Pattern.MULTILINE)) |
|
|
|
|
return tocRules.firstOrNull()?.let { |
|
|
|
|
analyze(it.rule.toPattern(Pattern.MULTILINE)) |
|
|
|
|
} ?: analyze() |
|
|
|
|
} |
|
|
|
|
} else { //进行本地虚拟分章 |
|
|
|
|
|
|
|
|
|
//block的偏移点 |
|
|
|
|
curOffset += length.toLong() |
|
|
|
|
|
|
|
|
|
//设置上一章的结尾 |
|
|
|
|
val lastChapter = toc.last() |
|
|
|
|
lastChapter.end = curOffset |
|
|
|
|
|
|
|
|
|
//当添加的block太多的时候,执行GC |
|
|
|
|
if (blockPos % 15 == 0) { |
|
|
|
|
System.gc() |
|
|
|
|
System.runFinalization() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (i in toc.indices) { |
|
|
|
|
val bean = toc[i] |
|
|
|
|
bean.index = i |
|
|
|
|
bean.bookUrl = book.bookUrl |
|
|
|
|
bean.url = (MD5Utils.md5Encode16(book.originName + i + bean.title)) |
|
|
|
|
} |
|
|
|
|
book.latestChapterTitle = toc.last().title |
|
|
|
|
book.totalChapterNum = toc.size |
|
|
|
|
|
|
|
|
|
System.gc() |
|
|
|
|
System.runFinalization() |
|
|
|
|
book.tocUrl = pattern.pattern() |
|
|
|
|
book.save() |
|
|
|
|
return toc |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun analyze(): ArrayList<BookChapter> { |
|
|
|
|
val toc = arrayListOf<BookChapter>() |
|
|
|
|
LocalBook.getBookInputStream(book).use { bis -> |
|
|
|
|
//加载章节 |
|
|
|
|
var curOffset: Long = 0 |
|
|
|
|
//block的个数 |
|
|
|
|
var blockPos = 0 |
|
|
|
|
//读取的长度 |
|
|
|
|
var length: Int |
|
|
|
|
val buffer = ByteArray(bufferSize) |
|
|
|
|
var bufferStart = 3 |
|
|
|
|
bis.read(buffer, 0, 3) |
|
|
|
|
if (Utf8BomUtils.hasBom(buffer)) { |
|
|
|
|
bufferStart = 0 |
|
|
|
|
curOffset = 3 |
|
|
|
|
} |
|
|
|
|
//获取文件中的数据到buffer,直到没有数据为止 |
|
|
|
|
while (bis.read(buffer, bufferStart, bufferSize - bufferStart) |
|
|
|
|
.also { length = it } > 0 |
|
|
|
|
) { |
|
|
|
|
bufferStart = 0 |
|
|
|
|
blockPos++ |
|
|
|
|
//章节在buffer的偏移量 |
|
|
|
|
var chapterOffset = 0 |
|
|
|
|
//当前剩余可分配的长度 |
|
|
|
@ -202,17 +257,10 @@ class TextFile(private val book: Book) { |
|
|
|
|
strLength = 0 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//block的偏移点 |
|
|
|
|
curOffset += length.toLong() |
|
|
|
|
|
|
|
|
|
if (pattern != null) { |
|
|
|
|
//设置上一章的结尾 |
|
|
|
|
val lastChapter = toc.last() |
|
|
|
|
lastChapter.end = curOffset |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//当添加的block太多的时候,执行GC |
|
|
|
|
if (blockPos % 15 == 0) { |
|
|
|
|
System.gc() |
|
|
|
@ -231,7 +279,7 @@ class TextFile(private val book: Book) { |
|
|
|
|
|
|
|
|
|
System.gc() |
|
|
|
|
System.runFinalization() |
|
|
|
|
book.tocUrl = pattern?.pattern() ?: "" |
|
|
|
|
book.tocUrl = "" |
|
|
|
|
book.save() |
|
|
|
|
return toc |
|
|
|
|
} |
|
|
|
|