StringAdapter extends TypeAdapter<String> { @Override public String deserialize(byte[] buffer, int bufferPosition) { int cursor = bufferPosition; int length = Serialization.readInt(buffer, cursor); cursor += Serialization.SIZE_OF_INT; if (buffer.length < cursor + length) { throw new IllegalArgumentException(String.format( "Not enough bytes to read from. String length = %d, starting position = %d, buffer length = %d", length, cursor, buffer.length)); } byte[] bytes = Serialization.subArray(buffer, cursor, cursor + length); try { return new String(bytes, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new AssertionError("UTF-8 is not supported (the JVM is not to spec!)", e); } } StringAdapter(); @Override byte[] serialize(String value); @Override String deserialize(byte[] buffer, int bufferPosition); @Override int getSerializedSize(String value);  }
@Test public void testDecodeEmpty() { byte[] buffer = {0, 0, 0, 0}; String expected = ""; assertEquals(expected, adapter.deserialize(buffer, 0)); }
@Test public void testDecode() { byte[] buffer = {0, 0, 0, 3, 'b', 'a', 'z'}; String expected = "baz"; assertEquals(expected, adapter.deserialize(buffer, 0)); }