Skip to content

Convert InputStreamSupplier to lambdas #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DirectoryArchiver
extends AbstractArchiver
{

private final List<Runnable> directoryChmods = new ArrayList<Runnable>();
private final List<Runnable> directoryChmods = new ArrayList<>();

public void resetArchiver()
throws IOException
Expand Down Expand Up @@ -103,10 +103,7 @@ public void execute()
}
}

for ( Runnable directoryChmod : directoryChmods )
{
directoryChmod.run();
}
directoryChmods.forEach( Runnable::run );
directoryChmods.clear();
}
catch ( final IOException ioe )
Expand Down Expand Up @@ -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 );
}

} );
}

Expand Down
55 changes: 22 additions & 33 deletions src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -139,12 +139,12 @@ public class JarArchiver
* <p/>
* Will not be filled unless the user has asked for an index.
*/
private Vector<String> rootEntries;
private List<String> rootEntries;

/**
* Path containing jars that shall be indexed in addition to this archive.
*/
private ArrayList<String> indexJars;
private List<String> indexJars;

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

/**
Expand Down Expand Up @@ -299,7 +299,7 @@ public void addConfiguredIndexJars( File indexJar )
{
if ( indexJars == null )
{
indexJars = new ArrayList<String>();
indexJars = new ArrayList<>();
}
indexJars.add( indexJar.getAbsolutePath() );
}
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -440,7 +441,7 @@ private void createIndexList( ConcurrentJarCreator zOut )
filteredDirs.remove( META_INF_NAME + '/' );
}
}
writeIndexLikeList( new ArrayList<String>( filteredDirs ), rootEntries, writer );
writeIndexLikeList( new ArrayList<>( filteredDirs ), rootEntries, writer );
writer.println();

if ( indexJars != null )
Expand All @@ -464,8 +465,8 @@ private void createIndexList( ConcurrentJarCreator zOut )
String name = findJarName( indexJar, cpEntries );
if ( name != null )
{
ArrayList<String> dirs = new ArrayList<String>();
ArrayList<String> files = new ArrayList<String>();
List<String> dirs = new ArrayList<>();
List<String> files = new ArrayList<>();
grabFilesAndDirs( indexJar, dirs, files );
if ( dirs.size() + files.size() > 0 )
{
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -624,7 +625,7 @@ protected void cleanUp()
filesetManifest = null;
originalManifest = null;
}
rootEntries.removeAllElements();
rootEntries.clear();
}

/**
Expand Down Expand Up @@ -722,21 +723,9 @@ protected static String findJarName( String fileName, String[] classpath )
return new File( fileName ).getName();
}
fileName = fileName.replace( File.separatorChar, '/' );
SortedMap<String, String> matches = new TreeMap<String, String>( new Comparator<String>()
{

// 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<String, String> matches = new TreeMap<>( Comparator.comparingInt( String::length ).reversed() );

for ( String aClasspath : classpath )
{
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/codehaus/plexus/archiver/util/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -96,6 +97,7 @@ public abstract class AbstractZipArchiver
/**
* @deprecated Use {@link Archiver#setDuplicateBehavior(String)} instead.
*/
@Deprecated
protected final String duplicate = Archiver.DUPLICATES_SKIP;

/**
Expand Down Expand Up @@ -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 ) )
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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 );
}
}
}
Expand All @@ -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
*
Expand Down
Loading