X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Fjvcard%2Fremote%2FSimpleSocket.java;h=264fff5c68b458e6a12e45c315da306cb0b04f1e;hb=d459d7e12a3398344e021b1b12c94a8d647cb6c4;hp=acc43b295c352bad99f3e09103b632397a27e03a;hpb=845fb1d7c3adc0d6cdf9465c0e983ba447cfab6d;p=jvcard.git diff --git a/src/be/nikiroo/jvcard/remote/SimpleSocket.java b/src/be/nikiroo/jvcard/remote/SimpleSocket.java index acc43b2..264fff5 100644 --- a/src/be/nikiroo/jvcard/remote/SimpleSocket.java +++ b/src/be/nikiroo/jvcard/remote/SimpleSocket.java @@ -1,6 +1,7 @@ package be.nikiroo.jvcard.remote; import java.io.BufferedReader; +import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; @@ -19,10 +20,57 @@ import java.util.List; * @author niki */ public class SimpleSocket { + /** + * An {@link Appendable} that can be used to send data over a + * {@link SimpleSocket}. You must close it to send the end of block element. + * + * @author niki + * + */ + public class BlockAppendable implements Appendable, Closeable { + private SimpleSocket ss; + + /** + * Create a new {@link BlockAppendable} for the given + * {@link SimpleSocket}. + * + * @param ss + * the {@link SimpleSocket} + */ + public BlockAppendable(SimpleSocket ss) { + this.ss = ss; + } + + @Override + public Appendable append(CharSequence csq) throws IOException { + ss.send(csq); + return this; + } + + @Override + public Appendable append(char c) throws IOException { + ss.send("" + c); + return this; + } + + @Override + public Appendable append(CharSequence csq, int start, int end) + throws IOException { + ss.send(csq.subSequence(start, end)); + return this; + } + + @Override + public void close() throws IOException { + ss.sendBlock(); + } + + } + /** * The current version of the network protocol. */ - static private final int CURRENT_VERSION = 1; + static public final int CURRENT_VERSION = 1; /** * The end of block marker. @@ -141,12 +189,12 @@ public class SimpleSocket { * @throws IOException * in case of IO error */ - protected void send(String data) throws IOException { + protected void send(CharSequence data) throws IOException { if (data != null) { - out.write(data); + out.append(data); } - out.write("\n"); + out.append("\n"); if (out.checkError()) throw new IOException(); @@ -220,7 +268,19 @@ public class SimpleSocket { * in case of IO error */ public void sendCommand(Command command, String param) throws IOException { - sendLine(new CommandInstance(command, param, CURRENT_VERSION).toString()); + sendLine(new CommandInstance(command, param, CURRENT_VERSION) + .toString()); + } + + /** + * Create a new {@link Appendable} that can be used to send data on this + * {@link SimpleSocket}. When you are done, just call + * {@link BlockAppendable#close()}. + * + * @return the {@link Appendable} + */ + public BlockAppendable createBlockAppendable() { + return new BlockAppendable(this); } /**