DataInputStreamUtils { public static byte[] decodeByteArray(DataInputStream in) throws IOException { int len = in.readInt(); if (len == -1) { return null; } if (len == 0) { return new byte[0]; } byte[] data = new byte[len]; int read = in.read(data); if (read != len) { throw new IOException("Unexpected read length. Expected " + len + " found " + read); } return 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 testDecodeEndingInZeroLengthByteArray() throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(byteArrayOutputStream); DataOutputStreamUtils.encodeByteArray(out, new byte[0]); DataInputStream in = new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); byte[] arr = DataInputStreamUtils.decodeByteArray(in); assertNotNull(arr); assertEquals(0, arr.length); }