mirror of
https://github.com/revanced/jadb.git
synced 2024-06-02 11:26:12 +02:00
b4f5083d00
The previous AdbFilterInputStream.read function was always waiting for buffer to fill, even if this meant to block for a long time. Now it checks if the next read will block. The one case it still could block, when reading a 0xd (carriage return) is not expected to occur for interactive output (at least as the last character of some input). This removes the need for a separate executeShellRaw method.
48 lines
1.2 KiB
Java
48 lines
1.2 KiB
Java
package se.vidstige.jadb;
|
|
|
|
import java.io.FilterInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
public class AdbFilterInputStream extends FilterInputStream {
|
|
public AdbFilterInputStream(InputStream inputStream) {
|
|
super(inputStream);
|
|
}
|
|
|
|
@Override
|
|
public int read() throws IOException {
|
|
int b1 = in.read();
|
|
if (b1 == 0x0d) {
|
|
in.mark(1);
|
|
int b2 = in.read();
|
|
if (b2 == 0x0a) {
|
|
return b2;
|
|
}
|
|
in.reset();
|
|
}
|
|
return b1;
|
|
}
|
|
|
|
@Override
|
|
public int read(byte[] buffer, int offset, int length) throws IOException {
|
|
int n = 0;
|
|
for (int i = 0; i < length; i++) {
|
|
int b = read();
|
|
if (b == -1) return n == 0 ? -1 : n;
|
|
buffer[offset + n] = (byte) b;
|
|
n++;
|
|
|
|
// Return as soon as no more data is available (and at least one byte was read)
|
|
if (in.available() <= 0) {
|
|
return n;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
@Override
|
|
public int read(byte[] buffer) throws IOException {
|
|
return read(buffer, 0, buffer.length);
|
|
}
|
|
}
|