Skip to content

Commit

Permalink
#8993: make the EOF / EMPTY decision in Content.Chunk.from()
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Dec 6, 2022
1 parent 41345d3 commit c4a928c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jetty.http2.client.transport.internal;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -81,21 +80,10 @@ public Content.Chunk read(boolean fillInterestIfNeeded)
return null;
}
DataFrame frame = data.frame();
ByteBuffer buffer = frame.getData();
if (!buffer.hasRemaining())
{
data.release();
if (frame.isEndStream())
{
responseSuccess(getHttpExchange(), null);
return Content.Chunk.EOF;
}
else
{
return Content.Chunk.EMPTY;
}
}
return Content.Chunk.from(buffer, false, data);
boolean last = frame.remaining() == 0 && frame.isEndStream();
if (last)
responseSuccess(getHttpExchange(), null);
return Content.Chunk.from(frame.getData(), last, data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,11 @@ public Runnable onTrailer(HeadersFrame frame)
private Content.Chunk createChunk(Stream.Data data)
{
DataFrame frame = data.frame();
ByteBuffer byteBuffer = frame.getData();
if (!byteBuffer.hasRemaining())
{
// We must NOT retain because we are not passing the
// ByteBuffer to the Chunk.
if (frame.isEndStream())
return Content.Chunk.EOF;
else
return Content.Chunk.EMPTY;
}
if (frame.isEndStream() && frame.remaining() == 0)
return Content.Chunk.EOF;
// We need to retain because we are passing the ByteBuffer to the Chunk.
data.retain();
return Content.Chunk.from(byteBuffer, frame.isEndStream(), data);
return Content.Chunk.from(frame.getData(), frame.isEndStream(), data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,10 @@ public Content.Chunk read(boolean fillInterestIfNeeded)
return null;
}
ByteBuffer byteBuffer = data.getByteBuffer();
if (!byteBuffer.hasRemaining())
{
data.release();
if (data.isLast())
{
responseSuccess(getHttpExchange(), null);
return Content.Chunk.EOF;
}
else
{
return Content.Chunk.EMPTY;
}
}
return Content.Chunk.from(byteBuffer, false, data);
boolean last = !byteBuffer.hasRemaining() && data.isLast();
if (last)
responseSuccess(getHttpExchange(), null);
return Content.Chunk.from(byteBuffer, last, data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,19 +233,12 @@ public Runnable onTrailer(HeadersFrame frame)

private Content.Chunk createChunk(Stream.Data data)
{
ByteBuffer byteBuffer = data.getByteBuffer();
if (!byteBuffer.hasRemaining())
{
// As we are NOT passing the ByteBuffer to the Chunk,
// we must not retain.
if (data.isLast())
return Content.Chunk.EOF;
else
return Content.Chunk.EMPTY;
}
if (data == Stream.Data.EOF)
return Content.Chunk.EOF;

// As we are passing the ByteBuffer to the Chunk we need to retain.
data.retain();
return Content.Chunk.from(byteBuffer, data.isLast(), data);
return Content.Chunk.from(data.getByteBuffer(), data.isLast(), data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ public interface Chunk extends Retainable
*/
static Chunk from(ByteBuffer byteBuffer, boolean last)
{
if (!byteBuffer.hasRemaining())
throw new IllegalArgumentException("Empty ByteBuffer instances are forbidden, use the EOF or EMPTY constant instead.");
return new ByteBufferChunk.WithReferenceCount(byteBuffer, last);
}

Expand All @@ -459,6 +461,11 @@ static Chunk from(ByteBuffer byteBuffer, boolean last)
*/
static Chunk from(ByteBuffer byteBuffer, boolean last, Runnable releaser)
{
if (!byteBuffer.hasRemaining())
{
releaser.run();
return last ? EOF : EMPTY;
}
return new ByteBufferChunk.ReleasedByRunnable(byteBuffer, last, Objects.requireNonNull(releaser));
}

Expand All @@ -475,6 +482,11 @@ static Chunk from(ByteBuffer byteBuffer, boolean last, Runnable releaser)
*/
static Chunk from(ByteBuffer byteBuffer, boolean last, Consumer<ByteBuffer> releaser)
{
if (!byteBuffer.hasRemaining())
{
releaser.accept(byteBuffer);
return last ? EOF : EMPTY;
}
return new ByteBufferChunk.ReleasedByConsumer(byteBuffer, last, Objects.requireNonNull(releaser));
}

Expand All @@ -492,6 +504,11 @@ static Chunk from(ByteBuffer byteBuffer, boolean last, Consumer<ByteBuffer> rele
*/
static Chunk from(ByteBuffer byteBuffer, boolean last, Retainable retainable)
{
if (!byteBuffer.hasRemaining())
{
retainable.release();
return last ? EOF : EMPTY;
}
return new ByteBufferChunk.WithRetainable(byteBuffer, last, Objects.requireNonNull(retainable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@

public abstract class ByteBufferChunk implements Content.Chunk
{
public static final ByteBufferChunk EMPTY = new ByteBufferChunk(true, BufferUtil.EMPTY_BUFFER, false)
public static final ByteBufferChunk EMPTY = new ByteBufferChunk(BufferUtil.EMPTY_BUFFER, false)
{
@Override
public String toString()
{
return "%s[EMPTY]".formatted(ByteBufferChunk.class.getSimpleName());
}
};
public static final ByteBufferChunk EOF = new ByteBufferChunk(true, BufferUtil.EMPTY_BUFFER, true)
public static final ByteBufferChunk EOF = new ByteBufferChunk(BufferUtil.EMPTY_BUFFER, true)
{
@Override
public String toString()
Expand All @@ -46,13 +46,6 @@ public String toString()

public ByteBufferChunk(ByteBuffer byteBuffer, boolean last)
{
this(false, byteBuffer, last);
}

private ByteBufferChunk(boolean isConstant, ByteBuffer byteBuffer, boolean last)
{
if (!isConstant && !byteBuffer.hasRemaining() && last)
throw new IllegalArgumentException("Empty ByteBuffer instances with last=true are forbidden, use the EOF constant instead.");
this.byteBuffer = Objects.requireNonNull(byteBuffer);
this.last = last;
}
Expand Down

0 comments on commit c4a928c

Please sign in to comment.