@Override public Manifest getManifest() throws IOException { Manifest manifest = (this.manifest == null ? null : this.manifest.get()); if (manifest == null) { if (this.type == JarFileType.NESTED_DIRECTORY) { manifest = new JarFile(this.getRootJarFile()).getManifest(); } else { InputStream inputStream = getInputStream(MANIFEST_NAME, ResourceAccess.ONCE); if (inputStream == null) { return null; } try { manifest = new Manifest(inputStream); } finally { inputStream.close(); } } this.manifest = new SoftReference<Manifest>(manifest); } return manifest; }
void setupEntryCertificates(JarEntry entry) { // Fallback to JarInputStream to obtain certificates, not fast but hopefully not // happening that often. try { JarInputStream inputStream = new JarInputStream( getData().getInputStream(ResourceAccess.ONCE)); try { java.util.jar.JarEntry certEntry = inputStream.getNextJarEntry(); while (certEntry != null) { inputStream.closeEntry(); if (entry.getName().equals(certEntry.getName())) { setCertificates(entry, certEntry); } setCertificates(getJarEntry(certEntry.getName()), certEntry); certEntry = inputStream.getNextJarEntry(); } } finally { inputStream.close(); } } catch (IOException ex) { throw new IllegalStateException(ex); } }
private void unpack(JarEntry entry, File file) throws IOException { InputStream inputStream = this.jarFile.getInputStream(entry, ResourceAccess.ONCE); try { OutputStream outputStream = new FileOutputStream(file); try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); } finally { outputStream.close(); } } finally { inputStream.close(); } }
@Test public void concurrentReads() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(20); List<Future<Boolean>> results = new ArrayList<Future<Boolean>>(); for (int i = 0; i < 100; i++) { results.add(executorService.submit(new Callable<Boolean>() { @Override public Boolean call() throws Exception { InputStream subsectionInputStream = RandomAccessDataFileTests.this.file .getSubsection(0, 256) .getInputStream(ResourceAccess.PER_READ); byte[] b = new byte[256]; subsectionInputStream.read(b); return Arrays.equals(b, BYTES); } })); } for (Future<Boolean> future : results) { assertThat(future.get()).isTrue(); } }
private List<JarEntryData> loadJarEntries(CentralDirectoryEndRecord endRecord) throws IOException { RandomAccessData centralDirectory = endRecord.getCentralDirectory(this.data); int numberOfRecords = endRecord.getNumberOfRecords(); List<JarEntryData> entries = new ArrayList<JarEntryData>(numberOfRecords); InputStream inputStream = centralDirectory.getInputStream(ResourceAccess.ONCE); try { JarEntryData entry = JarEntryData.fromInputStream(this, inputStream); while (entry != null) { entries.add(entry); processEntry(entry); entry = JarEntryData.fromInputStream(this, inputStream); } } finally { inputStream.close(); } return entries; }
void setupEntryCertificates() { // Fallback to JarInputStream to obtain certificates, not fast but hopefully not // happening that often. try { JarInputStream inputStream = new JarInputStream( getData().getInputStream(ResourceAccess.ONCE)); try { java.util.jar.JarEntry entry = inputStream.getNextJarEntry(); while (entry != null) { inputStream.closeEntry(); JarEntry jarEntry = getJarEntry(entry.getName()); if (jarEntry != null) { jarEntry.setupCertificates(entry); } entry = inputStream.getNextJarEntry(); } } finally { inputStream.close(); } } catch (IOException ex) { throw new IllegalStateException(ex); } }
private void unpack(JarEntryData data, File file) throws IOException { InputStream inputStream = data.getData().getInputStream(ResourceAccess.ONCE); try { OutputStream outputStream = new FileOutputStream(file); try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); } finally { outputStream.close(); } } finally { inputStream.close(); } }
@Test public void concurrentReads() throws Exception { ExecutorService executorService = Executors.newFixedThreadPool(20); List<Future<Boolean>> results = new ArrayList<Future<Boolean>>(); for (int i = 0; i < 100; i++) { results.add(executorService.submit(new Callable<Boolean>() { @Override public Boolean call() throws Exception { InputStream subsectionInputStream = RandomAccessDataFileTests.this.file .getSubsection(0, 256) .getInputStream(ResourceAccess.PER_READ); byte[] b = new byte[256]; subsectionInputStream.read(b); return Arrays.equals(b, BYTES); } })); } for (Future<Boolean> future : results) { assertThat(future.get(), equalTo(true)); } }
public static byte[] get(RandomAccessData data) throws IOException { InputStream inputStream = data.getInputStream(ResourceAccess.ONCE); try { return get(inputStream, data.getSize()); } finally { inputStream.close(); } }
public InputStream getInputStream(ZipEntry ze, ResourceAccess access) throws IOException { if (ze instanceof JarEntry) { return this.entries.getInputStream((JarEntry) ze, access); } return getInputStream(ze == null ? null : ze.getName(), access); }
public InputStream getInputStream(FileHeader entry, ResourceAccess access) throws IOException { if (entry == null) { return null; } InputStream inputStream = getEntryData(entry).getInputStream(access); if (entry.getMethod() == ZipEntry.DEFLATED) { inputStream = new ZipInflaterInputStream(inputStream, (int) entry.getSize()); } return inputStream; }
@Before public void setup() throws Exception { this.tempFile = this.temporaryFolder.newFile(); FileOutputStream outputStream = new FileOutputStream(this.tempFile); outputStream.write(BYTES); outputStream.close(); this.file = new RandomAccessDataFile(this.tempFile); this.inputStream = this.file.getInputStream(ResourceAccess.PER_READ); }
@Test public void inputStreamReadPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); assertThat(inputStream.read()).isEqualTo(1); assertThat(inputStream.read()).isEqualTo(2); assertThat(inputStream.read()).isEqualTo(-1); }
@Test public void inputStreamReadBytesPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); byte[] b = new byte[3]; int amountRead = inputStream.read(b); assertThat(b).isEqualTo(new byte[] { 1, 2, 0 }); assertThat(amountRead).isEqualTo(2); }
@Test public void inputStreamSkipPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); assertThat(inputStream.skip(3)).isEqualTo(2L); assertThat(inputStream.read()).isEqualTo(-1); }
@Test public void close() throws Exception { this.file.getInputStream(ResourceAccess.PER_READ).read(); this.file.close(); Field filePoolField = RandomAccessDataFile.class.getDeclaredField("filePool"); filePoolField.setAccessible(true); Object filePool = filePoolField.get(this.file); Field filesField = filePool.getClass().getDeclaredField("files"); filesField.setAccessible(true); Queue<?> queue = (Queue<?>) filesField.get(filePool); assertThat(queue.size()).isEqualTo(0); }
@Test public void testGetInputStream() throws Exception { byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; RandomAccessData data = new ByteArrayRandomAccessData(bytes); InputStream inputStream = data.getInputStream(ResourceAccess.PER_READ); assertThat(FileCopyUtils.copyToByteArray(inputStream)).isEqualTo(bytes); assertThat(data.getSize()).isEqualTo(bytes.length); }
@Test public void testGetSubsection() throws Exception { byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; RandomAccessData data = new ByteArrayRandomAccessData(bytes); data = data.getSubsection(1, 4).getSubsection(1, 2); InputStream inputStream = data.getInputStream(ResourceAccess.PER_READ); assertThat(FileCopyUtils.copyToByteArray(inputStream)) .isEqualTo(new byte[] { 2, 3 }); assertThat(data.getSize()).isEqualTo(2L); }
InputStream getInputStream() throws IOException { InputStream inputStream = getData().getInputStream(ResourceAccess.PER_READ); if (getMethod() == ZipEntry.DEFLATED) { inputStream = new ZipInflaterInputStream(inputStream, getSize()); } return inputStream; }
@Test public void inputStreamReadPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); assertThat(inputStream.read(), equalTo(1)); assertThat(inputStream.read(), equalTo(2)); assertThat(inputStream.read(), equalTo(-1)); }
@Test public void inputStreamReadBytesPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); byte[] b = new byte[3]; int amountRead = inputStream.read(b); assertThat(b, equalTo(new byte[] { 1, 2, 0 })); assertThat(amountRead, equalTo(2)); }
@Test public void inputStreamSkipPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); assertThat(inputStream.skip(3), equalTo(2L)); assertThat(inputStream.read(), equalTo(-1)); }
@Test public void close() throws Exception { this.file.getInputStream(ResourceAccess.PER_READ).read(); this.file.close(); Field filePoolField = RandomAccessDataFile.class.getDeclaredField("filePool"); filePoolField.setAccessible(true); Object filePool = filePoolField.get(this.file); Field filesField = filePool.getClass().getDeclaredField("files"); filesField.setAccessible(true); Queue<?> queue = (Queue<?>) filesField.get(filePool); assertThat(queue.size(), equalTo(0)); }
@Test public void testGetInputStream() throws Exception { byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; RandomAccessData data = new ByteArrayRandomAccessData(bytes); assertThat(FileCopyUtils.copyToByteArray( data.getInputStream(ResourceAccess.PER_READ)), equalTo(bytes)); assertThat(data.getSize(), equalTo((long) bytes.length)); }