From 581e306276b780092b43cd5b9a230f9960fa27c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Sol=C3=B3rzano?= Date: Wed, 27 Apr 2022 13:32:28 +0200 Subject: [PATCH] Convert InputStreamSupplier to lambdas MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jorge Solórzano --- .../archiver/dir/DirectoryArchiver.java | 28 ++--- .../plexus/archiver/jar/JarArchiver.java | 55 ++++------ .../plexus/archiver/util/Streams.java | 3 + .../archiver/zip/AbstractZipArchiver.java | 54 +++------ .../archiver/zip/ConcurrentJarCreator.java | 103 ++++-------------- .../plexus/archiver/jar/JarArchiverTest.java | 2 - .../zip/ConcurrentJarCreatorTest.java | 23 ++-- 7 files changed, 78 insertions(+), 190 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java b/src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java index b9945c39d..f63eb448c 100644 --- a/src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java @@ -39,7 +39,7 @@ public class DirectoryArchiver extends AbstractArchiver { - private final List directoryChmods = new ArrayList(); + private final List directoryChmods = new ArrayList<>(); public void resetArchiver() throws IOException @@ -103,10 +103,7 @@ public void execute() } } - for ( Runnable directoryChmod : directoryChmods ) - { - directoryChmod.run(); - } + directoryChmods.forEach( Runnable::run ); directoryChmods.clear(); } catch ( final IOException ioe ) @@ -169,22 +166,15 @@ else if ( !outFile.mkdirs() ) throw new ArchiverException( "Unable to create directory or parent directory of " + outFile ); } - directoryChmods.add( new Runnable() - { - - @Override - public void run() + directoryChmods.add( () -> { + try { - try - { - setFileModes( entry, outFile, inLastModified ); - } - catch ( IOException e ) - { - throw new ArchiverException( "Failed setting file attributes", e ); - } + setFileModes( entry, outFile, inLastModified ); + } + catch ( IOException e ) + { + throw new ArchiverException( "Failed setting file attributes", e ); } - } ); } diff --git a/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java b/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java index b6d25e5ff..54032cf78 100644 --- a/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java @@ -27,6 +27,7 @@ import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -37,7 +38,6 @@ import java.util.SortedMap; import java.util.StringTokenizer; import java.util.TreeMap; -import java.util.Vector; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; @@ -139,12 +139,12 @@ public class JarArchiver *

* Will not be filled unless the user has asked for an index. */ - private Vector rootEntries; + private List rootEntries; /** * Path containing jars that shall be indexed in addition to this archive. */ - private ArrayList indexJars; + private List indexJars; /** * Creates a minimal default manifest with {@code Manifest-Version: 1.0} only. @@ -159,7 +159,7 @@ public JarArchiver() super(); archiveType = "jar"; setEncoding( "UTF8" ); - rootEntries = new Vector(); + rootEntries = new ArrayList<>(); } /** @@ -299,7 +299,7 @@ public void addConfiguredIndexJars( File indexJar ) { if ( indexJars == null ) { - indexJars = new ArrayList(); + indexJars = new ArrayList<>(); } indexJars.add( indexJar.getAbsolutePath() ); } @@ -373,13 +373,14 @@ private void writeManifest( ConcurrentJarCreator zOut, Manifest manifest ) } zipDir( null, zOut, "META-INF/", DEFAULT_DIR_MODE, getEncoding() ); + // time to write the manifest - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream( 128 ); manifest.write( baos ); + InputStreamSupplier in = () -> new ByteArrayInputStream( baos.toByteArray() ); - ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); - super.zipFile( createInputStreamSupplier( bais ), zOut, MANIFEST_NAME, System.currentTimeMillis(), null, - DEFAULT_FILE_MODE, null, false ); + super.zipFile( in, zOut, MANIFEST_NAME, System.currentTimeMillis(), null, DEFAULT_FILE_MODE, null, + false ); super.initZipOutputStream( zOut ); } @@ -408,9 +409,9 @@ protected void finalizeZipOutputStream( ConcurrentJarCreator zOut ) private void createIndexList( ConcurrentJarCreator zOut ) throws IOException, ArchiverException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream( 128 ); // encoding must be UTF8 as specified in the specs. - PrintWriter writer = new PrintWriter( new OutputStreamWriter( baos, "UTF8" ) ); + PrintWriter writer = new PrintWriter( new OutputStreamWriter( baos, StandardCharsets.UTF_8 ) ); // version-info blankline writer.println( "JarIndex-Version: 1.0" ); @@ -440,7 +441,7 @@ private void createIndexList( ConcurrentJarCreator zOut ) filteredDirs.remove( META_INF_NAME + '/' ); } } - writeIndexLikeList( new ArrayList( filteredDirs ), rootEntries, writer ); + writeIndexLikeList( new ArrayList<>( filteredDirs ), rootEntries, writer ); writer.println(); if ( indexJars != null ) @@ -464,8 +465,8 @@ private void createIndexList( ConcurrentJarCreator zOut ) String name = findJarName( indexJar, cpEntries ); if ( name != null ) { - ArrayList dirs = new ArrayList(); - ArrayList files = new ArrayList(); + List dirs = new ArrayList<>(); + List files = new ArrayList<>(); grabFilesAndDirs( indexJar, dirs, files ); if ( dirs.size() + files.size() > 0 ) { @@ -479,9 +480,9 @@ private void createIndexList( ConcurrentJarCreator zOut ) writer.flush(); - ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); + InputStreamSupplier in = () -> new ByteArrayInputStream( baos.toByteArray() ); - super.zipFile( createInputStreamSupplier( bais ), zOut, INDEX_NAME, System.currentTimeMillis(), null, + super.zipFile( in, zOut, INDEX_NAME, System.currentTimeMillis(), null, DEFAULT_FILE_MODE, null, true ); } @@ -511,9 +512,9 @@ else if ( INDEX_NAME.equalsIgnoreCase( vPath ) && index ) } else { - if ( index && ( !vPath.contains( "/" ) ) ) + if ( index && !vPath.contains( "/" ) ) { - rootEntries.addElement( vPath ); + rootEntries.add( vPath ); } super.zipFile( is, zOut, vPath, lastModified, fromArchive, mode, symlinkDestination, addInParallel ); } @@ -624,7 +625,7 @@ protected void cleanUp() filesetManifest = null; originalManifest = null; } - rootEntries.removeAllElements(); + rootEntries.clear(); } /** @@ -722,21 +723,9 @@ protected static String findJarName( String fileName, String[] classpath ) return new File( fileName ).getName(); } fileName = fileName.replace( File.separatorChar, '/' ); - SortedMap matches = new TreeMap( new Comparator() - { - - // longest match comes first - @Override - public int compare( String o1, String o2 ) - { - if ( ( o1 != null ) && ( o2 != null ) ) - { - return o2.length() - o1.length(); - } - return 0; - } - } ); + // longest match comes first + SortedMap matches = new TreeMap<>( Comparator.comparingInt( String::length ).reversed() ); for ( String aClasspath : classpath ) { diff --git a/src/main/java/org/codehaus/plexus/archiver/util/Streams.java b/src/main/java/org/codehaus/plexus/archiver/util/Streams.java index a711a99a3..b37689afe 100644 --- a/src/main/java/org/codehaus/plexus/archiver/util/Streams.java +++ b/src/main/java/org/codehaus/plexus/archiver/util/Streams.java @@ -17,6 +17,7 @@ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -32,6 +33,8 @@ public class Streams { + public static final InputStream EMPTY_INPUTSTREAM = new ByteArrayInputStream( new byte[0] ); + public static BufferedInputStream bufferedInputStream( InputStream is ) { return is instanceof BufferedInputStream diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java index 6ddfd02f0..e38474872 100755 --- a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java @@ -22,10 +22,11 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; +import java.io.UncheckedIOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.attribute.FileTime; import java.util.Calendar; @@ -41,7 +42,6 @@ import org.apache.commons.compress.archivers.zip.ZipEncoding; import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; import org.apache.commons.compress.parallel.InputStreamSupplier; -import org.apache.commons.compress.utils.Charsets; import org.codehaus.plexus.archiver.AbstractArchiver; import org.codehaus.plexus.archiver.ArchiveEntry; import org.codehaus.plexus.archiver.Archiver; @@ -50,6 +50,7 @@ import org.codehaus.plexus.archiver.UnixStat; import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException; import org.codehaus.plexus.archiver.util.ResourceUtils; +import org.codehaus.plexus.archiver.util.Streams; import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier; import org.codehaus.plexus.components.io.resources.PlexusIoResource; import org.codehaus.plexus.util.FileUtils; @@ -96,6 +97,7 @@ public abstract class AbstractZipArchiver /** * @deprecated Use {@link Archiver#setDuplicateBehavior(String)} instead. */ + @Deprecated protected final String duplicate = Archiver.DUPLICATES_SKIP; /** @@ -330,11 +332,11 @@ private ZipArchiveOutputStream.UnicodeExtraFieldPolicy getUnicodeExtraFieldPolic effectiveEncoding = Charset.defaultCharset().name(); } - boolean utf8 = Charsets.UTF_8.name().equalsIgnoreCase( effectiveEncoding ); + boolean utf8 = StandardCharsets.UTF_8.name().equalsIgnoreCase( effectiveEncoding ); if ( !utf8 ) { - for ( String alias : Charsets.UTF_8.aliases() ) + for ( String alias : StandardCharsets.UTF_8.aliases() ) { if ( alias.equalsIgnoreCase( effectiveEncoding ) ) { @@ -468,12 +470,11 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin ze.setMethod( doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED ); ze.setUnixMode( UnixStat.FILE_FLAG | mode ); - InputStream payload; if ( ze.isUnixSymlink() ) { final byte[] bytes = encodeArchiveEntry( symlinkDestination, getEncoding() ); - payload = new ByteArrayInputStream( bytes ); - zOut.addArchiveEntry( ze, createInputStreamSupplier( payload ), true ); + InputStreamSupplier payload = () -> new ByteArrayInputStream( bytes ); + zOut.addArchiveEntry( ze, payload, true ); } else { @@ -506,22 +507,15 @@ protected void zipFile( final ArchiveEntry entry, ConcurrentJarCreator zOut, Str final boolean b = entry.getResource() instanceof SymlinkDestinationSupplier; String symlinkTarget = b ? ( (SymlinkDestinationSupplier) entry.getResource() ).getSymlinkDestination() : null; - InputStreamSupplier in = new InputStreamSupplier() - { - - @Override - public InputStream get() + InputStreamSupplier in = () -> { + try { - try - { - return entry.getInputStream(); - } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } + return entry.getInputStream(); + } + catch ( IOException e ) + { + throw new UncheckedIOException( e ); } - }; try { @@ -619,14 +613,14 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v if ( !isSymlink ) { - zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( "".getBytes() ) ), true ); + zOut.addArchiveEntry( ze, () -> Streams.EMPTY_INPUTSTREAM, true ); } else { String symlinkDestination = ( (SymlinkDestinationSupplier) dir ).getSymlinkDestination(); final byte[] bytes = encodeArchiveEntry( symlinkDestination, encodingToUse ); ze.setMethod( ZipArchiveEntry.DEFLATED ); - zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( bytes ) ), true ); + zOut.addArchiveEntry( ze, () -> new ByteArrayInputStream( bytes ), true ); } } } @@ -642,20 +636,6 @@ private byte[] encodeArchiveEntry( String payload, String encoding ) return encodedPayloadBytes; } - protected InputStreamSupplier createInputStreamSupplier( final InputStream inputStream ) - { - return new InputStreamSupplier() - { - - @Override - public InputStream get() - { - return inputStream; - } - - }; - } - /** * Create an empty zip file * diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java index 3a1c0f81d..ecd99f927 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.SequenceInputStream; +import java.io.UncheckedIOException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util.zip.Deflater; @@ -30,12 +31,12 @@ import org.apache.commons.compress.archivers.zip.StreamCompressor; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest; -import org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequestSupplier; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.compress.parallel.ScatterGatherBackingStore; import org.apache.commons.compress.parallel.ScatterGatherBackingStoreSupplier; -import org.codehaus.plexus.util.IOUtil; +import org.apache.commons.compress.utils.IOUtils; +import org.codehaus.plexus.archiver.util.Streams; import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest; @@ -67,6 +68,7 @@ private static class DeferredSupplier this.threshold = threshold; } + @Override public ScatterGatherBackingStore get() throws IOException { @@ -152,46 +154,28 @@ public void addArchiveEntry( final ZipArchiveEntry zipArchiveEntry, final InputS { throw new IllegalArgumentException( "Method must be set on the supplied zipArchiveEntry" ); } - if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals( zipArchiveEntry.getName() ) ) + final String zipEntryName = zipArchiveEntry.getName(); + if ( "META-INF".equals( zipEntryName ) || "META-INF/".equals( zipEntryName ) ) { - InputStream payload = source.get(); // TODO This should be enforced because META-INF non-directory does not make any sense?! if ( zipArchiveEntry.isDirectory() ) { zipArchiveEntry.setMethod( ZipEntry.STORED ); } - metaInfDir.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, - createInputStreamSupplier( payload ) ) ); - - payload.close(); + metaInfDir.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, source ) ); } - else if ( "META-INF/MANIFEST.MF".equals( zipArchiveEntry.getName() ) ) + else if ( "META-INF/MANIFEST.MF".equals( zipEntryName ) ) { - InputStream payload = source.get(); - // TODO This should be enforced because META-INF/MANIFEST as non-file does not make any sense?! - if ( zipArchiveEntry.isDirectory() ) - { - zipArchiveEntry.setMethod( ZipEntry.STORED ); - } - manifest.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, - createInputStreamSupplier( payload ) ) ); - - payload.close(); + manifest.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, source ) ); } else if ( zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink() ) { - final ByteArrayInputStream payload = new ByteArrayInputStream( new byte[] - { - } ); - - directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, createInputStreamSupplier( - payload ) ) ); - - payload.close(); + directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, + () -> Streams.EMPTY_INPUTSTREAM ) ); } else if ( addInParallel ) { - parallelScatterZipCreator.addArchiveEntry( createEntrySupplier( zipArchiveEntry, source ) ); + parallelScatterZipCreator.addArchiveEntry( () -> createEntry( zipArchiveEntry, source ) ); } else { @@ -199,20 +183,6 @@ else if ( addInParallel ) } } - private InputStreamSupplier createInputStreamSupplier( final InputStream payload ) - { - return new InputStreamSupplier() - { - - @Override - public InputStream get() - { - return payload; - } - - }; - } - public void writeTo( ZipArchiveOutputStream targetStream ) throws IOException, ExecutionException, InterruptedException { @@ -240,48 +210,24 @@ public String getStatisticsMessage() return parallelScatterZipCreator.getStatisticsMessage() + " Zip Close: " + zipCloseElapsed + "ms"; } - private ZipArchiveEntryRequestSupplier createEntrySupplier( final ZipArchiveEntry zipArchiveEntry, - final InputStreamSupplier inputStreamSupplier ) - { - - return new ZipArchiveEntryRequestSupplier() - { - - @Override - public ZipArchiveEntryRequest get() - { - try - { - return createEntry( zipArchiveEntry, inputStreamSupplier ); - } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } - } - - }; - } - private ZipArchiveEntryRequest createEntry( final ZipArchiveEntry zipArchiveEntry, - final InputStreamSupplier inputStreamSupplier ) throws IOException + final InputStreamSupplier inputStreamSupplier ) { // if we re-compress the zip files there is no need to look at the input stream - if ( compressAddedZips ) { return createZipArchiveEntryRequest( zipArchiveEntry, inputStreamSupplier ); } - // otherwise we should inspect the first four bites to see if the input stream is zip file or not - InputStream is = inputStreamSupplier.get(); + // otherwise we should inspect the first four bytes to see if the input stream is zip file or not byte[] header = new byte[4]; try { int read = is.read( header ); int compressionMethod = zipArchiveEntry.getMethod(); - if ( isZipHeader( header ) ) { + if ( isZipHeader( header ) ) + { compressionMethod = ZipEntry.STORED; } @@ -291,8 +237,8 @@ private ZipArchiveEntryRequest createEntry( final ZipArchiveEntry zipArchiveEntr } catch ( IOException e ) { - IOUtil.close( is ); - throw e; + IOUtils.closeQuietly( is ); + throw new UncheckedIOException( e ); } } @@ -303,18 +249,7 @@ private boolean isZipHeader( byte[] header ) private InputStreamSupplier prependBytesToStream( final byte[] bytes, final int len, final InputStream stream ) { - return new InputStreamSupplier() { - - @Override - public InputStream get() - { - return len > 0 - ? new SequenceInputStream( new ByteArrayInputStream( bytes, 0, len ), stream ) - : stream; - } - - }; - + return () -> len > 0 ? new SequenceInputStream( new ByteArrayInputStream( bytes, 0, len ), stream ) : stream; } } diff --git a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java index d0b9471ae..55c7af298 100644 --- a/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/jar/JarArchiverTest.java @@ -10,9 +10,7 @@ import java.nio.file.attribute.FileTime; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Enumeration; -import java.util.List; import java.util.Random; import java.util.TimeZone; import java.util.zip.ZipEntry; diff --git a/src/test/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreatorTest.java b/src/test/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreatorTest.java index 9ad2193c3..febf6de26 100644 --- a/src/test/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreatorTest.java +++ b/src/test/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreatorTest.java @@ -1,15 +1,14 @@ package org.codehaus.plexus.archiver.zip; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.nio.file.Files; import org.apache.commons.compress.archivers.zip.UnixStat; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; -import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.compress.utils.IOUtils; import org.codehaus.plexus.util.DirectoryScanner; import org.junit.Ignore; @@ -77,21 +76,15 @@ private void doAddAll( String base, ConcurrentJarCreator mos ) throws IOExceptio final File file = new File( base, fileName ); ZipArchiveEntry za = createZipArchiveEntry( file, fileName ); - mos.addArchiveEntry( za, new InputStreamSupplier() - { - - public InputStream get() + mos.addArchiveEntry( za, () -> { + try { - try - { - return file.isFile() ? Files.newInputStream( file.toPath() ) : null; - } - catch ( IOException e ) - { - throw new RuntimeException( e ); - } + return file.isFile() ? Files.newInputStream( file.toPath() ) : null; + } + catch ( IOException e ) + { + throw new UncheckedIOException( e ); } - }, true ); }