This commit is contained in:
gedoor 2021-12-26 20:44:51 +08:00
parent 3dec62a628
commit a5fbce7963
2 changed files with 33 additions and 17 deletions

View File

@ -44,16 +44,20 @@ object LocalBook {
} }
fun getContent(book: Book, chapter: BookChapter): String? { fun getContent(book: Book, chapter: BookChapter): String? {
return when { return try {
book.isEpub() -> { when {
EpubFile.getContent(book, chapter) book.isEpub() -> {
} EpubFile.getContent(book, chapter)
book.isUmd() -> { }
UmdFile.getContent(book, chapter) book.isUmd() -> {
} UmdFile.getContent(book, chapter)
else -> { }
TextFile.getContent(book, chapter) else -> {
TextFile.getContent(book, chapter)
}
} }
} catch (e: Exception) {
e.localizedMessage
} }
} }

View File

@ -1,6 +1,8 @@
package io.legado.app.model.localBook package io.legado.app.model.localBook
import android.net.Uri import android.net.Uri
import android.system.Os
import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
@ -8,8 +10,7 @@ import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.utils.* import io.legado.app.utils.*
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.*
import java.io.RandomAccessFile
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.regex.Matcher import java.util.regex.Matcher
import java.util.regex.Pattern import java.util.regex.Pattern
@ -253,22 +254,33 @@ class TextFile(private val book: Book) {
return TextFile(book).getChapterList() return TextFile(book).getChapterList()
} }
@Throws(FileNotFoundException::class)
fun getContent(book: Book, bookChapter: BookChapter): String { fun getContent(book: Book, bookChapter: BookChapter): String {
val bookFile = getBookFile(book) val count = (bookChapter.end!! - bookChapter.start!!).toInt()
//获取文件流 val content = ByteArray(count)
val bookStream = RandomAccessFile(bookFile, "r") Os.read(getBookFD(book), content, bookChapter.start!!.toInt(), count)
val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt())
bookStream.seek(bookChapter.start!!)
bookStream.read(content)
return String(content, book.fileCharset()) return String(content, book.fileCharset())
.substringAfter(bookChapter.title) .substringAfter(bookChapter.title)
.replace("^[\\n\\s]+".toRegex(), "  ") .replace("^[\\n\\s]+".toRegex(), "  ")
} }
@Throws(FileNotFoundException::class)
private fun getBookFD(book: Book): FileDescriptor? {
if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
return appCtx.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor
}
return FileInputStream(File(book.bookUrl)).fd
}
private fun getBookFile(book: Book): File { private fun getBookFile(book: Book): File {
if (book.bookUrl.isContentScheme()) { if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl) val uri = Uri.parse(book.bookUrl)
val bookFile = LocalBook.cacheFolder.getFile(book.originName) val bookFile = LocalBook.cacheFolder.getFile(book.originName)
val doc = DocumentFile.fromSingleUri(appCtx, uri)!!
if (bookFile.exists() && bookFile.lastModified() >= doc.lastModified()) {
return bookFile
}
if (!bookFile.exists()) { if (!bookFile.exists()) {
bookFile.createNewFile() bookFile.createNewFile()
DocumentUtils.readBytes(appCtx, uri).let { DocumentUtils.readBytes(appCtx, uri).let {