/** * Reads bytes from an input stream. * This implementation guarantees that it will read as many bytes * as possible before giving up; this may not always be the case for * subclasses of {@link InputStream}. * * @param input where to read input from * @param buffer destination * @return actual length read; may be less than requested if EOF was reached * @throws IOException if a read error occurs * @since 2.2 */ public static int read(final InputStream input, final byte[] buffer) throws IOException { return read(input, buffer, 0, buffer.length); }
@Test public void testRead_ReadableByteChannel() throws Exception { final ByteBuffer buffer = ByteBuffer.allocate(FILE_SIZE); final FileInputStream fileInputStream = new FileInputStream(m_testFile); final FileChannel input = fileInputStream.getChannel(); try { assertEquals(FILE_SIZE, IOUtils.read(input, buffer)); assertEquals(0, IOUtils.read(input, buffer)); assertEquals(0, buffer.remaining()); assertEquals(0, input.read(buffer)); buffer.clear(); try { IOUtils.readFully(input, buffer); fail("Should have failed with EOFxception"); } catch (final EOFException expected) { // expected } } finally { IOUtils.closeQuietly(input, fileInputStream); }}
/** * Reads characters from an input character stream. * This implementation guarantees that it will read as many characters * as possible before giving up; this may not always be the case for * subclasses of {@link Reader}. * * @param input where to read input from * @param buffer destination * @return actual length read; may be less than requested if EOF was reached * @throws IOException if a read error occurs * @since 2.2 */ public static int read(final Reader input, final char[] buffer) throws IOException { return read(input, buffer, 0, buffer.length); }
/** * Reads the requested number of bytes or fail if there are not enough left. * <p> * This allows for the possibility that {@link InputStream#read(byte[], int, int)} may * not read as many bytes as requested (most likely because of reaching EOF). * * @param input where to read input from * @param buffer destination * @param offset initial offset into buffer * @param length length to read, must be >= 0 * @throws IOException if there is a problem reading the file * @throws IllegalArgumentException if length is negative * @throws EOFException if the number of bytes read was incorrect * @since 2.2 */ public static void readFully(final InputStream input, final byte[] buffer, final int offset, final int length) throws IOException { final int actual = read(input, buffer, offset, length); if (actual != length) { throw new EOFException("Length to read: " + length + " actual: " + actual); } }
/** * Reads the requested number of characters or fail if there are not enough left. * <p> * This allows for the possibility that {@link Reader#read(char[], int, int)} may * not read as many characters as requested (most likely because of reaching EOF). * * @param input where to read input from * @param buffer destination * @param offset initial offset into buffer * @param length length to read, must be >= 0 * @throws IOException if there is a problem reading the file * @throws IllegalArgumentException if length is negative * @throws EOFException if the number of characters read was incorrect * @since 2.2 */ public static void readFully(final Reader input, final char[] buffer, final int offset, final int length) throws IOException { final int actual = read(input, buffer, offset, length); if (actual != length) { throw new EOFException("Length to read: " + length + " actual: " + actual); } }
/** * Reads the requested number of bytes or fail if there are not enough left. * <p> * This allows for the possibility that {@link ReadableByteChannel#read(ByteBuffer)} may * not read as many bytes as requested (most likely because of reaching EOF). * * @param input the byte channel to read * @param buffer byte buffer destination * @throws IOException if there is a problem reading the file * @throws EOFException if the number of bytes read was incorrect * @since 2.5 */ public static void readFully(final ReadableByteChannel input, final ByteBuffer buffer) throws IOException { final int expected = buffer.remaining(); final int actual = read(input, buffer); if (actual != expected) { throw new EOFException("Length to read: " + expected + " actual: " + actual); } }
private byte[] readBuffer() throws IOException { stream = new BufferedInputStream(Files.newInputStream(filePath), BYTES_TO_DECODE * 2); stream.mark(BYTES_TO_DECODE); byte[] buf = new byte[BYTES_TO_DECODE]; int read = IOUtils.read(stream, buf, 0, BYTES_TO_DECODE); stream.reset(); stream.mark(-1); return Arrays.copyOf(buf, read); }
@Override public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException { String redirectHeader = httpRequest.getHeader("X-REDIRECT"); if (redirectHeader != null && !redirectAlreadyPerformed) { redirectAlreadyPerformed = true; httpResponse.setStatus(Integer.valueOf(redirectHeader)); httpResponse.setContentLength(0); httpResponse.setHeader(LOCATION.toString(), getTargetUrl()); } else { receivedContentType = request.getContentType(); httpResponse.setStatus(200); int len = request.getContentLength(); httpResponse.setContentLength(len); if (len > 0) { byte[] buffer = new byte[len]; IOUtils.read(request.getInputStream(), buffer); httpResponse.getOutputStream().write(buffer); } } httpResponse.getOutputStream().flush(); httpResponse.getOutputStream().close(); } };
boolean fill(Reader reader) throws IOException { dataLength = IOUtils.read(reader, data); return dataLength < data.length; }
private void loadRam(int[] ram, InputStream is, long length) throws IOException { byte[] buffer = new byte[ram.length]; IOUtils.read(is, buffer, 0, Math.min((int) length, ram.length)); for (int i = 0; i < ram.length; i++) { ram[i] = buffer[i] & 0xff; } }
boolean read(InputStream is) throws IOException { bytesReadLast = IOUtils.read(is, bytes); if (DBFReader.STRICT && bytesReadLast != bytes.length) { throw new IOException("Truncated record, only read "+bytesReadLast+ " bytes, but should have read: "+bytes.length); } return bytesReadLast > 0; }
private void loadClock(long[] clockData, InputStream is) throws IOException { byte[] byteBuff = new byte[4 * clockData.length]; IOUtils.read(is, byteBuff); ByteBuffer buff = ByteBuffer.wrap(byteBuff); buff.order(ByteOrder.LITTLE_ENDIAN); int i = 0; while (buff.hasRemaining()) { clockData[i++] = buff.getInt() & 0xffffffff; } }
public static byte[] extractHead(BufferedInputStream bufferedInputStream, int maxSize) throws IOException { bufferedInputStream.mark(maxSize); byte[] initialBytes = new byte[maxSize]; int read = IOUtils.read(bufferedInputStream, initialBytes); if (read != maxSize) { byte[] trimmed = new byte[read]; System.arraycopy(initialBytes, 0, trimmed, 0, read); initialBytes = trimmed; } bufferedInputStream.reset(); return initialBytes; } }
IOUtils.read(in, magic); if (!Arrays.equals(magic, FILE_MAGIC)) { throw new IOException("Invalid file (bad magic)");
boolean read(InputStream is) throws IOException { bytesReadLast = IOUtils.read(is, bytes); if (DBFReader.STRICT && bytesReadLast != bytes.length) { throw new IOException("Truncated record, only read "+bytesReadLast+ " bytes, but should have read: "+bytes.length); } return bytesReadLast > 0; }
boolean read(InputStream is) throws IOException { bytesReadLast = IOUtils.read(is, bytes); if (DBFReader.STRICT && bytesReadLast != bytes.length) { throw new IOException("Truncated record, only read "+bytesReadLast+ " bytes, but should have read: "+bytes.length); } return bytesReadLast > 0; }
private byte[] readBuffer() throws IOException { stream = new BufferedInputStream(Files.newInputStream(filePath), BYTES_TO_DECODE * 2); stream.mark(BYTES_TO_DECODE); byte[] buf = new byte[BYTES_TO_DECODE]; int read = IOUtils.read(stream, buf, 0, BYTES_TO_DECODE); stream.reset(); stream.mark(-1); return Arrays.copyOf(buf, read); }
public static String loadFileContentByClassPath(String fileName) throws IOException{ Resource[] resources = getResources(fileName); if (resources.length == 0) { throw new FileNotFoundException(fileName); } InputStream io = resources[0].getInputStream(); byte[] contents = new byte[102400]; IOUtils.read(io, contents); IOUtils.closeQuietly(io); return new String(contents); }
private byte[] readSignableBytesAgainForLaterValidation(final CountingInputStream bufferedInputStream) throws IOException { final int position = (int)bufferedInputStream.getCount(); bufferedInputStream.reset(); final byte[] bytes = new byte[position]; IOUtils.read(bufferedInputStream, bytes, 0, position); return bytes; } }
@Test public void readBinary() throws Exception { testHarness.createBinaryFile(); Message response = readPath(BINARY_FILE_NAME, false); assertThat(response.getPayload().getDataType().getMediaType().getPrimaryType(), is(MediaType.BINARY.getPrimaryType())); assertThat(response.getPayload().getDataType().getMediaType().getSubType(), is(MediaType.BINARY.getSubType())); AbstractFileInputStream payload = (AbstractFileInputStream) response.getPayload().getValue(); assertThat(payload.isLocked(), is(false)); byte[] readContent = new byte[new Long(HELLO_WORLD.length()).intValue()]; org.apache.commons.io.IOUtils.read(payload, readContent); assertThat(new String(readContent), is(HELLO_WORLD)); }