From 27c7ccc10dc024c7cbad756606ba6d3d7ff77eca Mon Sep 17 00:00:00 2001 From: HapeLee <63206378+HapeLee@users.noreply.github.com> Date: Mon, 11 May 2026 01:08:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=9C=AC=E5=9C=B0=E4=B9=A6?= =?UTF-8?q?=E7=B1=8D=E6=97=B6=EF=BC=8C=E9=94=99=E8=AF=AF=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=AC=A6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/book/BookHelp.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookHelp.kt b/app/src/main/java/io/legado/app/help/book/BookHelp.kt index 18562a43d..9d79cfd18 100644 --- a/app/src/main/java/io/legado/app/help/book/BookHelp.kt +++ b/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) {