diff --git a/build.gradle b/build.gradle index 330719c..63fe69e 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.0-alpha11' + classpath 'com.android.tools.build:gradle:3.5.0-alpha12' classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1' diff --git a/core/src/main/java/com/topjohnwu/superuser/internal/ShellImpl.java b/core/src/main/java/com/topjohnwu/superuser/internal/ShellImpl.java index 95ce082..1fc57b2 100644 --- a/core/src/main/java/com/topjohnwu/superuser/internal/ShellImpl.java +++ b/core/src/main/java/com/topjohnwu/superuser/internal/ShellImpl.java @@ -23,6 +23,8 @@ import androidx.annotation.NonNull; import com.topjohnwu.superuser.Shell; import com.topjohnwu.superuser.ShellUtils; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FilterInputStream; import java.io.FilterOutputStream; @@ -56,7 +58,7 @@ class ShellImpl extends Shell { private static class NoCloseInputStream extends FilterInputStream { NoCloseInputStream(InputStream in) { - super(in); + super((in instanceof BufferedInputStream) ? in : new BufferedInputStream(in)); } @Override @@ -70,7 +72,7 @@ class ShellImpl extends Shell { private static class NoCloseOutputStream extends FilterOutputStream { NoCloseOutputStream(@NonNull OutputStream out) { - super(out); + super((out instanceof BufferedOutputStream) ? out : new BufferedOutputStream(out)); } @Override diff --git a/core/src/main/java/com/topjohnwu/superuser/internal/StreamGobbler.java b/core/src/main/java/com/topjohnwu/superuser/internal/StreamGobbler.java index dcd3835..9ef1a20 100644 --- a/core/src/main/java/com/topjohnwu/superuser/internal/StreamGobbler.java +++ b/core/src/main/java/com/topjohnwu/superuser/internal/StreamGobbler.java @@ -16,7 +16,7 @@ package com.topjohnwu.superuser.internal; -import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Collections; @@ -29,6 +29,7 @@ class StreamGobbler implements Callable { private final String token; private final boolean returnCode; + private final StringBuilder sb; private InputStream in; private List list; @@ -36,6 +37,7 @@ class StreamGobbler implements Callable { StreamGobbler(String token, Boolean b) { this.token = token; returnCode = b; + sb = new StringBuilder(); } public Callable set(InputStream in, List list) { @@ -44,30 +46,47 @@ class StreamGobbler implements Callable { return this; } - private void output(String s) { - if (list != null) { - list.add(s); - InternalUtils.log(TAG, s); + private boolean output(String line) { + boolean eof = false; + int sl = line.length() - 1; + int tl = token.length() - 1; + if (sl >= tl) { + eof = true; + for (; tl >= 0; --tl, --sl) { + if (token.charAt(tl) != line.charAt(sl)) { + eof = false; + break; + } + } + if (eof) + line = sl >= 0 ? line.substring(0, sl + 1) : null; } + if (list != null && line != null) { + list.add(line); + InternalUtils.log(TAG, line); + } + return eof; + } + + private String readLine(InputStreamReader reader) throws IOException { + sb.setLength(0); + for (int c;;) { + c = reader.read(); + if (c == '\n' || c == -1) + break; + sb.append((char) c); + } + return sb.toString(); } @Override public Integer call() throws Exception { - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); - String line; - while ((line = reader.readLine()) != null) { - int end = line.lastIndexOf(token); - if (end >= 0) { - if (end > 0) { - while (line.charAt(end - 1) == 0) - --end; - output(line.substring(0, end)); - } + InputStreamReader reader = new InputStreamReader(in, "UTF-8"); + for (;;) { + if (output(readLine(reader))) break; - } - output(line); } - int code = returnCode ? Integer.parseInt(reader.readLine()) : 0; + int code = returnCode ? Integer.parseInt(readLine(reader)) : 0; reader.close(); in = null; list = null;