Skip to content

Commit

Permalink
[MSHADE-417] Fix null bytes appended to small files (#160)
Browse files Browse the repository at this point in the history
* Fix MSHADE-417
* don't unread when nothing was read
* Add a unit test

Co-authored-by: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com>
  • Loading branch information
gnodet and NichtStudioCode authored Oct 20, 2022
1 parent 41bd72f commit 0945bcb
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 @@ -181,8 +181,11 @@ protected ZipHeaderPeekInputStream( InputStream in )
public boolean hasZipHeader() throws IOException
{
final byte[] header = new byte[HEADER_LEN];
super.read( header, 0, HEADER_LEN );
super.unread( header );
int len = super.read( header, 0, HEADER_LEN );
if ( len != -1 )
{
super.unread( header, 0, len );
}
return Arrays.equals( header, ZIP_HEADER );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,45 @@ public void testShaderWithDuplicateService() throws Exception
temporaryFolder.delete();
}

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

final String innerJarFileName = "inner.jar";
int len;

temporaryFolder.create();
File innerJar = temporaryFolder.newFile( innerJarFileName );
try ( JarOutputStream jos = new JarOutputStream( new FileOutputStream( innerJar ) ) )
{
jos.putNextEntry( new JarEntry( "foo.txt" ) );
byte[] bytes = "c1".getBytes(StandardCharsets.UTF_8);
len = bytes.length;
jos.write( bytes );
jos.closeEntry();
}

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

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

JarFile shadedJarFile = new JarFile( shadedFile );
JarEntry entry = shadedJarFile.getJarEntry( "foo.txt" );

//After shading, entry compression method should not be changed.
Assert.assertEquals( entry.getSize(), len );

temporaryFolder.delete();
}

private void writeEntryWithoutCompression( String entryName, byte[] entryBytes, JarOutputStream jos ) throws IOException
{
final JarEntry entry = new JarEntry( entryName );
Expand Down

0 comments on commit 0945bcb

Please sign in to comment.