Skip to content
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

[MSHADE-417] Fix null bytes appended to small files #160

Merged
merged 3 commits into from Oct 20, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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