pull/1630/head
kunfei 3 years ago
parent 63e302ddf9
commit 5f61d2e178
  1. 22
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@ -90,42 +90,28 @@ class TextFile(private val book: Book) {
/** /**
* 按规则解析目录 * 按规则解析目录
*/ */
private fun analyze( private fun analyze(pattern: Pattern?): ArrayList<BookChapter> {
pattern: Pattern?, pattern ?: return analyze()
fileStart: Long = 0L,
fileEnd: Long = Long.MAX_VALUE
): ArrayList<BookChapter> {
pattern ?: return analyze(fileStart, fileEnd)
val toc = arrayListOf<BookChapter>() val toc = arrayListOf<BookChapter>()
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
var blockContent: String var blockContent: String
//加载章节 //加载章节
var curOffset: Long = 0 var curOffset: Long = 0
//读取的长度 //读取的长度
var length = 0 var length: Int
val buffer = ByteArray(bufferSize) val buffer = ByteArray(bufferSize)
var bufferStart = 3 var bufferStart = 3
if (fileStart == 0L) {
bis.read(buffer, 0, 3) bis.read(buffer, 0, 3)
if (Utf8BomUtils.hasBom(buffer)) { if (Utf8BomUtils.hasBom(buffer)) {
bufferStart = 0 bufferStart = 0
curOffset = 3 curOffset = 3
} }
} else {
bis.skip(fileStart)
curOffset = fileStart
bufferStart = 0
}
//获取文件中的数据到buffer,直到没有数据为止 //获取文件中的数据到buffer,直到没有数据为止
while ( while (
fileEnd - curOffset - bufferStart > 0 &&
bis.read( bis.read(
buffer, buffer,
bufferStart, bufferStart,
min( bufferSize - bufferStart
(bufferSize - bufferStart).toLong(),
fileEnd - curOffset - bufferStart
).toInt()
).also { length = it } > 0 ).also { length = it } > 0
) { ) {
var end = bufferStart + length var end = bufferStart + length

Loading…
Cancel
Save