WffBinaryMessageOutputStreamer { public int write(final NameValue nameValue) throws IOException { final byte maxNoNameLengthBytes = 4; final byte maxNoValueLengthBytes = 4; int totalBytesWritten = 0; if (firstWrite) { writeByChunk( new byte[] { maxNoNameLengthBytes, maxNoValueLengthBytes }); totalBytesWritten = 2; firstWrite = false; } final byte[] name = nameValue.getName(); final byte[] nameLegthBytes = WffBinaryMessageUtil .getBytesFromInt(name.length); writeByChunk(nameLegthBytes); totalBytesWritten += nameLegthBytes.length; writeByChunk(name); totalBytesWritten += name.length; final byte[][] values = nameValue.getValues(); if (values.length == 0) { writeByChunk(new byte[] { 0, 0, 0, 0 }); totalBytesWritten += maxNoValueLengthBytes; } else { int valueLegth = 0; for (final byte[] value : values) { valueLegth += value.length; } valueLegth += (maxNoValueLengthBytes * values.length); byte[] valueLegthBytes = WffBinaryMessageUtil .getBytesFromInt(valueLegth); writeByChunk(valueLegthBytes); totalBytesWritten += valueLegthBytes.length; for (final byte[] value : values) { valueLegthBytes = WffBinaryMessageUtil .getBytesFromInt(value.length); writeByChunk(valueLegthBytes); totalBytesWritten += valueLegthBytes.length; writeByChunk(value); totalBytesWritten += value.length; } } return totalBytesWritten; } WffBinaryMessageOutputStreamer(final OutputStream os);  WffBinaryMessageOutputStreamer(final OutputStream os,
            final WffBinaryMessageVersion version); int write(final NameValue nameValue); WffBinaryMessageVersion getVersion(); void setChunkSize(final int chunkSize); int getChunkSize();  }
@Test public void testWithoutChunkSize() { ByteArrayOutputStream os = new ByteArrayOutputStream(); WffBinaryMessageOutputStreamer streamer = new WffBinaryMessageOutputStreamer(os); int total = 0; try { List<NameValue> nameValues = new LinkedList<NameValue>(); nameValues.add(new NameValue("key1".getBytes(), new byte[][]{"value1".getBytes()})); nameValues.add(new NameValue("key3".getBytes(), new byte[][]{"value3".getBytes(), "value41".getBytes()})); nameValues.add(new NameValue("key4".getBytes(), new byte[0][0])); nameValues.add(new NameValue("key2".getBytes(), new byte[][]{"value2".getBytes()})); nameValues.add(new NameValue("key5".getBytes(), new byte[0][0])); for (NameValue nameValue : nameValues) { total += streamer.write(nameValue); } } catch (IOException e) { e.printStackTrace(); } byte[] actualMessage = os.toByteArray(); byte[] expectedMessage = { 4, 4, 0, 0, 0, 4, 'k', 'e', 'y', '1', 0, 0, 0, 10, 0, 0, 0, 6, 'v', 'a', 'l', 'u', 'e', '1', 0, 0, 0, 4, 'k', 'e', 'y', '3', 0, 0, 0, 21, 0, 0, 0, 6, 'v', 'a', 'l', 'u', 'e', '3', 0, 0, 0, 7, 'v', 'a', 'l', 'u', 'e', '4', '1', 0, 0, 0, 4, 'k', 'e', 'y', '4', 0, 0, 0, 0, 0, 0, 0, 4, 'k', 'e', 'y', '2', 0, 0, 0, 10, 0, 0, 0, 6, 'v', 'a', 'l', 'u', 'e', '2', 0, 0, 0, 4, 'k', 'e', 'y', '5', 0, 0, 0, 0, }; assertArrayEquals(expectedMessage, actualMessage); assertEquals(expectedMessage.length, total); }