pull/1514/head
gedoor 3 years ago
parent 7d1947e975
commit 9788e13607
  1. 27
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@ -215,6 +215,7 @@ class TextFile(private val book: Book) {
var blockPos = 0 var blockPos = 0
//加载章节 //加载章节
var curOffset: Long = 0 var curOffset: Long = 0
var chapterPos = 0
//读取的长度 //读取的长度
var length: Int var length: Int
val buffer = ByteArray(bufferSize) val buffer = ByteArray(bufferSize)
@ -232,14 +233,15 @@ class TextFile(private val book: Book) {
//章节在buffer的偏移量 //章节在buffer的偏移量
var chapterOffset = 0 var chapterOffset = 0
//当前剩余可分配的长度 //当前剩余可分配的长度
var strLength = bufferStart + length length += bufferStart
var strLength = length
//分章的位置 //分章的位置
var chapterPos = 0 chapterPos = 0
while (strLength > 0) { while (strLength > 0) {
++chapterPos ++chapterPos
//是否长度超过一章 //是否长度超过一章
if (strLength > maxLengthWithNoToc) { //在buffer中一章的终止点 if (strLength > maxLengthWithNoToc) { //在buffer中一章的终止点
var end = bufferStart + length var end = length
//寻找换行符作为终止点 //寻找换行符作为终止点
for (i in chapterOffset + maxLengthWithNoToc until length) { for (i in chapterOffset + maxLengthWithNoToc until length) {
if (buffer[i] == blank) { if (buffer[i] == blank) {
@ -257,22 +259,21 @@ class TextFile(private val book: Book) {
//设置偏移的位置 //设置偏移的位置
chapterOffset = end chapterOffset = end
} else { } else {
buffer.copyInto( buffer.copyInto(buffer, 0, length - strLength, length)
buffer, length -= strLength
0,
bufferStart + length - strLength,
bufferStart + length
)
length = bufferStart + length - strLength
bufferStart = strLength bufferStart = strLength
break strLength = 0
} }
} }
//block的偏移点 //block的偏移点
curOffset += length.toLong() curOffset += length.toLong()
//设置上一章的结尾
toc.lastOrNull()?.end = curOffset
} }
//设置结尾章节
val chapter = BookChapter()
chapter.title = "${blockPos}章(${chapterPos + 1})"
chapter.start = toc.lastOrNull()?.end ?: curOffset
chapter.end = chapter.start!! + bufferStart
toc.add(chapter)
} }
if (toc.isEmpty()) { if (toc.isEmpty()) {
return analyze() return analyze()

Loading…
Cancel
Save