From fa74fd88bd1a40844bb07049e5a07bb64aae4f05 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Thu, 20 Mar 2014 17:30:21 +0100 Subject: [PATCH] Fixing a bug where jadb would hang if no devices where found. --- src/se/vidstige/jadb/JadbConnection.java | 4 +++- .../vidstige/jadb/server/AdbProtocolHandler.java | 14 +++++++------- test/se/vidstige/jadb/test/MockedTestCases.java | 9 +++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 5d7f1f0..d03aa0f 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -52,7 +52,9 @@ public class JadbConnection { for (String line : lines) { String[] parts = line.split("\t"); - devices.add(new JadbDevice(parts[0], parts[1], main)); + if (parts.length > 1) { + devices.add(new JadbDevice(parts[0], parts[1], main)); + } } return devices; } diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index b991f98..57b65d0 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -1,9 +1,6 @@ package se.vidstige.jadb.server; -import java.io.DataInput; -import java.io.DataInputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; +import java.io.*; import java.net.Socket; import java.nio.charset.Charset; @@ -47,11 +44,14 @@ public class AdbProtocolHandler implements Runnable { output.write("OKAY"); } else if ("host:devices".equals(command)) { - output.write("OKAY"); + ByteArrayOutputStream tmp = new ByteArrayOutputStream(); + DataOutputStream writer = new DataOutputStream(tmp); for (AdbDeviceResponder d : responder.getDevices()) { - send(output, d.getSerial() + "\t" + d.getType() + "\n"); + writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n"); } + output.write("OKAY"); + send(output, new String(tmp.toByteArray(), Charset.forName("utf-8"))); } else { @@ -61,7 +61,7 @@ public class AdbProtocolHandler implements Runnable { output.flush(); } } catch (IOException e) { - System.out.println(e.getMessage()); + System.out.println("IO Error: " + e.getMessage()); } } diff --git a/test/se/vidstige/jadb/test/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java index 4bc44c7..a54aa3c 100644 --- a/test/se/vidstige/jadb/test/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/MockedTestCases.java @@ -6,8 +6,6 @@ import org.junit.Before; import org.junit.Test; import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbDevice; -import se.vidstige.jadb.server.AdbServer; -import se.vidstige.jadb.server.SocketServer; import se.vidstige.jadb.test.fakes.FakeAdbServer; import java.util.List; @@ -36,4 +34,11 @@ public class MockedTestCases { List devices = connection.getDevices(); Assert.assertEquals("serial-123", devices.get(0).getSerial()); } + + @Test + public void testListNoDevices() throws Exception { + List devices = connection.getDevices(); + Assert.assertEquals(0, devices.size()); + } + }