Skip to content

Commit

Permalink
Work around Brotli4j EncoderJNI.Operation permission issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Aug 21, 2024
1 parent 820564a commit 5ba1c5d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package com.aayushatharva.brotli4j.encoder;

/**
* A hack to allow the EncoderJNI.Operation to be seen by Jetty's code.
*
* Remove this and use the EncoderJNI.Operation once the fix at brotli4j is released.
*
* See <a href="https://github.com/hyperxpro/Brotli4j/issues/169">Request release of brotli4j</a>
*/
public enum EncoderJNIOp
{
PROCESS(EncoderJNI.Operation.PROCESS),
FLUSH(EncoderJNI.Operation.FLUSH),
FINISH(EncoderJNI.Operation.FINISH);

private final EncoderJNI.Operation op;

private EncoderJNIOp(EncoderJNI.Operation op)
{
this.op = op;
}

public EncoderJNI.Operation getOp()
{
return op;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.aayushatharva.brotli4j.encoder.Encoder;
import com.aayushatharva.brotli4j.encoder.EncoderJNI;
import com.aayushatharva.brotli4j.encoder.EncoderJNIOp;
import org.eclipse.jetty.compression.EncoderSink;
import org.eclipse.jetty.io.Content;
import org.eclipse.jetty.util.BufferUtil;
Expand Down Expand Up @@ -106,7 +107,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content)
// only encode if inputBuffer is full.
if (!inputBuffer.hasRemaining())
{
ByteBuffer output = encode(EncoderJNI.Operation.PROCESS);
ByteBuffer output = encode(EncoderJNIOp.PROCESS);
if (output != null)
return new WriteRecord(false, output, Callback.NOOP);
}
Expand All @@ -128,15 +129,15 @@ protected WriteRecord encode(boolean last, ByteBuffer content)
case FLUSHING ->
{
inputBuffer.limit(inputBuffer.position());
ByteBuffer output = encode(EncoderJNI.Operation.FLUSH);
ByteBuffer output = encode(EncoderJNIOp.FLUSH);
state.compareAndSet(State.FLUSHING, State.FINISHING);
if (output != null)
return new WriteRecord(false, output, Callback.NOOP);
}
case FINISHING ->
{
inputBuffer.limit(inputBuffer.position());
ByteBuffer output = encode(EncoderJNI.Operation.FINISH);
ByteBuffer output = encode(EncoderJNIOp.FINISH);
state.compareAndSet(State.FINISHING, State.FINISHED);
return new WriteRecord(true, output != null ? output : EMPTY_BUFFER, Callback.NOOP);
}
Expand All @@ -148,7 +149,7 @@ protected WriteRecord encode(boolean last, ByteBuffer content)
}
}

protected ByteBuffer encode(EncoderJNI.Operation op)
protected ByteBuffer encode(EncoderJNIOp op)
{
try
{
Expand All @@ -167,11 +168,11 @@ else if (encoder.hasMoreOutput())
}
else if (encoder.hasRemainingInput())
{
encoder.push(op, 0);
encoder.push(op.getOp(), 0);
}
else if (!inputPushed)
{
encoder.push(op, inputBuffer.limit());
encoder.push(op.getOp(), inputBuffer.limit());
inputPushed = true;
}
else
Expand Down

0 comments on commit 5ba1c5d

Please sign in to comment.