|
@@ -215,10 +215,6 @@ object BookHelp {
|
|
|
val uri = book.getLocalUri()
|
|
val uri = book.getLocalUri()
|
|
|
val charset = book.fileCharset()
|
|
val charset = book.fileCharset()
|
|
|
val oldTitle = bookChapter.title
|
|
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()) {
|
|
val pfd = if (uri.isContentScheme()) {
|
|
|
appCtx.contentResolver.openFileDescriptor(uri, "rw")
|
|
appCtx.contentResolver.openFileDescriptor(uri, "rw")
|
|
@@ -231,6 +227,21 @@ object BookHelp {
|
|
|
try {
|
|
try {
|
|
|
val totalLength = Os.fstat(fd).st_size
|
|
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) {
|
|
if (diff != 0L) {
|
|
|
val remaining = totalLength - end
|
|
val remaining = totalLength - end
|
|
|
if (remaining > 0) {
|
|
if (remaining > 0) {
|