/** * Read a byte array from the input stream * * @param buffer where to put bytes read * @param offset offset to start putting bytes into buffer * @param length number of bytes to read * @throws RuntimeEOFException if fewer than length bytes to read */ public void readBytes(final byte[] buffer, final int offset, final int length) { int totalNumRead = 0; do { final int numRead = readBytesOrFewer(buffer, offset + totalNumRead, length - totalNumRead); if (numRead < 0) { String msg = String.format("Premature EOF. Expected %d but only received %d", length, totalNumRead); throw new RuntimeEOFException(constructErrorMessage(msg)); } else { totalNumRead += numRead; } } while (totalNumRead < length); }
/** * Read a byte array from the input stream * * @param buffer where to put bytes read * @param offset offset to start putting bytes into buffer * @param length number of bytes to read * @throws RuntimeEOFException if fewer than length bytes to read */ public void readBytes(final byte[] buffer, final int offset, final int length) { int totalNumRead = 0; do { final int numRead = readBytesOrFewer(buffer, offset + totalNumRead, length - totalNumRead); if (numRead < 0) { String msg = String.format("Premature EOF. Expected %d but only received %d", length, totalNumRead); throw new RuntimeEOFException(constructErrorMessage(msg)); } else { totalNumRead += numRead; } } while (totalNumRead < length); }
/** * Read a byte array from the input stream * * @param buffer where to put bytes read * @param offset offset to start putting bytes into buffer * @param length number of bytes to read * @throws RuntimeEOFException if fewer than length bytes to read */ public void readBytes(final byte[] buffer, final int offset, final int length) { int totalNumRead = 0; do { final int numRead = readBytesOrFewer(buffer, offset + totalNumRead, length - totalNumRead); if (numRead < 0) { String msg = String.format("Premature EOF. Expected %d but only received %d", length, totalNumRead); throw new RuntimeEOFException(constructErrorMessage(msg)); } else { totalNumRead += numRead; } } while (totalNumRead < length); }
@Test public void testReadBytesOrFewerNoneAvailable() { final byte[] value = new byte[0]; final ByteArrayInputStream instream = new ByteArrayInputStream(value); final BinaryCodec readCodec = new BinaryCodec(instream); Assert.assertEquals(readCodec.readBytesOrFewer(value, 0, 0), 0); } }