pull/1486/head
gedoor 3 years ago
parent 3dec62a628
commit a5fbce7963
  1. 22
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 28
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt

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

@ -1,6 +1,8 @@
package io.legado.app.model.localBook
import android.net.Uri
import android.system.Os
import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
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.utils.*
import splitties.init.appCtx
import java.io.File
import java.io.RandomAccessFile
import java.io.*
import java.nio.charset.Charset
import java.util.regex.Matcher
import java.util.regex.Pattern
@ -253,22 +254,33 @@ class TextFile(private val book: Book) {
return TextFile(book).getChapterList()
}
@Throws(FileNotFoundException::class)
fun getContent(book: Book, bookChapter: BookChapter): String {
val bookFile = getBookFile(book)
//获取文件流
val bookStream = RandomAccessFile(bookFile, "r")
val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt())
bookStream.seek(bookChapter.start!!)
bookStream.read(content)
val count = (bookChapter.end!! - bookChapter.start!!).toInt()
val content = ByteArray(count)
Os.read(getBookFD(book), content, bookChapter.start!!.toInt(), count)
return String(content, book.fileCharset())
.substringAfter(bookChapter.title)
.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 {
if (book.bookUrl.isContentScheme()) {
val uri = Uri.parse(book.bookUrl)
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()) {
bookFile.createNewFile()
DocumentUtils.readBytes(appCtx, uri).let {

Loading…
Cancel
Save