From c9a0a59ace5efab776573deb8bdcf4f729393f5a Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Mon, 28 Mar 2016 09:41:53 +0200 Subject: [PATCH] Filtering out 0x0d, 0x0a sequences from shell commands. Makes screenshotting work again. :-) --- .gitignore | 1 + .../vidstige/jadb/AdbFilterOutputStream.java | 21 +++++++++ src/se/vidstige/jadb/JadbDevice.java | 2 +- .../jadb/test/AdbOutputStreamFixture.java | 44 +++++++++++++++++++ .../jadb/test/RealDeviceTestCases.java | 9 ++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/se/vidstige/jadb/AdbFilterOutputStream.java create mode 100644 test/se/vidstige/jadb/test/AdbOutputStreamFixture.java diff --git a/.gitignore b/.gitignore index 01be335..0955094 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ Generated by tests /foobar.md /xyz +screenshot.png ################# ## Eclipse diff --git a/src/se/vidstige/jadb/AdbFilterOutputStream.java b/src/se/vidstige/jadb/AdbFilterOutputStream.java new file mode 100644 index 0000000..c385f38 --- /dev/null +++ b/src/se/vidstige/jadb/AdbFilterOutputStream.java @@ -0,0 +1,21 @@ +package se.vidstige.jadb; + +import java.io.IOException; +import java.io.OutputStream; + +public class AdbFilterOutputStream extends LookBackFilteringOutputStream { + public AdbFilterOutputStream(OutputStream inner) { + super(inner, 1); + } + + @Override + public void write(int c) throws IOException { + if (!lookback().isEmpty()) { + Byte last = lookback().getFirst(); + if (last != null && last == 0x0d && c == 0x0a) { + unwrite(); + } + } + super.write(c); + } +} diff --git a/src/se/vidstige/jadb/JadbDevice.java b/src/se/vidstige/jadb/JadbDevice.java index 365b148..d86d3ae 100644 --- a/src/se/vidstige/jadb/JadbDevice.java +++ b/src/se/vidstige/jadb/JadbDevice.java @@ -60,7 +60,7 @@ public class JadbDevice { } send(transport, "shell:" + shellLine.toString()); if (stdout != null) { - transport.readResponseTo(stdout); + transport.readResponseTo(new AdbFilterOutputStream(stdout)); } } diff --git a/test/se/vidstige/jadb/test/AdbOutputStreamFixture.java b/test/se/vidstige/jadb/test/AdbOutputStreamFixture.java new file mode 100644 index 0000000..9d6c5df --- /dev/null +++ b/test/se/vidstige/jadb/test/AdbOutputStreamFixture.java @@ -0,0 +1,44 @@ +package se.vidstige.jadb.test; + +import org.junit.Assert; +import org.junit.Test; +import se.vidstige.jadb.AdbFilterOutputStream; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class AdbOutputStreamFixture { + + private byte[] passthrough(byte[] input) throws IOException { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + OutputStream sut = new AdbFilterOutputStream(output); + sut.write(input); + sut.flush(); + return output.toByteArray(); + } + + @Test + public void testSimple() throws Exception { + byte[] actual = passthrough(new byte[]{ 1, 2, 3}); + Assert.assertArrayEquals(new byte[]{ 1, 2, 3}, actual); + } + + @Test + public void testEmpty() throws Exception { + byte[] actual = passthrough(new byte[]{}); + Assert.assertArrayEquals(new byte[]{}, actual); + } + + @Test + public void testSimpleRemoval() throws Exception { + byte[] actual = passthrough(new byte[]{0x0d, 0x0a}); + Assert.assertArrayEquals(new byte[]{0x0a}, actual); + } + + @Test + public void testDoubleRemoval() throws Exception { + byte[] actual = passthrough(new byte[]{0x0d, 0x0a, 0x0d, 0x0a}); + Assert.assertArrayEquals(new byte[]{0x0a, 0x0a}, actual); + } +} diff --git a/test/se/vidstige/jadb/test/RealDeviceTestCases.java b/test/se/vidstige/jadb/test/RealDeviceTestCases.java index a7ed348..0bc6e1b 100644 --- a/test/se/vidstige/jadb/test/RealDeviceTestCases.java +++ b/test/se/vidstige/jadb/test/RealDeviceTestCases.java @@ -7,6 +7,7 @@ import se.vidstige.jadb.JadbException; import se.vidstige.jadb.RemoteFile; import java.io.File; +import java.io.FileOutputStream; import java.util.List; public class RealDeviceTestCases { @@ -77,4 +78,12 @@ public class RealDeviceTestCases { any.executeShell(System.out, "ls /"); any.executeShell(System.out, "ls", "-la", "/"); } + + @Test + public void testScreenshot() throws Exception { + JadbConnection jadb = new JadbConnection(); + JadbDevice any = jadb.getAnyDevice(); + FileOutputStream outputStream = new FileOutputStream(new File("screenshot.png")); + any.executeShell(outputStream, "screencap", "-p"); + } }