This commit is contained in:
kunfei 2023-04-17 10:40:12 +08:00
parent 8574046f35
commit a4fc1b10f5

View File

@ -34,11 +34,7 @@ class TextFile(private val book: Book) {
@Synchronized @Synchronized
@Throws(FileNotFoundException::class) @Throws(FileNotFoundException::class)
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
if (txtBuffer == null if (txtBuffer == null || bookUrl != book.bookUrl || bookChapter.start!! > bufferEnd || bookChapter.end!! < bufferStart) {
|| bookUrl != book.bookUrl
|| bookChapter.start!! > bufferEnd
|| bookChapter.end!! < bufferStart
) {
bookUrl = book.bookUrl bookUrl = book.bookUrl
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
bufferStart = bufferSize * (bookChapter.start!! / bufferSize).toInt() bufferStart = bufferSize * (bookChapter.start!! / bufferSize).toInt()
@ -52,9 +48,7 @@ class TextFile(private val book: Book) {
val count = (bookChapter.end!! - bookChapter.start!!).toInt() val count = (bookChapter.end!! - bookChapter.start!!).toInt()
val buffer = ByteArray(count) val buffer = ByteArray(count)
if (bookChapter.start!! < bufferEnd && bookChapter.end!! > bufferEnd if (bookChapter.start!! < bufferEnd && bookChapter.end!! > bufferEnd || bookChapter.start!! < bufferStart && bookChapter.end!! > bufferStart) {
|| bookChapter.start!! < bufferStart && bookChapter.end!! > bufferStart
) {
/** 章节内容在缓冲区交界处 */ /** 章节内容在缓冲区交界处 */
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
bis.skip(bookChapter.start!!) bis.skip(bookChapter.start!!)
@ -70,8 +64,7 @@ class TextFile(private val book: Book) {
) )
} }
return String(buffer, book.fileCharset()) return String(buffer, book.fileCharset()).substringAfter(bookChapter.title)
.substringAfter(bookChapter.title)
.replace(padRegex, "  ") .replace(padRegex, "  ")
} }
@ -142,11 +135,8 @@ class TextFile(private val book: Book) {
curOffset = 3 curOffset = 3
} }
//获取文件中的数据到buffer直到没有数据为止 //获取文件中的数据到buffer直到没有数据为止
while ( while (bis.read(
bis.read( buffer, bufferStart, bufferSize - bufferStart
buffer,
bufferStart,
bufferSize - bufferStart
).also { length = it } > 0 ).also { length = it } > 0
) { ) {
var end = bufferStart + length var end = bufferStart + length
@ -174,9 +164,7 @@ class TextFile(private val book: Book) {
val chapterContent = blockContent.substring(seekPos, chapterStart) val chapterContent = blockContent.substring(seekPos, chapterStart)
val chapterLength = chapterContent.toByteArray(charset).size val chapterLength = chapterContent.toByteArray(charset).size
val lastStart = toc.lastOrNull()?.start ?: curOffset val lastStart = toc.lastOrNull()?.start ?: curOffset
if (book.getSplitLongChapter() if (book.getSplitLongChapter() && curOffset + chapterLength - lastStart > maxLengthWithToc) {
&& curOffset + chapterLength - lastStart > maxLengthWithToc
) {
toc.lastOrNull()?.let { toc.lastOrNull()?.let {
it.end = it.start it.end = it.start
} }
@ -184,8 +172,7 @@ class TextFile(private val book: Book) {
val lastTitle = toc.lastOrNull()?.title val lastTitle = toc.lastOrNull()?.title
val lastTitleLength = lastTitle?.toByteArray(charset)?.size ?: 0 val lastTitleLength = lastTitle?.toByteArray(charset)?.size ?: 0
val chapters = analyze( val chapters = analyze(
lastStart + lastTitleLength, lastStart + lastTitleLength, curOffset + chapterLength
curOffset + chapterLength
) )
lastTitle?.let { lastTitle?.let {
chapters.forEachIndexed { index, bookChapter -> chapters.forEachIndexed { index, bookChapter ->
@ -223,8 +210,7 @@ class TextFile(private val book: Book) {
lastChapter.isVolume = lastChapter.isVolume =
chapterContent.substringAfter(lastChapter.title).isBlank() chapterContent.substringAfter(lastChapter.title).isBlank()
//将当前段落添加上一章去 //将当前段落添加上一章去
lastChapter.end = lastChapter.end = lastChapter.end!! + chapterLength.toLong()
lastChapter.end!! + chapterLength.toLong()
//创建当前章节 //创建当前章节
val curChapter = BookChapter() val curChapter = BookChapter()
curChapter.title = matcher.group() curChapter.title = matcher.group()
@ -263,14 +249,13 @@ class TextFile(private val book: Book) {
} }
toc.lastOrNull()?.let { chapter -> toc.lastOrNull()?.let { chapter ->
//章节字数太多进行拆分 //章节字数太多进行拆分
if (chapter.end!! - chapter.start!! > maxLengthWithToc) { if (book.getSplitLongChapter() && chapter.end!! - chapter.start!! > maxLengthWithToc) {
val end = chapter.end!! val end = chapter.end!!
chapter.end = chapter.start chapter.end = chapter.start
val lastTitle = chapter.title val lastTitle = chapter.title
val lastTitleLength = lastTitle.toByteArray(charset).size val lastTitleLength = lastTitle.toByteArray(charset).size
val chapters = analyze( val chapters = analyze(
chapter.start!! + lastTitleLength, chapter.start!! + lastTitleLength, end
end
) )
chapters.forEachIndexed { index, bookChapter -> chapters.forEachIndexed { index, bookChapter ->
bookChapter.title = "$lastTitle(${index + 1})" bookChapter.title = "$lastTitle(${index + 1})"
@ -288,8 +273,7 @@ class TextFile(private val book: Book) {
* 无规则拆分目录 * 无规则拆分目录
*/ */
private fun analyze( private fun analyze(
fileStart: Long = 0L, fileStart: Long = 0L, fileEnd: Long = Long.MAX_VALUE
fileEnd: Long = Long.MAX_VALUE
): ArrayList<BookChapter> { ): ArrayList<BookChapter> {
val toc = arrayListOf<BookChapter>() val toc = arrayListOf<BookChapter>()
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
@ -314,14 +298,9 @@ class TextFile(private val book: Book) {
bufferStart = 0 bufferStart = 0
} }
//获取文件中的数据到buffer直到没有数据为止 //获取文件中的数据到buffer直到没有数据为止
while ( while (fileEnd - curOffset - bufferStart > 0 && bis.read(
fileEnd - curOffset - bufferStart > 0 && buffer, bufferStart, min(
bis.read( (bufferSize - bufferStart).toLong(), fileEnd - curOffset - bufferStart
buffer,
bufferStart,
min(
(bufferSize - bufferStart).toLong(),
fileEnd - curOffset - bufferStart
).toInt() ).toInt()
).also { length = it } > 0 ).also { length = it } > 0
) { ) {