Skip to content

Commit 581e306

Browse files
jorsolslachiewicz
authored andcommitted
Convert InputStreamSupplier to lambdas
Signed-off-by: Jorge Solórzano <[email protected]>
1 parent 7eb5a66 commit 581e306

File tree

7 files changed

+78
-190
lines changed

7 files changed

+78
-190
lines changed

src/main/java/org/codehaus/plexus/archiver/dir/DirectoryArchiver.java

+9-19
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class DirectoryArchiver
3939
extends AbstractArchiver
4040
{
4141

42-
private final List<Runnable> directoryChmods = new ArrayList<Runnable>();
42+
private final List<Runnable> directoryChmods = new ArrayList<>();
4343

4444
public void resetArchiver()
4545
throws IOException
@@ -103,10 +103,7 @@ public void execute()
103103
}
104104
}
105105

106-
for ( Runnable directoryChmod : directoryChmods )
107-
{
108-
directoryChmod.run();
109-
}
106+
directoryChmods.forEach( Runnable::run );
110107
directoryChmods.clear();
111108
}
112109
catch ( final IOException ioe )
@@ -169,22 +166,15 @@ else if ( !outFile.mkdirs() )
169166
throw new ArchiverException( "Unable to create directory or parent directory of " + outFile );
170167
}
171168

172-
directoryChmods.add( new Runnable()
173-
{
174-
175-
@Override
176-
public void run()
169+
directoryChmods.add( () -> {
170+
try
177171
{
178-
try
179-
{
180-
setFileModes( entry, outFile, inLastModified );
181-
}
182-
catch ( IOException e )
183-
{
184-
throw new ArchiverException( "Failed setting file attributes", e );
185-
}
172+
setFileModes( entry, outFile, inLastModified );
173+
}
174+
catch ( IOException e )
175+
{
176+
throw new ArchiverException( "Failed setting file attributes", e );
186177
}
187-
188178
} );
189179
}
190180

src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java

+22-33
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.InputStream;
2828
import java.io.OutputStreamWriter;
2929
import java.io.PrintWriter;
30+
import java.nio.charset.StandardCharsets;
3031
import java.util.ArrayList;
3132
import java.util.Collections;
3233
import java.util.Comparator;
@@ -37,7 +38,6 @@
3738
import java.util.SortedMap;
3839
import java.util.StringTokenizer;
3940
import java.util.TreeMap;
40-
import java.util.Vector;
4141

4242
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
4343
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
@@ -139,12 +139,12 @@ public class JarArchiver
139139
* <p/>
140140
* Will not be filled unless the user has asked for an index.
141141
*/
142-
private Vector<String> rootEntries;
142+
private List<String> rootEntries;
143143

144144
/**
145145
* Path containing jars that shall be indexed in addition to this archive.
146146
*/
147-
private ArrayList<String> indexJars;
147+
private List<String> indexJars;
148148

149149
/**
150150
* Creates a minimal default manifest with {@code Manifest-Version: 1.0} only.
@@ -159,7 +159,7 @@ public JarArchiver()
159159
super();
160160
archiveType = "jar";
161161
setEncoding( "UTF8" );
162-
rootEntries = new Vector<String>();
162+
rootEntries = new ArrayList<>();
163163
}
164164

165165
/**
@@ -299,7 +299,7 @@ public void addConfiguredIndexJars( File indexJar )
299299
{
300300
if ( indexJars == null )
301301
{
302-
indexJars = new ArrayList<String>();
302+
indexJars = new ArrayList<>();
303303
}
304304
indexJars.add( indexJar.getAbsolutePath() );
305305
}
@@ -373,13 +373,14 @@ private void writeManifest( ConcurrentJarCreator zOut, Manifest manifest )
373373
}
374374

375375
zipDir( null, zOut, "META-INF/", DEFAULT_DIR_MODE, getEncoding() );
376+
376377
// time to write the manifest
377-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
378+
ByteArrayOutputStream baos = new ByteArrayOutputStream( 128 );
378379
manifest.write( baos );
380+
InputStreamSupplier in = () -> new ByteArrayInputStream( baos.toByteArray() );
379381

380-
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
381-
super.zipFile( createInputStreamSupplier( bais ), zOut, MANIFEST_NAME, System.currentTimeMillis(), null,
382-
DEFAULT_FILE_MODE, null, false );
382+
super.zipFile( in, zOut, MANIFEST_NAME, System.currentTimeMillis(), null, DEFAULT_FILE_MODE, null,
383+
false );
383384
super.initZipOutputStream( zOut );
384385
}
385386

@@ -408,9 +409,9 @@ protected void finalizeZipOutputStream( ConcurrentJarCreator zOut )
408409
private void createIndexList( ConcurrentJarCreator zOut )
409410
throws IOException, ArchiverException
410411
{
411-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
412+
ByteArrayOutputStream baos = new ByteArrayOutputStream( 128 );
412413
// encoding must be UTF8 as specified in the specs.
413-
PrintWriter writer = new PrintWriter( new OutputStreamWriter( baos, "UTF8" ) );
414+
PrintWriter writer = new PrintWriter( new OutputStreamWriter( baos, StandardCharsets.UTF_8 ) );
414415

415416
// version-info blankline
416417
writer.println( "JarIndex-Version: 1.0" );
@@ -440,7 +441,7 @@ private void createIndexList( ConcurrentJarCreator zOut )
440441
filteredDirs.remove( META_INF_NAME + '/' );
441442
}
442443
}
443-
writeIndexLikeList( new ArrayList<String>( filteredDirs ), rootEntries, writer );
444+
writeIndexLikeList( new ArrayList<>( filteredDirs ), rootEntries, writer );
444445
writer.println();
445446

446447
if ( indexJars != null )
@@ -464,8 +465,8 @@ private void createIndexList( ConcurrentJarCreator zOut )
464465
String name = findJarName( indexJar, cpEntries );
465466
if ( name != null )
466467
{
467-
ArrayList<String> dirs = new ArrayList<String>();
468-
ArrayList<String> files = new ArrayList<String>();
468+
List<String> dirs = new ArrayList<>();
469+
List<String> files = new ArrayList<>();
469470
grabFilesAndDirs( indexJar, dirs, files );
470471
if ( dirs.size() + files.size() > 0 )
471472
{
@@ -479,9 +480,9 @@ private void createIndexList( ConcurrentJarCreator zOut )
479480

480481
writer.flush();
481482

482-
ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
483+
InputStreamSupplier in = () -> new ByteArrayInputStream( baos.toByteArray() );
483484

484-
super.zipFile( createInputStreamSupplier( bais ), zOut, INDEX_NAME, System.currentTimeMillis(), null,
485+
super.zipFile( in, zOut, INDEX_NAME, System.currentTimeMillis(), null,
485486
DEFAULT_FILE_MODE, null, true );
486487
}
487488

@@ -511,9 +512,9 @@ else if ( INDEX_NAME.equalsIgnoreCase( vPath ) && index )
511512
}
512513
else
513514
{
514-
if ( index && ( !vPath.contains( "/" ) ) )
515+
if ( index && !vPath.contains( "/" ) )
515516
{
516-
rootEntries.addElement( vPath );
517+
rootEntries.add( vPath );
517518
}
518519
super.zipFile( is, zOut, vPath, lastModified, fromArchive, mode, symlinkDestination, addInParallel );
519520
}
@@ -624,7 +625,7 @@ protected void cleanUp()
624625
filesetManifest = null;
625626
originalManifest = null;
626627
}
627-
rootEntries.removeAllElements();
628+
rootEntries.clear();
628629
}
629630

630631
/**
@@ -722,21 +723,9 @@ protected static String findJarName( String fileName, String[] classpath )
722723
return new File( fileName ).getName();
723724
}
724725
fileName = fileName.replace( File.separatorChar, '/' );
725-
SortedMap<String, String> matches = new TreeMap<String, String>( new Comparator<String>()
726-
{
727-
728-
// longest match comes first
729-
@Override
730-
public int compare( String o1, String o2 )
731-
{
732-
if ( ( o1 != null ) && ( o2 != null ) )
733-
{
734-
return o2.length() - o1.length();
735-
}
736-
return 0;
737-
}
738726

739-
} );
727+
// longest match comes first
728+
SortedMap<String, String> matches = new TreeMap<>( Comparator.comparingInt( String::length ).reversed() );
740729

741730
for ( String aClasspath : classpath )
742731
{

src/main/java/org/codehaus/plexus/archiver/util/Streams.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.BufferedInputStream;
1919
import java.io.BufferedOutputStream;
20+
import java.io.ByteArrayInputStream;
2021
import java.io.File;
2122
import java.io.IOException;
2223
import java.io.InputStream;
@@ -32,6 +33,8 @@
3233
public class Streams
3334
{
3435

36+
public static final InputStream EMPTY_INPUTSTREAM = new ByteArrayInputStream( new byte[0] );
37+
3538
public static BufferedInputStream bufferedInputStream( InputStream is )
3639
{
3740
return is instanceof BufferedInputStream

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

+17-37
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.io.ByteArrayInputStream;
2323
import java.io.File;
2424
import java.io.IOException;
25-
import java.io.InputStream;
2625
import java.io.OutputStream;
26+
import java.io.UncheckedIOException;
2727
import java.nio.ByteBuffer;
2828
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
2930
import java.nio.file.Files;
3031
import java.nio.file.attribute.FileTime;
3132
import java.util.Calendar;
@@ -41,7 +42,6 @@
4142
import org.apache.commons.compress.archivers.zip.ZipEncoding;
4243
import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
4344
import org.apache.commons.compress.parallel.InputStreamSupplier;
44-
import org.apache.commons.compress.utils.Charsets;
4545
import org.codehaus.plexus.archiver.AbstractArchiver;
4646
import org.codehaus.plexus.archiver.ArchiveEntry;
4747
import org.codehaus.plexus.archiver.Archiver;
@@ -50,6 +50,7 @@
5050
import org.codehaus.plexus.archiver.UnixStat;
5151
import org.codehaus.plexus.archiver.exceptions.EmptyArchiveException;
5252
import org.codehaus.plexus.archiver.util.ResourceUtils;
53+
import org.codehaus.plexus.archiver.util.Streams;
5354
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
5455
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
5556
import org.codehaus.plexus.util.FileUtils;
@@ -96,6 +97,7 @@ public abstract class AbstractZipArchiver
9697
/**
9798
* @deprecated Use {@link Archiver#setDuplicateBehavior(String)} instead.
9899
*/
100+
@Deprecated
99101
protected final String duplicate = Archiver.DUPLICATES_SKIP;
100102

101103
/**
@@ -330,11 +332,11 @@ private ZipArchiveOutputStream.UnicodeExtraFieldPolicy getUnicodeExtraFieldPolic
330332
effectiveEncoding = Charset.defaultCharset().name();
331333
}
332334

333-
boolean utf8 = Charsets.UTF_8.name().equalsIgnoreCase( effectiveEncoding );
335+
boolean utf8 = StandardCharsets.UTF_8.name().equalsIgnoreCase( effectiveEncoding );
334336

335337
if ( !utf8 )
336338
{
337-
for ( String alias : Charsets.UTF_8.aliases() )
339+
for ( String alias : StandardCharsets.UTF_8.aliases() )
338340
{
339341
if ( alias.equalsIgnoreCase( effectiveEncoding ) )
340342
{
@@ -468,12 +470,11 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin
468470
ze.setMethod( doCompress ? ZipArchiveEntry.DEFLATED : ZipArchiveEntry.STORED );
469471
ze.setUnixMode( UnixStat.FILE_FLAG | mode );
470472

471-
InputStream payload;
472473
if ( ze.isUnixSymlink() )
473474
{
474475
final byte[] bytes = encodeArchiveEntry( symlinkDestination, getEncoding() );
475-
payload = new ByteArrayInputStream( bytes );
476-
zOut.addArchiveEntry( ze, createInputStreamSupplier( payload ), true );
476+
InputStreamSupplier payload = () -> new ByteArrayInputStream( bytes );
477+
zOut.addArchiveEntry( ze, payload, true );
477478
}
478479
else
479480
{
@@ -506,22 +507,15 @@ protected void zipFile( final ArchiveEntry entry, ConcurrentJarCreator zOut, Str
506507

507508
final boolean b = entry.getResource() instanceof SymlinkDestinationSupplier;
508509
String symlinkTarget = b ? ( (SymlinkDestinationSupplier) entry.getResource() ).getSymlinkDestination() : null;
509-
InputStreamSupplier in = new InputStreamSupplier()
510-
{
511-
512-
@Override
513-
public InputStream get()
510+
InputStreamSupplier in = () -> {
511+
try
514512
{
515-
try
516-
{
517-
return entry.getInputStream();
518-
}
519-
catch ( IOException e )
520-
{
521-
throw new RuntimeException( e );
522-
}
513+
return entry.getInputStream();
514+
}
515+
catch ( IOException e )
516+
{
517+
throw new UncheckedIOException( e );
523518
}
524-
525519
};
526520
try
527521
{
@@ -619,14 +613,14 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
619613

620614
if ( !isSymlink )
621615
{
622-
zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( "".getBytes() ) ), true );
616+
zOut.addArchiveEntry( ze, () -> Streams.EMPTY_INPUTSTREAM, true );
623617
}
624618
else
625619
{
626620
String symlinkDestination = ( (SymlinkDestinationSupplier) dir ).getSymlinkDestination();
627621
final byte[] bytes = encodeArchiveEntry( symlinkDestination, encodingToUse );
628622
ze.setMethod( ZipArchiveEntry.DEFLATED );
629-
zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( bytes ) ), true );
623+
zOut.addArchiveEntry( ze, () -> new ByteArrayInputStream( bytes ), true );
630624
}
631625
}
632626
}
@@ -642,20 +636,6 @@ private byte[] encodeArchiveEntry( String payload, String encoding )
642636
return encodedPayloadBytes;
643637
}
644638

645-
protected InputStreamSupplier createInputStreamSupplier( final InputStream inputStream )
646-
{
647-
return new InputStreamSupplier()
648-
{
649-
650-
@Override
651-
public InputStream get()
652-
{
653-
return inputStream;
654-
}
655-
656-
};
657-
}
658-
659639
/**
660640
* Create an empty zip file
661641
*

0 commit comments

Comments
 (0)