pull/1486/head 3.21.122920
gedoor 4 years ago
parent 5a8dbd673e
commit c33eba9423
  1. 5
      app/src/main/java/io/legado/app/model/localBook/TextFile.kt
  2. 8
      app/src/main/java/io/legado/app/ui/association/FileAssociationActivity.kt
  3. 66
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -22,7 +22,7 @@ class TextFile(private val book: Book) {
private val tocRules = arrayListOf<TxtTocRule>() private val tocRules = arrayListOf<TxtTocRule>()
private lateinit var charset: Charset private lateinit var charset: Charset
@Throws(Exception::class) @Throws(FileNotFoundException::class)
fun getChapterList(): ArrayList<BookChapter> { fun getChapterList(): ArrayList<BookChapter> {
val bookFile = getBookFile(book) val bookFile = getBookFile(book)
if (book.charset == null) { if (book.charset == null) {
@ -40,7 +40,7 @@ class TextFile(private val book: Book) {
} }
} }
@Throws(Exception::class) @Throws(FileNotFoundException::class)
private fun analyze( private fun analyze(
bookIs: InputStream, bookIs: InputStream,
book: Book, book: Book,
@ -249,6 +249,7 @@ class TextFile(private val book: Book) {
//没有标题的时候,每个章节的最大长度 //没有标题的时候,每个章节的最大长度
private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024 private const val MAX_LENGTH_WITH_NO_CHAPTER = 10 * 1024
@Throws(FileNotFoundException::class)
fun getChapterList(book: Book): ArrayList<BookChapter> { fun getChapterList(book: Book): ArrayList<BookChapter> {
return TextFile(book).getChapterList() return TextFile(book).getChapterList()
} }

@ -104,13 +104,7 @@ class FileAssociationActivity :
val nDoc = treeDoc.createFile(FileUtils.getMimeType(name), name)!! val nDoc = treeDoc.createFile(FileUtils.getMimeType(name), name)!!
contentResolver.openOutputStream(nDoc.uri)!!.use { oStream -> contentResolver.openOutputStream(nDoc.uri)!!.use { oStream ->
contentResolver.openInputStream(bookDoc.uri)!!.use { iStream -> contentResolver.openInputStream(bookDoc.uri)!!.use { iStream ->
val brr = ByteArray(1024) iStream.copyTo(oStream)
var len: Int
while ((iStream.read(brr, 0, brr.size)
.also { len = it }) != -1
) {
oStream.write(brr, 0, len)
}
oStream.flush() oStream.flush()
} }
} }

@ -295,7 +295,7 @@ object FileUtils {
//返回当前目录所有以某些扩展名结尾的文件 //返回当前目录所有以某些扩展名结尾的文件
val extension = getExtension(name) val extension = getExtension(name)
allowExtensions?.contentDeepToString()?.contains(extension) == true allowExtensions?.contentDeepToString()?.contains(extension) == true
|| allowExtensions == null || allowExtensions == null
} }
} }
@ -382,21 +382,14 @@ object FileUtils {
fun copy(src: File, tar: File): Boolean { fun copy(src: File, tar: File): Boolean {
try { try {
if (src.isFile) { if (src.isFile) {
val `is` = FileInputStream(src) val inputStream = FileInputStream(src)
val op = FileOutputStream(tar) val outputStream = FileOutputStream(tar)
val bis = BufferedInputStream(`is`) inputStream.use {
val bos = BufferedOutputStream(op) outputStream.use {
val bt = ByteArray(1024 * 8) inputStream.copyTo(outputStream)
while (true) { outputStream.flush()
val len = bis.read(bt)
if (len == -1) {
break
} else {
bos.write(bt, 0, len)
} }
} }
bis.close()
bos.close()
} else if (src.isDirectory) { } else if (src.isDirectory) {
tar.mkdirs() tar.mkdirs()
src.listFiles()?.forEach { file -> src.listFiles()?.forEach { file ->
@ -461,18 +454,18 @@ object FileUtils {
var fis: FileInputStream? = null var fis: FileInputStream? = null
try { try {
fis = FileInputStream(filepath) fis = FileInputStream(filepath)
val baos = ByteArrayOutputStream() val outputStream = ByteArrayOutputStream()
val buffer = ByteArray(1024) val buffer = ByteArray(1024)
while (true) { while (true) {
val len = fis.read(buffer, 0, buffer.size) val len = fis.read(buffer, 0, buffer.size)
if (len == -1) { if (len == -1) {
break break
} else { } else {
baos.write(buffer, 0, len) outputStream.write(buffer, 0, len)
} }
} }
val data = baos.toByteArray() val data = outputStream.toByteArray()
baos.close() outputStream.close()
return data return data
} catch (e: IOException) { } catch (e: IOException) {
return null return null
@ -527,30 +520,20 @@ object FileUtils {
* 保存文件内容 * 保存文件内容
*/ */
fun writeInputStream(file: File, data: InputStream): Boolean { fun writeInputStream(file: File, data: InputStream): Boolean {
var fos: FileOutputStream? = null
return try { return try {
if (!file.exists()) { if (!file.exists()) {
file.parentFile?.mkdirs() file.parentFile?.mkdirs()
file.createNewFile() file.createNewFile()
} }
val buffer = ByteArray(1024 * 4) data.use {
fos = FileOutputStream(file) FileOutputStream(file).use { fos ->
while (true) { data.copyTo(fos)
val len = data.read(buffer, 0, buffer.size) fos.flush()
if (len == -1) {
break
} else {
fos.write(buffer, 0, len)
} }
} }
data.close()
fos.flush()
true true
} catch (e: IOException) { } catch (e: IOException) {
false false
} finally {
closeSilently(fos)
} }
} }
@ -562,7 +545,6 @@ object FileUtils {
var writer: FileWriter? = null var writer: FileWriter? = null
return try { return try {
if (!file.exists()) { if (!file.exists()) {
file.createNewFile() file.createNewFile()
} }
writer = FileWriter(file, true) writer = FileWriter(file, true)
@ -671,15 +653,9 @@ object FileUtils {
val stamp1 = File(path1).lastModified() val stamp1 = File(path1).lastModified()
val stamp2 = File(path2).lastModified() val stamp2 = File(path2).lastModified()
return when { return when {
stamp1 > stamp2 -> { stamp1 > stamp2 -> 1
1 stamp1 < stamp2 -> -1
} else -> 0
stamp1 < stamp2 -> {
-1
}
else -> {
0
}
} }
} }
@ -701,11 +677,7 @@ object FileUtils {
override fun compare(f1: File?, f2: File?): Int { override fun compare(f1: File?, f2: File?): Int {
return if (f1 == null || f2 == null) { return if (f1 == null || f2 == null) {
if (f1 == null) { if (f1 == null) -1 else 1
-1
} else {
1
}
} else { } else {
if (f1.isDirectory && f2.isFile) { if (f1.isDirectory && f2.isFile) {
-1 -1

Loading…
Cancel
Save