add tests
This commit is contained in:
@@ -95,7 +95,7 @@ public class Parser {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<ByteBuffer> results = new ArrayList<ByteBuffer>(packets.length);
|
final ArrayList<byte[]> results = new ArrayList<byte[]>(packets.length);
|
||||||
|
|
||||||
for (Packet packet : packets) {
|
for (Packet packet : packets) {
|
||||||
encodePacket(packet, new EncodeCallback() {
|
encodePacket(packet, new EncodeCallback() {
|
||||||
@@ -103,31 +103,30 @@ public class Parser {
|
|||||||
public void call(Object packet) {
|
public void call(Object packet) {
|
||||||
if (packet instanceof String) {
|
if (packet instanceof String) {
|
||||||
String encodingLength = String.valueOf(((String)packet).getBytes(Charset.forName("UTF-8")).length);
|
String encodingLength = String.valueOf(((String)packet).getBytes(Charset.forName("UTF-8")).length);
|
||||||
ByteBuffer sizeBuffer = ByteBuffer.allocate(encodingLength.length() + 2);
|
byte[] sizeBuffer = new byte[encodingLength.length() + 2];
|
||||||
|
|
||||||
sizeBuffer.put((byte)0); // is a string
|
sizeBuffer[0] = (byte)0; // is a string
|
||||||
for (char ch : encodingLength.toCharArray()) {
|
for (int i = 0; i < encodingLength.length(); i ++) {
|
||||||
sizeBuffer.put((byte)Character.getNumericValue(ch));
|
sizeBuffer[i + 1] = (byte)Character.getNumericValue(encodingLength.charAt(i));
|
||||||
}
|
}
|
||||||
sizeBuffer.put((byte)255);
|
sizeBuffer[sizeBuffer.length - 1] = (byte)255;
|
||||||
results.add(Buffer.concat(new ByteBuffer[] {sizeBuffer,
|
results.add(Buffer.concat(new byte[][] {sizeBuffer, ((String)packet).getBytes(Charset.forName("UTF-8"))}));
|
||||||
ByteBuffer.wrap(((String)packet).getBytes(Charset.forName("UTF-8")))}));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String encodingLength = String.valueOf(((ByteBuffer)packet).capacity());
|
String encodingLength = String.valueOf(((byte[])packet).length);
|
||||||
ByteBuffer sizeBuffer = ByteBuffer.allocate(encodingLength.length() + 2);
|
byte[] sizeBuffer = new byte[encodingLength.length() + 2];
|
||||||
sizeBuffer.put((byte)1); // is binary
|
sizeBuffer[0] = (byte)1; // is binary
|
||||||
for (char ch : encodingLength.toCharArray()) {
|
for (int i = 0; i < encodingLength.length(); i ++) {
|
||||||
sizeBuffer.put((byte)ch);
|
sizeBuffer[i + 1] = (byte)Character.getNumericValue(encodingLength.charAt(i));
|
||||||
}
|
}
|
||||||
sizeBuffer.put((byte)255);
|
sizeBuffer[sizeBuffer.length - 1] = (byte)255;
|
||||||
results.add(Buffer.concat(new ByteBuffer[] {sizeBuffer, (ByteBuffer)packet}));
|
results.add(Buffer.concat(new byte[][] {sizeBuffer, (byte[])packet}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
callback.call(Buffer.concat(results.toArray(new ByteBuffer[results.size()])).array());
|
callback.call(Buffer.concat(results.toArray(new byte[results.size()][])));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void decodePayload(String data, DecodePayloadCallback<String> callback) {
|
public static void decodePayload(String data, DecodePayloadCallback<String> callback) {
|
||||||
@@ -245,27 +244,26 @@ class Buffer {
|
|||||||
|
|
||||||
private Buffer() {}
|
private Buffer() {}
|
||||||
|
|
||||||
public static ByteBuffer concat(ByteBuffer[] list) {
|
public static byte[] concat(byte[][] list) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
for (ByteBuffer buf : list) {
|
for (byte[] buf : list) {
|
||||||
length += buf.capacity();
|
length += buf.length;
|
||||||
}
|
}
|
||||||
return concat(list, length);
|
return concat(list, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ByteBuffer concat(ByteBuffer[] list, int length) {
|
public static byte[] concat(byte[][] list, int length) {
|
||||||
if (list.length == 0) {
|
if (list.length == 0) {
|
||||||
return ByteBuffer.allocate(0);
|
return new byte[0];
|
||||||
} else if (list.length == 1) {
|
} else if (list.length == 1) {
|
||||||
return list[0];
|
return list[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(length);
|
ByteBuffer buffer = ByteBuffer.allocate(length);
|
||||||
for (ByteBuffer buf : list) {
|
for (byte[] buf : list) {
|
||||||
buf.clear();
|
|
||||||
buffer.put(buf);
|
buffer.put(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buffer;
|
return buffer.array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -288,4 +288,86 @@ public class ParserTest {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encodeBinaryMessage() {
|
||||||
|
final byte[] data = new byte[5];
|
||||||
|
for (int i = 0; i < data.length; i++) {
|
||||||
|
data[0] = (byte)i;
|
||||||
|
}
|
||||||
|
encodePacket(new Packet<byte[]>(Packet.MESSAGE, data), new EncodeCallback<byte[]>() {
|
||||||
|
@Override
|
||||||
|
public void call(byte[] encoded) {
|
||||||
|
Packet<byte[]> p = decodePacket(encoded);
|
||||||
|
assertThat(p.type, is(Packet.MESSAGE));
|
||||||
|
assertThat(p.data, is(data));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encodeBinaryContents() {
|
||||||
|
final byte[] firstBuffer = new byte[5];
|
||||||
|
for (int i = 0 ; i < firstBuffer.length; i++) {
|
||||||
|
firstBuffer[0] = (byte)i;
|
||||||
|
}
|
||||||
|
final byte[] secondBuffer = new byte[4];
|
||||||
|
for (int i = 0 ; i < secondBuffer.length; i++) {
|
||||||
|
secondBuffer[0] = (byte)(firstBuffer.length + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
encodePayload(new Packet[]{
|
||||||
|
new Packet<byte[]>(Packet.MESSAGE, firstBuffer),
|
||||||
|
new Packet<byte[]>(Packet.MESSAGE, secondBuffer),
|
||||||
|
}, new EncodeCallback<byte[]>() {
|
||||||
|
@Override
|
||||||
|
public void call(byte[] data) {
|
||||||
|
decodePayload(data, new DecodePayloadCallback() {
|
||||||
|
@Override
|
||||||
|
public boolean call(Packet packet, int index, int total) {
|
||||||
|
boolean isLast = index + 1 == total;
|
||||||
|
assertThat(packet.type, is(Packet.MESSAGE));
|
||||||
|
if (!isLast) {
|
||||||
|
assertThat((byte[])packet.data, is(firstBuffer));
|
||||||
|
} else {
|
||||||
|
assertThat((byte[])packet.data, is(secondBuffer));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void encodeMixedBinaryAndStringContents() {
|
||||||
|
final byte[] firstBuffer = new byte[123];
|
||||||
|
for (int i = 0 ; i < firstBuffer.length; i++) {
|
||||||
|
firstBuffer[0] = (byte)i;
|
||||||
|
}
|
||||||
|
encodePayload(new Packet[]{
|
||||||
|
new Packet<byte[]>(Packet.MESSAGE, firstBuffer),
|
||||||
|
new Packet<String>(Packet.MESSAGE, "hello"),
|
||||||
|
new Packet<String>(Packet.CLOSE),
|
||||||
|
}, new EncodeCallback<byte[]>() {
|
||||||
|
@Override
|
||||||
|
public void call(byte[] encoded) {
|
||||||
|
decodePayload(encoded, new DecodePayloadCallback() {
|
||||||
|
@Override
|
||||||
|
public boolean call(Packet packet, int index, int total) {
|
||||||
|
if (index == 0) {
|
||||||
|
assertThat(packet.type, is(Packet.MESSAGE));
|
||||||
|
assertThat((byte[])packet.data, is(firstBuffer));
|
||||||
|
} else if (index == 1) {
|
||||||
|
assertThat(packet.type, is(Packet.MESSAGE));
|
||||||
|
assertThat((String)packet.data, is("hello"));
|
||||||
|
} else {
|
||||||
|
assertThat(packet.type, is(Packet.CLOSE));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user