BitWorks { public static byte[] convertStringsToByteArray(final String... values) throws IOException { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); try { for (String value : values) { final byte[] valueAsBytes = value.getBytes(Charset.forName("UTF-8")); stream.write(ByteBuffer.allocate(4).putInt(valueAsBytes.length).array()); stream.write(valueAsBytes); } return stream.toByteArray(); } finally { stream.close(); } }  static byte[] convertUUIDToByteArray(final UUID uuid); static UUID convertByteArrayToUUID(final byte[] bytes); static byte[] convertStringsToByteArray(final String... values);  }
@Test public void convertStringsToByteArray() throws IOException { String x = "something"; String y = "anything"; byte[] bytes = BitWorks.convertStringsToByteArray(x, y); ByteBuffer buffer = ByteBuffer.wrap(bytes); Assert.assertEquals(x.length(), buffer.getInt()); byte[] xBytes = new byte[x.length()]; buffer.get(xBytes); Assert.assertEquals(x, new String(xBytes)); Assert.assertEquals(y.length(), buffer.getInt()); byte[] yBytes = new byte[y.length()]; buffer.get(yBytes); Assert.assertEquals(y, new String(yBytes)); }