From 4bf4e24bae55958215cb20cbcdd8ea0e45452705 Mon Sep 17 00:00:00 2001 From: TonyJiangWJ Date: Tue, 14 Dec 2021 23:14:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=84=9A=E6=9C=AC=E9=97=B4?= =?UTF-8?q?=E9=80=9A=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tony/autojs/common/ProcessMappedShare.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/autojs-tool-common/src/main/java/com/tony/autojs/common/ProcessMappedShare.java b/autojs-tool-common/src/main/java/com/tony/autojs/common/ProcessMappedShare.java index 7ae1b77e..848665d7 100644 --- a/autojs-tool-common/src/main/java/com/tony/autojs/common/ProcessMappedShare.java +++ b/autojs-tool-common/src/main/java/com/tony/autojs/common/ProcessMappedShare.java @@ -28,6 +28,7 @@ public class ProcessMappedShare { private volatile boolean subscribing; private volatile boolean unsubscribe; private Object threads; + private static final byte TERMINATE = '\n'; /** * 指定文件路径、通信数据缓冲区字节大小 @@ -79,8 +80,8 @@ public class ProcessMappedShare { FileChannel fc = randomAccessFile.getChannel(); final MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize); final ByteArrayOutputStream bos = new ByteArrayOutputStream(bufferSize); - final byte terminate = '\n'; - clearBuffer(mbb); + + clearBuffer(mbb, bufferSize); unsubscribe = false; subscribing = true; final Thread runningThread = start(new Runnable() { @@ -108,12 +109,12 @@ public class ProcessMappedShare { do { bos.write(read); read = mbb.get(index++); - } while (read != terminate); + } while (read != TERMINATE); String result = new String(bos.toByteArray(), StandardCharsets.UTF_8); callback.call(result); bos.reset(); - clearBuffer(mbb); + clearBuffer(mbb, index); } while (loop && !Thread.currentThread().isInterrupted() && !unsubscribe); subscribing = false; } @@ -155,10 +156,10 @@ public class ProcessMappedShare { * * @param mbb */ - private void clearBuffer(MappedByteBuffer mbb) { + private void clearBuffer(MappedByteBuffer mbb, int readEnd) { // 清除文件内容 - for (int i = 0; i < bufferSize; i++) { - mbb.put(i, (byte) 10); + for (int i = 0; i < readEnd; i++) { + mbb.put(i, TERMINATE); } }