org.less4j
Class Netstring
java.lang.Object
org.less4j.Netstring
public class Netstring
- extends java.lang.Object
Conveniences to send and receive netstrings efficiently over Java's
synchronous socket API.
Synosis
import org.less4j.Netstring;
import java.net.Socket;
import java.util.Iterator;
Socket conn = new Socket("127.0.0.2", 3999)
try {
Netstring.send(
conn, "[\"CREATE TABLE relation (s, p, o, c)\", []]", "UTF-8"
);
Netstring.send(
conn, "[\"SELECT * FROM relation\", []]", "UTF-8"
);
Iterator netstring = new Netstring.recv(conn, 16384, "UTF-8");
while (netstring.hasNext())
System.out.println (netstring.next());
} finally {
conn.close ();
}
This class is a lazy implementation of an netstring stream
collector, just enough to support netstring protocols.
- Version:
- 0.10
|
Method Summary |
static java.util.Iterator |
decode(byte[] buffer,
java.lang.String encoding)
|
static java.util.Iterator |
recv(java.net.Socket conn,
int limit)
|
static java.util.Iterator |
recv(java.net.Socket conn,
int limit,
java.lang.String encoding)
|
static void |
send(java.net.Socket conn,
byte[] buffer)
|
static void |
send(java.net.Socket conn,
byte[] buffer,
int off,
int len)
|
static void |
send(java.net.Socket conn,
java.util.Iterator strings,
java.lang.String encoding)
Iterate through Strings, queue the encoded 8-bit byte
netstrings in a single ByteBuffer, then dump it in a
single byte array, write it to the conn
socket's output stream and finally flush that stream. |
static void |
send(java.net.Socket conn,
java.lang.String string,
java.lang.String encoding)
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Netstring
public Netstring()
decode
public static java.util.Iterator decode(byte[] buffer,
java.lang.String encoding)
recv
public static java.util.Iterator recv(java.net.Socket conn,
int limit)
throws java.io.IOException
- Parameters:
conn - limit -
- Returns:
-
- Throws:
java.io.IOException
recv
public static java.util.Iterator recv(java.net.Socket conn,
int limit,
java.lang.String encoding)
throws java.io.IOException
- Parameters:
conn - limit - encoding -
- Returns:
-
- Throws:
java.io.IOException
send
public static void send(java.net.Socket conn,
byte[] buffer)
throws java.io.IOException
- Parameters:
os - buffer -
- Throws:
java.io.IOException
send
public static void send(java.net.Socket conn,
byte[] buffer,
int off,
int len)
throws java.io.IOException
- Parameters:
os - buffer - off - len -
- Throws:
java.io.IOException
send
public static void send(java.net.Socket conn,
java.util.Iterator strings,
java.lang.String encoding)
throws java.io.IOException
- Iterate through
Strings, queue the encoded 8-bit byte
netstrings in a single ByteBuffer, then dump it in a
single byte array, write it to the conn
socket's output stream and finally flush that stream.
The purpose is to buffer small strings before actually sending
data through the socket in order to minimize local overhead and
network latency. I found out writing a byte at a time to a TCP
socket OutputStream that each byte can waste a few
UDP datagrams and considerably slow down its application.
Again, let's trade space for speed.
- Parameters:
conn - strings - encoding -
- Throws:
java.io.IOException
send
public static void send(java.net.Socket conn,
java.lang.String string,
java.lang.String encoding)
throws java.io.IOException
- Parameters:
os - string - encoding -
- Throws:
java.io.IOException