Source:
public void testDeserializeString() { String randomString = getRandomString(10); ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); String outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); assertEquals("Input and output strings don't match", randomString, outputString); randomString = getRandomString(10) + "Ò"; outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); randomString = randomString.substring(0, randomString.length() - 1) + "?"; assertEquals("Input and output strings don't match", randomString, outputString); randomString = ""; outputBuffer = ByteBuffer.allocate(4); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); assertTrue("Output string \"" + outputString + "\" expected to be empty", (outputString.equals(""))); randomString = getRandomString(10); outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); outputBuffer.putInt(12); outputBuffer.put(randomString.getBytes()); outputBuffer.flip(); try { outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); Assert.fail("Deserialization should have failed " + randomString); } catch (RuntimeException e) { } }

Target:
public void testDeserializeString() { String randomString = getRandomString(10); ByteBuffer outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); String outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); assertEquals("Input and output strings don't match", randomString, outputString); randomString = getRandomString(10) + "Ò"; outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); randomString = randomString.substring(0, randomString.length() - 1) + "?"; assertEquals("Input and output strings don't match", randomString, outputString); randomString = ""; outputBuffer = ByteBuffer.allocate(4); Utils.serializeString(outputBuffer, randomString, StandardCharsets.US_ASCII); outputBuffer.flip(); outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); assertEquals("Output string \"" + outputString + "\" expected to be empty", outputString, ""); randomString = getRandomString(10); outputBuffer = ByteBuffer.allocate(4 + randomString.getBytes().length); outputBuffer.putInt(12); outputBuffer.put(randomString.getBytes()); outputBuffer.flip(); try { outputString = Utils.deserializeString(outputBuffer, StandardCharsets.US_ASCII); Assert.fail("Deserialization should have failed " + randomString); } catch (RuntimeException e) { } }