@Override public synchronized int read() throws IOException { if(getRemainingBytes()==0) return handleStreamOutOfBound(); int i = in.read(); totalRead++; return i; }
@Override public int read(byte b[]) throws IOException { return read(b, 0, b.length); }
/** * Performs some tests that are common to {@link #testBoundedStreamWithException()} and * {@link #testBoundedStreamWithoutException()}. * * @param bin the BoundedInputStream to prepare * @throws IOException should not happen */ private void prepareBoundedStream(BoundedInputStream bin) throws IOException { assert 0 == bin.getProcessedBytes(); assert 4 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read()!=-1; assert 1 == bin.getProcessedBytes(); assert 3 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read(new byte[1])!=-1; assert 2 == bin.getProcessedBytes(); assert 2 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.read(new byte[1], 0, 1)!=-1; assert 3 == bin.getProcessedBytes(); assert 1 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); assert bin.skip(1)!=-1; assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); }
@Override public InputStream getEntryInputStream(ArchiveEntry entry, ArchiveEntryIterator entryIterator) throws IOException, UnsupportedFileOperationException { InputStream in = getInputStream(); ArchiveEntryIterator iterator = new ArArchiveEntryIterator(in); ArchiveEntry currentEntry; while((currentEntry = iterator.nextEntry())!=null) { if(currentEntry.getName().equals(entry.getName())) { LOGGER.trace("found entry {}", entry.getName()); return new BoundedInputStream(in, entry.getSize(), false); } } // Entry not found, should not normally happen LOGGER.info("Warning: entry not found, throwing IOException"); throw new IOException(); } }
@Override public synchronized int available() throws IOException { return Math.min(in.available(), (int)getRemainingBytes()); }
/** * Tests a <code>BoundedInputStream</code> operating in bounded mode and returning <code>-1/</code>. * * @throws IOException should not happen */ @Test public void testBoundedStreamWithoutException() throws IOException { BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, false); prepareBoundedStream(bin); assert -1 == bin.read(); assert -1 == bin.read(new byte[1]); assert -1 == bin.read(new byte[1], 0, 1); assert -1 == bin.skip(1); assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); // Attempt to read a chunk larger than the remaining bytes and assert that it does not return -1 bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, false); assert bin.read(new byte[6])!=-1; }
/** * Creates a regular file and fills it with <code>length</code> random bytes, overwriting the file if it exists, * and returns the md5 checksum of the random data that was copied. * <p> * Before returning, this method asserts that the file {@link AbstractFile#exists() exists} and that its * {@link AbstractFile#getSize() size} matches the specified length argument. * </p> * * @param file the file to create or overwrite * @param length the number of random bytes to fill the file with * @return the md5 checksum of the data written to the file * @throws IOException if the file already exists or if an error occurred while writing to it * @throws NoSuchAlgorithmException should not happen */ protected String createFile(AbstractFile file, long length) throws IOException, NoSuchAlgorithmException { ChecksumInputStream md5In = new ChecksumInputStream(new BoundedInputStream(new RandomGeneratorInputStream(), length, false), MessageDigest.getInstance("md5")); file.copyStream(md5In, false, length); assert file.exists(); assert length == file.getSize(); return md5In.getChecksumString(); }
BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), -1, false); assert 0 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert bin.read()!=-1; assert 1 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert bin.read(new byte[1])!=-1; assert 2 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); assert bin.read(new byte[1], 0, 1)!=-1; assert 3 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes(); assert -1 == bin.getAllowedBytes(); while(bin.read()!=-1) totalRead ++;
@Override public synchronized int read(byte b[], int off, int len) throws IOException { int canRead = (int)Math.min(getRemainingBytes(), len); if(canRead==0) return handleStreamOutOfBound(); int nbRead = in.read(b, off, canRead); if(nbRead>0) totalRead += nbRead; return nbRead; }
BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, true); prepareBoundedStream(bin); try { bin.read(); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } try { bin.read(new byte[1]); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } try { bin.read(new byte[1], 0, 1); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } try { bin.skip(1); } catch(StreamOutOfBoundException e) { exceptionThrown = true; } assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes(); bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, true); assert bin.read(new byte[6])!=-1; assert 4 == bin.getProcessedBytes(); assert 0 == bin.getRemainingBytes(); assert 4 == bin.getAllowedBytes();
@Override public synchronized long skip(long n) throws IOException { int canSkip = (int)Math.min(getRemainingBytes(), n); if(canSkip==0) return handleStreamOutOfBound(); long nbSkipped = in.skip(canSkip); if(nbSkipped>0) totalRead += nbSkipped; return nbSkipped; }