@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(); } }
/** * 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), 4, true); prepareBoundedStream(bin); bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), 4, true); assert bin.read(new byte[6])!=-1;
BoundedInputStream bin = new BoundedInputStream(new ByteArrayInputStream(TEST_BYTES), -1, false); assert 0 == bin.getProcessedBytes(); assert Long.MAX_VALUE == bin.getRemainingBytes();