StringArrayAdapter extends TypeAdapter<String[]> { @Override public byte[] serialize(String[] data) { if (data.length == 0) { return new byte[Serialization.SIZE_OF_INT]; } byte[] buf = new byte[Serialization.sizeOfStringArray(data)]; int pos = 0; Serialization.put(buf, Serialization.toByteArray(data.length), pos); pos += Serialization.SIZE_OF_INT; for (String string : data) { byte[] arr = Serialization.encode(string); Serialization.put(buf, arr, pos); pos += arr.length; } return buf; } StringArrayAdapter(); @Override byte[] serialize(String[] data); @Override String[] deserialize(byte[] array, int pos); @Override int getSerializedSize(String[] value);  }
@Test public void testEncodeEmpty() { String[] array = {}; byte[] expected = {0, 0, 0, 0}; assertArrayEquals(expected, adapter.serialize(array)); }
@Test public void testEncode() { String[] array = {"foo", "bar"}; byte[] expected = {0, 0, 0, 2, 0, 0, 0, 3, 'f', 'o', 'o', 0, 0, 0, 3, 'b', 'a', 'r'}; assertArrayEquals(expected, adapter.serialize(array)); }