From 054b1fe42a0ecd23ecf7bf2ca12cc45e25a15fd6 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Tue, 22 Jan 2019 10:13:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20shell.execAndWaitFor()?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E4=B8=80=E6=AC=A1=E5=91=BD=E4=BB=A4=E7=9A=84?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/caches/build_file_checksums.ser | Bin 733 -> 733 bytes .../com/stardust/autojs/core/util/Shell.java | 123 ++++++++---------- .../autojs/runtime/api/AbstractShell.java | 1 - 3 files changed, 55 insertions(+), 69 deletions(-) diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 82ae2df753cfd8cde70c64fec4b80d239d8ecdf0..000bfdb50684604ab40d47b71b3d39b5ad11250c 100644 GIT binary patch delta 15 Xcmcc1dY5&=43>hsOEosmabyAjI8Fwe delta 15 Xcmcc1dY5&=3>NQuD}HaB mCommandOutputs = new ArrayList<>(); public MyShellTermSession(TermSettings settings, String initialCommand) throws IOException { super(settings, initialCommand); - mBufferedReader = new BufferedReader(new InputStreamReader(new ByteBufferBackedInputStream(mByteBuffer))); - mOutputStream = new ByteBufferBackedOutputStream(mByteBuffer); - if (mShouldReadOutput) { - startReadingThread(); - } - } - - private void startReadingThread() { - mReadingThread = new ThreadCompat(() -> { - String line; - try { - while (true) { - if (!mReading) { - break; - } - synchronized (mByteBuffer) { - line = mBufferedReader.readLine(); - } - if (line == null) { - break; - } - onNewLine(line); - } - } catch (IOException e) { - e.printStackTrace(); - } - }); - mReading = true; - mReadingThread.start(); } private void onNewLine(String line) { @@ -250,6 +226,8 @@ public class Shell extends AbstractShell { if (!isRoot() && line.endsWith(" $ sh")) { notifyInitialized(); } + } else { + mCommandOutputs.add(line); } if (mCallback != null) { mCallback.onNewLine(line); @@ -259,6 +237,21 @@ public class Shell extends AbstractShell { } } + private void onCommandOutput(ArrayList output) { + StringBuilder result = new StringBuilder(); + for (int i = 1; i < output.size(); i++) { + result.append(output.get(i)); + if (i < output.size() - 1) { + result.append("\n"); + } + } + logDebug("onCommandOutput: lines = " + output + ", output = " + result); + synchronized (mCommandOutputLock) { + mCommandOutput = result.toString(); + mCommandOutputLock.notifyAll(); + } + } + private void onOutput(String str) { logDebug("onOutput: " + str); if (!mInitialized) { @@ -266,23 +259,34 @@ public class Shell extends AbstractShell { notifyInitialized(); } } + int start = 0; + int i; + while (true) { + i = str.indexOf("\n", start); + if (i > 0) { + onNewLine((mStringBuffer.toString() + str.substring(0, i - 1)).trim()); + mStringBuffer.delete(0, mStringBuffer.length()); + } else { + if (start <= str.length() - 1) { + mStringBuffer.append(str.substring(start)); + } + break; + } + start = i + 1; + } + if (str.endsWith(" # ") || str.endsWith(" $ ")) { + onCommandOutput(mCommandOutputs); + mCommandOutputs.clear(); + } if (mCallback != null) { - mCallback.onOutput(str); + mCallback.onOutput(str.replace("\r", "")); } } @Override protected void processInput(byte[] data, int offset, int count) { - try { - onOutput(new String(data, offset, count)); - synchronized (mByteBuffer) { - mOutputStream.write(data, offset, count); - } - } catch (IOException e) { - e.printStackTrace(); - finish(); - } + onOutput(new String(data, offset, count)); } private void notifyExit() { @@ -312,23 +316,6 @@ public class Shell extends AbstractShell { } } - @Override - public void finish() { - super.finish(); - if (!mShouldReadOutput) - return; - mReading = false; - if (mReadingThread != null) { - mReadingThread.interrupt(); - mReadingThread = null; - } - try { - mBufferedReader.close(); - mOutputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } } } diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/AbstractShell.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/AbstractShell.java index 24911ec7..f0d17a5b 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/AbstractShell.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/AbstractShell.java @@ -12,7 +12,6 @@ import com.stardust.util.ScreenMetrics; public abstract class AbstractShell { - public static class Result { public int code = -1; public String error;