Skip to content

Commit

Permalink
Fix multithreading issue on GzipChannel write header
Browse files Browse the repository at this point in the history
ByteBuffers are not thread-safe so give each instance its own copy of the gzip header buffer. Fixes #69.

Reported-by: sorin.c
  • Loading branch information
ato committed Sep 28, 2023
1 parent f1f8470 commit 68575d4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/org/netpreserve/jwarc/GzipChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class GzipChannel implements WritableByteChannel {
static final int CM_DEFLATE = Deflater.DEFLATED;

/** Default gzip header, 10 bytes long */
private static final byte[] GZIP_HEADER_ = new byte[] { //
private static final byte[] GZIP_HEADER = new byte[] { //
(byte) GZIP_MAGIC, //
(byte) (GZIP_MAGIC >> 8), //
Deflater.DEFLATED, //
0, 0, 0, 0, 0, 0, 0 };
private static final ByteBuffer GZIP_HEADER = ByteBuffer.wrap(GZIP_HEADER_);

private final ByteBuffer gzipHeader = ByteBuffer.wrap(GZIP_HEADER);
private boolean headerWritten = false;
private boolean finished = false;
private boolean dataWritten = false;
Expand Down Expand Up @@ -66,8 +66,8 @@ private void checkStatus(boolean finish) throws IOException {
}

private void writeHeader() throws IOException {
outputPosition += channel.write(GZIP_HEADER);
GZIP_HEADER.rewind();
outputPosition += channel.write(gzipHeader);
gzipHeader.rewind();
headerWritten = true;
}

Expand Down

0 comments on commit 68575d4

Please sign in to comment.