Skip to content
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
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The <action> type attribute can be add,update,fix,remove.
<!-- FIX pack200 -->
<action type="fix" dev="ggregory" due-to="Gary Gregory, Igor Morgenstern">org.apache.commons.compress.harmony.pack200 now throws Pack200Exception, IllegalArgumentException, IllegalStateException, instead of other runtime exceptions and Error.</action>
<action type="fix" dev="ppkarwasz" due-to="Raeps">Extract duplicate code in org.apache.commons.compress.harmony.pack200.IntList.</action>
<action type="fix" dev="pkarwasz" due-to="Piotr P. Karwasz">Simplify `PackingUtils` by leveraging Commons IO.</action>
<!-- FIX cpio -->
<action type="fix" dev="ggregory" due-to="Stan, Gary Gregory">CpioArchiveEntry now throws ArchiveException instead of Arithmetic exception.</action>
<action type="fix" dev="ggregory" due-to="Stan, Gary Gregory">CpioArchiveInputStream.getNextEntry() now throws a MemoryLimitException instead of OutOfMemoryError when it can't process input greater than available memory.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.commons.compress.harmony.pack200;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -40,6 +39,8 @@
import java.util.logging.SimpleFormatter;

import org.apache.commons.compress.harmony.pack200.Archive.PackingFile;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.function.IOConsumer;

public class PackingUtils {

Expand Down Expand Up @@ -97,16 +98,12 @@ public static void config(final PackingOptions options) throws IOException {
public static void copyThroughJar(final JarFile jarFile, final OutputStream outputStream) throws IOException {
try (JarOutputStream jarOutputStream = new JarOutputStream(outputStream)) {
jarOutputStream.setComment("PACK200");
final byte[] bytes = new byte[16384];
final Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
final JarEntry jarEntry = entries.nextElement();
jarOutputStream.putNextEntry(jarEntry);
try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
int bytesRead;
while ((bytesRead = inputStream.read(bytes)) != -1) {
jarOutputStream.write(bytes, 0, bytesRead);
}
IOUtils.copyLarge(inputStream, jarOutputStream);
jarOutputStream.closeEntry();
log("Packed " + jarEntry.getName());
}
Expand All @@ -128,14 +125,10 @@ public static void copyThroughJar(final JarInputStream jarInputStream, final Out
jarOutputStream.setComment("PACK200");
log("Packed " + JarFile.MANIFEST_NAME);

final byte[] bytes = new byte[16384];
JarEntry jarEntry;
int bytesRead;
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
jarOutputStream.putNextEntry(jarEntry);
while ((bytesRead = jarInputStream.read(bytes)) != -1) {
jarOutputStream.write(bytes, 0, bytesRead);
}
IOUtils.copyLarge(jarInputStream, jarOutputStream);
log("Packed " + jarEntry.getName());
}
jarInputStream.close();
Expand All @@ -144,14 +137,11 @@ public static void copyThroughJar(final JarInputStream jarInputStream, final Out

public static List<PackingFile> getPackingFileListFromJar(final JarFile jarFile, final boolean keepFileOrder) throws IOException {
final List<PackingFile> packingFileList = new ArrayList<>();
final Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
final JarEntry jarEntry = jarEntries.nextElement();
IOConsumer.forEach(jarFile.stream(), jarEntry -> {
try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
final byte[] bytes = readJarEntry(jarEntry, new BufferedInputStream(inputStream));
packingFileList.add(new PackingFile(bytes, jarEntry));
packingFileList.add(new PackingFile(readJarEntry(jarEntry, inputStream), jarEntry));
Comment on lines -151 to +142
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffering is no longer necessary: Commons IO uses optimal size buffers to copy data.

}
}
});

// check whether it need reorder packing file list
if (!keepFileOrder) {
Expand All @@ -173,10 +163,8 @@ public static List<PackingFile> getPackingFileListFromJar(final JarInputStream j

// add rest of entries in the jar
JarEntry jarEntry;
byte[] bytes;
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
bytes = readJarEntry(jarEntry, new BufferedInputStream(jarInputStream));
packingFileList.add(new PackingFile(bytes, jarEntry));
packingFileList.add(new PackingFile(readJarEntry(jarEntry, jarInputStream), jarEntry));
}

// check whether it need reorder packing file list
Expand All @@ -191,19 +179,13 @@ public static void log(final String message) {
}

private static byte[] readJarEntry(final JarEntry jarEntry, final InputStream inputStream) throws IOException {
long size = jarEntry.getSize();
final long size = jarEntry.getSize();
if (size > Integer.MAX_VALUE) {
// TODO: Should probably allow this
throw new IllegalArgumentException("Large Class!");
}
if (size < 0) {
size = 0;
}
final byte[] bytes = new byte[(int) size];
if (inputStream.read(bytes) != size) {
throw new IllegalArgumentException("Error reading from stream");
}
return bytes;
// Negative size means unknown size
return size < 0 ? IOUtils.toByteArray(inputStream) : IOUtils.toByteArray(inputStream, (int) size, IOUtils.DEFAULT_BUFFER_SIZE);
}

private static void reorderPackingFiles(final List<PackingFile> packingFileList) {
Expand Down