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
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,16 @@ public Content.Chunk read()
try (AutoLock ignored = lock.lock())
{
lockedEnsureOpenOrTerminal();

if (_terminal != null)
return _terminal;

if (_length == 0)
{
lockedSetTerminal(Content.Chunk.EOF);
return Content.Chunk.EOF;
}

if (_buffer == null)
{
_buffer = _byteBufferPool.acquire();
Expand All @@ -172,7 +179,7 @@ else if (_buffer.isRetained())
{
ByteBuffer byteBuffer = _buffer.getByteBuffer();
BufferUtil.clearToFill(byteBuffer);
if (_length >= 0)
if (_length > 0)
byteBuffer.limit((int)Math.min(_buffer.capacity(), _length - _totalRead));
int read = _byteChannel.read(byteBuffer);
BufferUtil.flipToFlush(byteBuffer, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ public Content.Source newContentSource(ByteBufferPool.Sized bufferPool, long off
public static Stream<Resource> all() throws Exception
{
Path testResourcePath = MavenTestingUtils.getTestResourcePath("keystore.p12");

URI resourceUri = testResourcePath.toUri();
return Stream.of(
ResourceFactory.root().newResource(resourceUri),
ResourceFactory.root().newMemoryResource(resourceUri.toURL()),
ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("zero")),
ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("one")),
new URLResourceFactory().newResource(resourceUri),
new TestContentSourceFactoryResource(resourceUri, Files.readAllBytes(testResourcePath))
);
Expand Down Expand Up @@ -181,12 +184,13 @@ public void testAsContentSourceWithFirst(Resource resource) throws Exception
{
TestSink sink = new TestSink();
Callback.Completable callback = new Callback.Completable();

Content.Source contentSource = IOResources.asContentSource(resource, bufferPool, 100, -1);
Content.copy(contentSource, sink, callback);
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(resource.length() - 100L));
assertThat(sum, is(Math.max(0L, resource.length() - 100L)));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand All @@ -201,7 +205,7 @@ public void testAsContentSourceWithLength(Resource resource) throws Exception
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(500L));
assertThat(sum, is(Math.min(resource.length(), 500L)));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand All @@ -211,12 +215,15 @@ public void testAsContentSourceWithFirstAndLength(Resource resource) throws Exce
{
TestSink sink = new TestSink();
Callback.Completable callback = new Callback.Completable();
Content.Source contentSource = IOResources.asContentSource(resource, bufferPool, 100, 500);

long offset = Math.min(resource.length(), 100);
long length = Math.min(resource.length() - offset, 500);
Content.Source contentSource = IOResources.asContentSource(resource, bufferPool, offset, length);
Content.copy(contentSource, sink, callback);
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(500L));
assertThat(sum, is(length));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand All @@ -240,11 +247,12 @@ public void testCopyWithFirst(Resource resource) throws Exception
{
TestSink sink = new TestSink();
Callback.Completable callback = new Callback.Completable();
IOResources.copy(resource, sink, bufferPool, 100, -1, callback);
long offset = Math.min(resource.length(), 100);
IOResources.copy(resource, sink, bufferPool, offset, -1, callback);
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(resource.length() - 100L));
assertThat(sum, is(Math.max(0L, resource.length() - 100L)));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand All @@ -254,11 +262,12 @@ public void testCopyWithLength(Resource resource) throws Exception
{
TestSink sink = new TestSink();
Callback.Completable callback = new Callback.Completable();
IOResources.copy(resource, sink, bufferPool, 0, 500, callback);
long length = resource.length() >= 0 ? Math.min(resource.length(), 500) : 500;
IOResources.copy(resource, sink, bufferPool, 0, length, callback);
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(500L));
assertThat(sum, is(length));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand All @@ -268,11 +277,13 @@ public void testCopyWithFirstAndLength(Resource resource) throws Exception
{
TestSink sink = new TestSink();
Callback.Completable callback = new Callback.Completable();
IOResources.copy(resource, sink, bufferPool, 100, 500, callback);
long offset = Math.min(resource.length(), 100);
long length = Math.min(resource.length() - offset, 500);
IOResources.copy(resource, sink, bufferPool, offset, length, callback);
callback.get();
List<Content.Chunk> chunks = sink.takeAccumulatedChunks();
long sum = chunks.stream().mapToLong(Content.Chunk::remaining).sum();
assertThat(sum, is(500L));
assertThat(sum, is(length));
assertThat(chunks.get(chunks.size() - 1).isLast(), is(true));
}

Expand Down
1 change: 1 addition & 0 deletions jetty-core/jetty-io/src/test/resources/one
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Empty file.