private String responseBody(CloseableHttpResponse response) throws IOException { try (InputStream is = response.getEntity() == null ? new NullInputStream(0) : response.getEntity().getContent()) { return IOUtils.toString(is, StandardCharsets.UTF_8); } }
/** * Read some bytes into the specified array. * * @param bytes The byte array to read into * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to {@code false}. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to {@code true}. * @throws IOException if trying to read past the end of file. */ @Override public int read(final byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
/** * Read a byte. * * @return Either The byte value returned by <code>processByte()</code> * or <code>-1</code> if the end of file has been reached and * <code>throwEofException</code> is set to {@code false}. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to {@code true}. * @throws IOException if trying to read past the end of file. */ @Override public int read() throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position++; return processByte(); }
/** * Read the specified number bytes into an array. * * @param bytes The byte array to read into. * @param offset The offset to start reading bytes into. * @param length The number of bytes to read. * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to {@code false}. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to {@code true}. * @throws IOException if trying to read past the end of file. */ @Override public int read(final byte[] bytes, final int offset, final int length) throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position += length; int returnLength = length; if (position > size) { returnLength = length - (int)(position - size); position = size; } processBytes(bytes, offset, returnLength); return returnLength; }
@Override public synchronized int read(final byte[] chunk, final Long offset) throws IOException { final RandomAccessFile file = random(); if(offset < file.length()) { file.seek(offset); if(chunk.length + offset > file.length()) { return file.read(chunk, 0, (int) (file.length() - offset)); } else { return file.read(chunk, 0, chunk.length); } } else { final NullInputStream nullStream = new NullInputStream(length); if(nullStream.available() > 0) { nullStream.skip(offset); return nullStream.read(chunk, 0, chunk.length); } else { return IOUtils.EOF; } } }
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final OutputStream nos = new NullOutputStream(); final CountingOutputStream cos = new CountingOutputStream(nos); // Test integer methods IOUtils.copyLarge(mock, cos); try { cos.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cos.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(mock, cos); assertEquals("getByteCount()", size, cos.getByteCount()); assertEquals("resetByteCount()", size, cos.resetByteCount()); }
/** * Skip a specified number of bytes. * * @param numberOfBytes The number of bytes to skip. * @return The number of bytes skipped or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to {@code false}. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to {@code true}. * @throws IOException if trying to read past the end of file. */ @Override public long skip(final long numberOfBytes) throws IOException { if (eof) { throw new IOException("Skip after end of file"); } if (position == size) { return doEndOfFile(); } position += numberOfBytes; long returnLength = numberOfBytes; if (position > size) { returnLength = numberOfBytes - (position - size); position = size; } return returnLength; }
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final CountingInputStream cis = new CountingInputStream(mock); final OutputStream out = new NullOutputStream(); // Test integer methods IOUtils.copyLarge(cis, out); try { cis.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cis.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(cis, out); assertEquals("getByteCount()", size, cis.getByteCount()); assertEquals("resetByteCount()", size, cis.resetByteCount()); }
/** * Read the specified number bytes into an array. * * @param bytes The byte array to read into. * @param offset The offset to start reading bytes into. * @param length The number of bytes to read. * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read(byte[] bytes, int offset, int length) throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position += length; int returnLength = length; if (position > size) { returnLength = length - (int)(position - size); position = size; } processBytes(bytes, offset, returnLength); return returnLength; }
/** * Skip a specified number of bytes. * * @param numberOfBytes The number of bytes to skip. * @return The number of bytes skipped or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public long skip(long numberOfBytes) throws IOException { if (eof) { throw new IOException("Skip after end of file"); } if (position == size) { return doEndOfFile(); } position += numberOfBytes; long returnLength = numberOfBytes; if (position > size) { returnLength = numberOfBytes - (position - size); position = size; } return returnLength; }
private String responseBody(CloseableHttpResponse response) throws IOException { try (InputStream is = response.getEntity() == null ? new NullInputStream(0) : response.getEntity().getContent()) { return IOUtils.toString(is, StandardCharsets.UTF_8); } } }
/** * Read a byte. * * @return Either The byte value returned by <code>processByte()</code> * or <code>-1</code> if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read() throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position++; return processByte(); }
/** * Read the specified number bytes into an array. * * @param bytes The byte array to read into. * @param offset The offset to start reading bytes into. * @param length The number of bytes to read. * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read(byte[] bytes, int offset, int length) throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position += length; int returnLength = length; if (position > size) { returnLength = length - (int)(position - size); position = size; } processBytes(bytes, offset, returnLength); return returnLength; }
/** * Read some bytes into the specified array. * * @param bytes The byte array to read into * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read(byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
/** * Skip a specified number of bytes. * * @param numberOfBytes The number of bytes to skip. * @return The number of bytes skipped or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public long skip(long numberOfBytes) throws IOException { if (eof) { throw new IOException("Skip after end of file"); } if (position == size) { return doEndOfFile(); } position += numberOfBytes; long returnLength = numberOfBytes; if (position > size) { returnLength = numberOfBytes - (position - size); position = size; } return returnLength; }
@Test public void testCopy_inputStreamToOutputStream_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final InputStream in = new NullInputStream(size); final OutputStream out = new NullOutputStream(); // Test copy() method assertEquals(-1, IOUtils.copy(in, out)); // reset the input in.close(); // Test copyLarge() method assertEquals("copyLarge()", size, IOUtils.copyLarge(in, out)); }
/** * Read a byte. * * @return Either The byte value returned by <code>processByte()</code> * or <code>-1</code> if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read() throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position++; return processByte(); }
/** * Read the specified number bytes into an array. * * @param bytes The byte array to read into. * @param offset The offset to start reading bytes into. * @param length The number of bytes to read. * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read(byte[] bytes, int offset, int length) throws IOException { if (eof) { throw new IOException("Read after end of file"); } if (position == size) { return doEndOfFile(); } position += length; int returnLength = length; if (position > size) { returnLength = length - (int)(position - size); position = size; } processBytes(bytes, offset, returnLength); return returnLength; }
/** * Read some bytes into the specified array. * * @param bytes The byte array to read into * @return The number of bytes read or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public int read(byte[] bytes) throws IOException { return read(bytes, 0, bytes.length); }
/** * Skip a specified number of bytes. * * @param numberOfBytes The number of bytes to skip. * @return The number of bytes skipped or <code>-1</code> * if the end of file has been reached and * <code>throwEofException</code> is set to <code>false</code>. * @throws EOFException if the end of file is reached and * <code>throwEofException</code> is set to <code>true</code>. * @throws IOException if trying to read past the end of file. */ public long skip(long numberOfBytes) throws IOException { if (eof) { throw new IOException("Skip after end of file"); } if (position == size) { return doEndOfFile(); } position += numberOfBytes; long returnLength = numberOfBytes; if (position > size) { returnLength = numberOfBytes - (position - size); position = size; } return returnLength; }