Skip to content

Commit

Permalink
[MSHADE-431] Use a caching output stream (#158)
Browse files Browse the repository at this point in the history
Use a caching output stream to avoid overwriting the file if not needed
  • Loading branch information
gnodet authored Oct 20, 2022
1 parent 31d3506 commit 5f45ecf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -62,6 +61,7 @@
import org.apache.maven.plugins.shade.resource.ReproducibleResourceTransformer;
import org.apache.maven.plugins.shade.resource.ResourceTransformer;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.io.CachingOutputStream;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
Expand Down Expand Up @@ -116,7 +116,8 @@ public void shade( ShadeRequest shadeRequest )
shadeRequest.getUberJar().getParentFile().mkdirs();

try ( JarOutputStream out =
new JarOutputStream( new BufferedOutputStream( new FileOutputStream( shadeRequest.getUberJar() ) ) ) )
new JarOutputStream( new BufferedOutputStream(
new CachingOutputStream( shadeRequest.getUberJar() ) ) ) )
{
goThroughAllJarEntriesForManifestTransformer( shadeRequest, resources, manifestTransformer, out );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -343,6 +346,44 @@ public void testShaderWithNestedJar() throws Exception

final String innerJarFileName = "inner.jar";

temporaryFolder.create();
File innerJar = temporaryFolder.newFile( innerJarFileName );
try ( JarOutputStream jos = new JarOutputStream( Files.newOutputStream( innerJar.toPath() ) ) )
{
jos.putNextEntry( new JarEntry( "foo.txt" ) );
jos.write( "c1".getBytes( StandardCharsets.UTF_8 ) );
jos.closeEntry();
}

ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars( new LinkedHashSet<>( Collections.singleton( innerJar ) ) );
shadeRequest.setFilters( Collections.emptyList() );
shadeRequest.setRelocators( Collections.emptyList() );
shadeRequest.setResourceTransformers( Collections.emptyList() );
File shadedFile = temporaryFolder.newFile( "shaded.jar" );
shadeRequest.setUberJar( shadedFile );

DefaultShader shader = newShader();
shader.shade( shadeRequest );

FileTime lastModified = FileTime.from( Files.getLastModifiedTime( shadedFile.toPath() ).toInstant()
.minus( 5, ChronoUnit.SECONDS ) );

Files.setLastModifiedTime( shadedFile.toPath(), lastModified );

shader.shade(shadeRequest);
assertEquals( lastModified, Files.getLastModifiedTime( shadedFile.toPath() ) );

temporaryFolder.delete();
}

@Test
public void testShaderNoOverwrite() throws Exception
{
TemporaryFolder temporaryFolder = new TemporaryFolder();

final String innerJarFileName = "inner.jar";

temporaryFolder.create();
File innerJar = temporaryFolder.newFile( innerJarFileName );
try ( JarOutputStream jos = new JarOutputStream( new FileOutputStream( innerJar ) ) )
Expand Down

0 comments on commit 5f45ecf

Please sign in to comment.