From 831ee1ecbc71e9011f90a2f0d6312e722bd6a2d2 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Thu, 25 Jul 2013 21:38:44 +0200 Subject: [PATCH] Refactor: Moving out transport code into its own class. --- src/se/vidstige/jadb/JadbConnection.java | 49 +++++------------------ src/se/vidstige/jadb/Transport.java | 50 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 40 deletions(-) create mode 100644 src/se/vidstige/jadb/Transport.java diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 289c0a1..d4dd85b 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -1,68 +1,37 @@ package se.vidstige.jadb; -import java.io.DataInput; -import java.io.DataInputStream; import java.io.IOException; -import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; -import java.nio.charset.Charset; public class JadbConnection { - private Socket _socket; + private final Socket _socket; private static final int DEFAULTPORT = 5037; + private final Transport transport; + public JadbConnection() throws UnknownHostException, IOException { _socket = new Socket("localhost", DEFAULTPORT); + transport = new Transport(_socket.getOutputStream(), _socket.getInputStream()); } public void getHostVersion() throws IOException, JadbException { - send("host:version"); - verifyResponse(); + transport.send("host:version"); + transport.verifyResponse(); } public void getDevices() throws IOException, JadbException { - send("host:devices"); - verifyResponse(); - String body = readString(); + transport.send("host:devices"); + transport.verifyResponse(); + String body = transport.readString(); System.out.println(body); } - private String readString() throws IOException { - String encodedLength = readString(4); - int length = Integer.parseInt(encodedLength, 16); - return readString(length); - } - public void close() throws IOException { _socket.close(); } - - private void verifyResponse() throws IOException, JadbException { - String response = readString(4); - if ("OKAY".equals(response) == false) throw new JadbException("command failed"); - } - - private String readString(int length) throws IOException { - DataInput reader = new DataInputStream(_socket.getInputStream()); - byte[] responseBuffer = new byte[length]; - reader.readFully(responseBuffer); - String response = new String(responseBuffer, Charset.forName("utf-8")); - return response; - } - - private String getCommandLength(String command) { - return String.format("%04x", Integer.valueOf(command.length())); - } - - private void send(String command) throws IOException { - OutputStreamWriter writer = new OutputStreamWriter(_socket.getOutputStream()); - writer.write(getCommandLength(command)); - writer.write(command); - writer.flush(); - } } diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java new file mode 100644 index 0000000..09fddcc --- /dev/null +++ b/src/se/vidstige/jadb/Transport.java @@ -0,0 +1,50 @@ +package se.vidstige.jadb; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; + +class Transport { + + private final OutputStream outputStream; + private final InputStream inputStream; + + public Transport(OutputStream outputStream, InputStream inputStream) { + this.outputStream = outputStream; + this.inputStream = inputStream; + } + + public String readString() throws IOException { + String encodedLength = readString(4); + int length = Integer.parseInt(encodedLength, 16); + return readString(length); + } + + public void verifyResponse() throws IOException, JadbException { + String response = readString(4); + if ("OKAY".equals(response) == false) throw new JadbException("command failed"); + } + + public String readString(int length) throws IOException { + DataInput reader = new DataInputStream(inputStream); + byte[] responseBuffer = new byte[length]; + reader.readFully(responseBuffer); + String response = new String(responseBuffer, Charset.forName("utf-8")); + return response; + } + + public String getCommandLength(String command) { + return String.format("%04x", Integer.valueOf(command.length())); + } + + public void send(String command) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outputStream); + writer.write(getCommandLength(command)); + writer.write(command); + writer.flush(); + } +}