/** * Performs copy operation between currentNodeStream and outputStream using buffer length. * * @throws IOException */ private void doCopy() throws IOException { int copied = IOUtil.copy(currentNodeStream, outputStream, BUFFER_LENGTH); if (copied == -1) { currentNodeStream.close(); currentNodeStream = null; endAsset(); } }
/** * {@inheritDoc} */ @Override public void writeTo(final OutputStream outputStream, final Formatter formatter) throws IllegalArgumentException { try { IOUtil.bufferedWriteWithFlush(outputStream, toString(formatter).getBytes()); } catch (IOException ioe) { throw new IllegalArgumentException("Could not write Archive contents to specified OutputStream", ioe); } }
/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param stream * @param task */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task) { closeOnComplete(stream, task, DEFAULT_ERROR_HANDLER); }
/** * SHRINKWRAP-237: Reading the same resource multiple times cause IOException */ @Test public void shouldBeAbleToLoadAResourceFromArchiveMultipleTimes() throws Exception { String resourceName = getResourceNameOfClass(applicationClassLoaderClass); // Load the class as a resource URL resource = shrinkWrapClassLoader.getResource(resourceName); // Assertions Assert.assertNotNull(resource); // Read the stream until EOF IOUtil.copyWithClose(resource.openStream(), new ByteArrayOutputStream()); // Load the class as a resource for the second time resource = shrinkWrapClassLoader.getResource(resourceName); // Assertions Assert.assertNotNull(resource); // SHRINKWRAP-237: This throws IOException: Stream closed IOUtil.copyWithClose(resource.openStream(), new ByteArrayOutputStream()); }
/** * Assert an asset is actually in the file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInTar(final File archive, final ArchivePath path, final Asset asset) throws IllegalArgumentException, IOException { final InputStream in = this.getEntryFromTarFile(archive, path); byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); byte[] actualContents = IOUtil.asByteArray(in); Assert.assertArrayEquals(expectedContents, actualContents); }
IOUtil.copyWithClose(assetInputStream, assetBufferedOutputStream); } catch (final Exception e) {
/** * Assert an asset is actually in the Zip file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInZip(ZipFile expectedZip, ArchivePath path, Asset asset) throws IllegalArgumentException, IOException { final ZipEntry entry = this.getEntryFromZip(expectedZip, path); final byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); final byte[] actualContents = IOUtil.asByteArray(expectedZip.getInputStream(entry)); Assert.assertArrayEquals(expectedContents, actualContents); }
/** * Performs copy operation between currentNodeStream and outputStream using buffer length. * * @throws IOException */ private void doCopy() throws IOException { int copied = IOUtil.copy(currentNodeStream, outputStream, BUFFER_LENGTH); if (copied == -1) { currentNodeStream.close(); currentNodeStream = null; endAsset(); } }
IOUtil.copyWithClose(assetInputStream, assetBufferedOutputStream); } catch (final Exception e) {
/** * Assert an asset is actually in the Zip file * * @throws IOException * @throws IllegalArgumentException */ private void assertAssetInZip(ZipFile expectedZip, ArchivePath path, Asset asset) throws IllegalArgumentException, IOException { final ZipEntry entry = this.getEntryFromZip(expectedZip, path); final byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); final byte[] actualContents = IOUtil.asByteArray(expectedZip.getInputStream(entry)); Assert.assertArrayEquals(expectedContents, actualContents); }
/** * {@inheritDoc} */ @Override public void writeTo(final OutputStream outputStream, final Formatter formatter) throws IllegalArgumentException { try { IOUtil.bufferedWriteWithFlush(outputStream, toString(formatter).getBytes()); } catch (IOException ioe) { throw new IllegalArgumentException("Could not write Archive contents to specified OutputStream", ioe); } }
/** * Helper method to run a specified task and automatically handle the closing of the stream. * * @param stream * @param task */ public static <S extends Closeable> void closeOnComplete(S stream, StreamTask<S> task) { closeOnComplete(stream, task, DEFAULT_ERROR_HANDLER); }
/** * Copies the contents from an InputStream to an OutputStream and closes both streams. * * @param input * @param output * @throws IOException * If a problem occurred during any I/O operations during the copy, but on closing the streams these * will be ignored and logged at {@link Level#FINER} */ public static void copyWithClose(InputStream input, OutputStream output) throws IOException { try { copy(input, output); } finally { try { input.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } try { output.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } } }
/** * https://jira.jboss.org/jira/browse/SHRINKWRAP-86 Ensure an IllegalArgumentException is thrown when output * directory is a file */ @Test(expected = IllegalArgumentException.class) public void testExportExplodedOutpuDirIsAFile() throws Exception { log.info("testExportExplodedOutpuDirIsAFile"); final File directory = createTempDirectory("testExportExplodedOutpuDirIsAFile"); // Will cause the creation of Archive directory to fail final File existingFile = new File(directory, NAME_ARCHIVE + this.getArchiveExtension()); final boolean created = existingFile.createNewFile(); IOUtil.copyWithClose(new ByteArrayInputStream("test-test".getBytes()), new FileOutputStream(existingFile)); Assert.assertEquals("Could not create test file", true, created); createArchiveWithAssets().as(ExplodedExporter.class).exportExploded(directory); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#ensureAssetInExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath, org.jboss.shrinkwrap.api.asset.Asset) */ protected final void ensureAssetInExportedFile(final File file, final ArchivePath path, final Asset asset) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; assert asset != null : "asset must be specified"; // Get as Exported File final InputStream actualStream = this.getContentsFromExportedFile(file, path); assert actualStream != null : "No contents found at path " + path + " in " + file.getAbsolutePath(); byte[] actualContents = IOUtil.asByteArray(actualStream); byte[] expectedContents = IOUtil.asByteArray(asset.openStream()); Assert.assertArrayEquals(expectedContents, actualContents); } }
@Test public void shouldBufferWritesCorrectly() throws IOException { MockOutputStream outputStream = new MockOutputStream(); byte[] content = new byte[9202]; for (int i = 0; i < content.length; i++) { content[i] = (i + "").getBytes()[0]; } IOUtil.bufferedWriteWithFlush(outputStream, content); Assert.assertArrayEquals("Inconsistent writes?", content, outputStream.getContents()); }
/** * Copies the contents from an InputStream to an OutputStream and closes both streams. * * @param input * @param output * @throws IOException * If a problem occurred during any I/O operations during the copy, but on closing the streams these * will be ignored and logged at {@link Level#FINER} */ public static void copyWithClose(InputStream input, OutputStream output) throws IOException { try { copy(input, output); } finally { try { input.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } try { output.close(); } catch (final IOException ignore) { if (log.isLoggable(Level.FINER)) { log.finer("Could not close stream due to: " + ignore.getMessage() + "; ignoring"); } } } }
/** * Test to make sue an archive can be exported and all contents are correctly located. * * @throws Exception */ @Test public void testExport() throws Exception { log.info("testExport"); // Get an archive instance Archive<?> archive = createArchiveWithAssets(); // Export as InputStream final InputStream exportStream = this.exportAsInputStream(archive); // Validate final File tempDirectory = createTempDirectory("testExport"); final File serialized = new File(tempDirectory, archive.getName()); final FileOutputStream out = new FileOutputStream(serialized); IOUtil.copyWithClose(exportStream, out); ensureInExpectedForm(serialized); }
/** * {@inheritDoc} * * @see org.jboss.shrinkwrap.impl.base.exporter.StreamExporterTestBase#getContentsFromExportedFile(java.io.File, * org.jboss.shrinkwrap.api.ArchivePath) */ @Override protected InputStream getContentsFromExportedFile(final File file, final ArchivePath path) throws IOException { // Precondition checks assert file != null : "file must be specified"; assert path != null : "path must be specified"; // Get as Zip File final ZipFile zipFile = new ZipFile(file); final ZipEntry entry = zipFile.getEntry(PathUtil.optionallyRemovePrecedingSlash(path.get())); if (entry == null) { return null; } final byte[] actualContents = IOUtil.asByteArray(zipFile.getInputStream(entry)); return new ByteArrayInputStream(actualContents); }
/** * Serializes the invocation with a custom form * * @serialData After all non-transient fields are written, we send the {@link Archive} contents encoded as ZIP. */ private void writeObject(final ObjectOutputStream out) throws IOException { // Default write of non-transient fields out.defaultWriteObject(); // Write as ZIP final InputStream in = archive.as(ZipExporter.class).exportAsInputStream(); try { IOUtil.copy(in, out); // Don't close the outstream // Write the ID explicitly out.writeObject(id); } finally { // In case we get an InputStream type that supports closing in.close(); } // Log if (log.isLoggable(Level.FINER)) { log.finer("Wrote archive: " + archive.toString()); } }