Skip to content

Commit 5a603f2

Browse files
committed
Bypass extra array-copy in deflate decompression
1 parent a255299 commit 5a603f2

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

src/main/java/software/coley/llzip/format/compression/DeflateDecompressor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package software.coley.llzip.format.compression;
22

33
import software.coley.llzip.format.model.LocalFileHeader;
4-
import software.coley.llzip.util.BufferData;
54
import software.coley.llzip.util.ByteData;
5+
import software.coley.llzip.util.FastWrapOutputStream;
66

7-
import java.io.ByteArrayOutputStream;
87
import java.io.IOException;
98
import java.util.zip.DataFormatException;
109
import java.util.zip.Inflater;
@@ -21,7 +20,7 @@ public ByteData decompress(LocalFileHeader header, ByteData data) throws IOExcep
2120
if (header.getCompressionMethod() != ZipCompressions.DEFLATED)
2221
throw new IOException("LocalFileHeader contents not using 'Deflated'!");
2322
Inflater inflater = new Inflater(true);
24-
ByteArrayOutputStream out = new ByteArrayOutputStream();
23+
FastWrapOutputStream out = new FastWrapOutputStream();
2524
try {
2625
byte[] output = new byte[1024];
2726
byte[] buffer = new byte[1024];
@@ -43,12 +42,12 @@ public ByteData decompress(LocalFileHeader header, ByteData data) throws IOExcep
4342
out.write(output, 0, count);
4443
}
4544
} while (!inflater.finished() && !inflater.needsDictionary());
46-
}catch (DataFormatException e) {
45+
} catch (DataFormatException e) {
4746
String s = e.getMessage();
4847
throw (ZipException) new ZipException(s != null ? null : "Invalid ZLIB data format").initCause(e);
4948
} finally {
5049
inflater.end();
5150
}
52-
return BufferData.wrap(out.toByteArray());
51+
return out.wrap();
5352
}
5453
}

src/main/java/software/coley/llzip/format/compression/UnsafeDeflateDecompressor.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package software.coley.llzip.format.compression;
22

33
import software.coley.llzip.format.model.LocalFileHeader;
4-
import software.coley.llzip.util.BufferData;
54
import software.coley.llzip.util.ByteData;
5+
import software.coley.llzip.util.FastWrapOutputStream;
66
import software.coley.llzip.util.UnsafeInflater;
77

8-
import java.io.ByteArrayOutputStream;
98
import java.io.IOException;
109
import java.util.ArrayDeque;
1110
import java.util.Deque;
@@ -24,7 +23,7 @@ public class UnsafeDeflateDecompressor implements Decompressor {
2423
public ByteData decompress(LocalFileHeader header, ByteData data) throws IOException {
2524
if (header.getCompressionMethod() != ZipCompressions.DEFLATED)
2625
throw new IOException("LocalFileHeader contents not using 'Deflated'!");
27-
ByteArrayOutputStream out = new ByteArrayOutputStream();
26+
FastWrapOutputStream out = new FastWrapOutputStream();
2827
UnsafeInflater inflater;
2928
Deque<UnsafeInflater> inflaters = INFLATERS;
3029
synchronized (inflaters) {
@@ -72,6 +71,6 @@ public ByteData decompress(LocalFileHeader header, ByteData data) throws IOExcep
7271
inflater.end();
7372
}
7473
}
75-
return BufferData.wrap(out.toByteArray());
74+
return out.wrap();
7675
}
7776
}

src/main/java/software/coley/llzip/util/BufferData.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,18 @@ public static BufferData wrap(ByteBuffer buffer) {
153153
public static BufferData wrap(byte[] array) {
154154
return new BufferData(ByteBuffer.wrap(array).order(ByteOrder.LITTLE_ENDIAN), new AtomicBoolean());
155155
}
156+
157+
/**
158+
* @param array
159+
* Byte array to wrap.
160+
* @param offset
161+
* Offset into the array to start at.
162+
* @param length
163+
* Length of content.
164+
*
165+
* @return Buffer data.
166+
*/
167+
public static BufferData wrap(byte[] array, int offset, int length) {
168+
return new BufferData(ByteBuffer.wrap(array, offset, length).order(ByteOrder.LITTLE_ENDIAN), new AtomicBoolean());
169+
}
156170
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package software.coley.llzip.util;
2+
3+
import java.io.ByteArrayOutputStream;
4+
5+
public final class FastWrapOutputStream extends ByteArrayOutputStream {
6+
public BufferData wrap() {
7+
return BufferData.wrap(buf, 0, count);
8+
}
9+
}

0 commit comments

Comments
 (0)