From 008492e140ea69c4a3cbdd2b9e9154d81310f6ba Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Thu, 25 Jul 2013 21:21:33 +0200 Subject: [PATCH] Refactor: Moving out a method --- src/se/vidstige/jadb/JadbConnection.java | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/se/vidstige/jadb/JadbConnection.java b/src/se/vidstige/jadb/JadbConnection.java index 8538daf..0b39b5c 100644 --- a/src/se/vidstige/jadb/JadbConnection.java +++ b/src/se/vidstige/jadb/JadbConnection.java @@ -3,13 +3,9 @@ package se.vidstige.jadb; import java.io.DataInput; import java.io.DataInputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; -import java.nio.CharBuffer; import java.nio.charset.Charset; public class JadbConnection { @@ -20,15 +16,13 @@ public class JadbConnection { public JadbConnection() throws UnknownHostException, IOException { _socket = new Socket("localhost", DEFAULTPORT); - } public void getHostVersion() throws IOException { - OutputStreamWriter writer = new OutputStreamWriter(_socket.getOutputStream()); + send("host:version"); + DataInput reader = new DataInputStream(_socket.getInputStream()); - writer.write("000Chost:version"); - writer.flush(); - byte[] response = new byte[4]; + byte[] response = new byte[4]; reader.readFully(response); System.out.println(new String(response, Charset.forName("utf-8"))); } @@ -37,4 +31,15 @@ public class JadbConnection { { _socket.close(); } + + 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(); + } }