DataInputStreamUtils { public static ByteSequence decodeByteSequence(DataInputStream in) throws IOException { int length = in.readInt(); if (length == -1) { return null; } if (length == 0) { return new ArrayByteSequence(new byte[0]); } byte[] data = new byte[length]; int read = in.read(data); if (read != length) { throw new IOException("Expected " + length + " bytes, found " + read + " bytes"); } return new ArrayByteSequence(data); }  static Text decodeText(DataInputStream in); static String decodeString(DataInputStream in); static byte[] decodeByteArray(DataInputStream in); static int[] decodeIntArray(DataInputStream in); static ByteArrayWrapper decodeByteArrayWrapper(DataInputStream in); static Set<String> decodeSetOfStrings(DataInputStream in); static ElementType decodeElementType(DataInputStream in); static ByteSequence decodeByteSequence(DataInputStream in); static Direction decodeDirection(DataInputStream in); static Value decodeValue(DataInputStream in); static Long decodeLong(DataInputStream in);  }
@Test public void testDecodeEndingInZeroLengthByteSequence() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteArrayOutputStream); DataOutputStreamUtils.encodeByteSequence(out, new ArrayByteSequence(new byte[0])); DataInputStream in = new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); ByteSequence arr = DataInputStreamUtils.decodeByteSequence(in); assertNotNull(arr); assertEquals(0, arr.length()); }