Skip to content

Commit

Permalink
修复 Shell的内存泄漏问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hyb1996 committed Jan 21, 2019
1 parent 5cf08a9 commit 91365c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
45 changes: 30 additions & 15 deletions autojs/src/main/java/com/stardust/autojs/core/util/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import android.preference.PreferenceManager;
import android.util.Log;

import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.autojs.runtime.api.AbstractShell;
import com.stardust.autojs.runtime.exception.ScriptInterruptedException;
import com.stardust.autojs.runtime.ScriptRuntime;
import com.stardust.io.ByteBufferBackedInputStream;
import com.stardust.io.ByteBufferBackedOutputStream;
import com.stardust.lang.ThreadCompat;
import com.stardust.pio.UncheckedIOException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;

import jackpal.androidterm.ShellTermSession;
import jackpal.androidterm.emulatorview.TermSession;
Expand Down Expand Up @@ -131,13 +132,13 @@ private void ensureInitialized() {
checkInitException();
throw new IllegalStateException();
}
}else {
} else {
logDebug("ensureInitialized: init");
}
}

private void logDebug(String log){
if(DEBUG){
private void logDebug(String log) {
if (DEBUG) {
Log.d(TAG, log);
}
}
Expand All @@ -150,7 +151,7 @@ private void checkInitException() {

private void waitInitialization() {
synchronized (mInitLock) {
if(mInitialized){
if (mInitialized) {
return;
}
logDebug("waitInitialization: enter");
Expand Down Expand Up @@ -204,15 +205,16 @@ public TermSession getTermSession() {

private class MyShellTermSession extends ShellTermSession {

private final ByteBuffer mByteBuffer = ByteBuffer.allocate(8192);
private BufferedReader mBufferedReader;
private OutputStream mOutputStream;
private Thread mReadingThread;
private volatile boolean mReading = false;

public MyShellTermSession(TermSettings settings, String initialCommand) throws IOException {
super(settings, initialCommand);
PipedInputStream pipedInputStream = new PipedInputStream(8192);
mBufferedReader = new BufferedReader(new InputStreamReader(pipedInputStream));
mOutputStream = new PipedOutputStream(pipedInputStream);
mBufferedReader = new BufferedReader(new InputStreamReader(new ByteBufferBackedInputStream(mByteBuffer)));
mOutputStream = new ByteBufferBackedOutputStream(mByteBuffer);
if (mShouldReadOutput) {
startReadingThread();
}
Expand All @@ -222,14 +224,23 @@ private void startReadingThread() {
mReadingThread = new ThreadCompat(() -> {
String line;
try {
while (!Thread.currentThread().isInterrupted()
&& (line = mBufferedReader.readLine()) != null) {
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();
}

Expand All @@ -248,7 +259,7 @@ private void onNewLine(String line) {
}
}

private void onOutput(String str){
private void onOutput(String str) {
logDebug("onOutput: " + str);
if (!mInitialized) {
if (isRoot() && str.endsWith(":/ # ")) {
Expand All @@ -265,7 +276,9 @@ private void onOutput(String str){
protected void processInput(byte[] data, int offset, int count) {
try {
onOutput(new String(data, offset, count));
mOutputStream.write(data, offset, count);
synchronized (mByteBuffer) {
mOutputStream.write(data, offset, count);
}
} catch (IOException e) {
e.printStackTrace();
finish();
Expand Down Expand Up @@ -304,8 +317,10 @@ public void finish() {
super.finish();
if (!mShouldReadOutput)
return;
if(mReadingThread != null){
mReading = false;
if (mReadingThread != null) {
mReadingThread.interrupt();
mReadingThread = null;
}
try {
mBufferedReader.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stardust.io

import java.io.IOException
import java.io.InputStream
import java.nio.ByteBuffer

class ByteBufferBackedInputStream(private var buf: ByteBuffer) : InputStream() {

@Throws(IOException::class)
override fun read(): Int {
return if (!buf.hasRemaining()) {
-1
} else buf.get().toInt() and 0xFF
}

@Throws(IOException::class)
override fun read(bytes: ByteArray, off: Int, len: Int): Int {
if (!buf.hasRemaining()) {
return -1
}
val read = Math.min(len, available())
buf.get(bytes, off, read)
buf.position(buf.position() - read)
return read
}

override fun available(): Int {
return buf.position()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.stardust.io

import java.io.IOException
import java.io.OutputStream
import java.nio.ByteBuffer

class ByteBufferBackedOutputStream(private var buf: ByteBuffer) : OutputStream() {

@Throws(IOException::class)
override fun write(b: Int) {
buf.put(b.toByte())
}

@Throws(IOException::class)
override fun write(bytes: ByteArray, off: Int, len: Int) {
buf.put(bytes, off, len)
}

}

0 comments on commit 91365c6

Please sign in to comment.