Просмотр исходного кода

编辑本地书籍时,错误删除换行符的问题

HapeLee 6 месяцев назад
Родитель
Сommit
27c7ccc10d
1 измененных файлов с 15 добавлено и 4 удалено
  1. 15 4
      app/src/main/java/io/legado/app/help/book/BookHelp.kt

+ 15 - 4
app/src/main/java/io/legado/app/help/book/BookHelp.kt

@@ -215,10 +215,6 @@ object BookHelp {
         val uri = book.getLocalUri()
         val charset = book.fileCharset()
         val oldTitle = bookChapter.title
-        val newContent = oldTitle + "\n" + content
-        val newBytes = newContent.toByteArray(charset)
-        val oldLength = end - start
-        val diff = newBytes.size - oldLength
 
         val pfd = if (uri.isContentScheme()) {
             appCtx.contentResolver.openFileDescriptor(uri, "rw")
@@ -231,6 +227,21 @@ object BookHelp {
             try {
                 val totalLength = Os.fstat(fd).st_size
 
+                // 尝试保留章节末尾的换行符
+                val oldLength = end - start
+                val readLen = if (oldLength > 32) 32 else oldLength.toInt()
+                var gap = ""
+                if (readLen > 0) {
+                    val lastBytes = ByteArray(readLen)
+                    Os.pread(fd, lastBytes, 0, readLen, end - readLen)
+                    val lastString = String(lastBytes, charset)
+                    gap = lastString.takeLastWhile { c -> c == '\n' || c == '\r' }
+                }
+
+                val newContent = oldTitle + "\n" + content + gap
+                val newBytes = newContent.toByteArray(charset)
+                val diff = newBytes.size - oldLength
+
                 if (diff != 0L) {
                     val remaining = totalLength - end
                     if (remaining > 0) {