Skip to content

Commit 26147aa

Browse files
committed
Cleaner logic
1 parent bfff110 commit 26147aa

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/main/java/com/emc/mongoose/storage/driver/coop/netty/data/PartialChunkedNioStream.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ private PartialChunkedNioStream(ReadableByteChannel in, long sizeToTransfer) {
3939

4040
@Override
4141
public final boolean isEndOfInput() {
42-
// Offset may exceed size when size is not a multiple of chunk size
43-
return offset >= sizeToTransfer;
42+
return offset == sizeToTransfer;
4443
}
4544

4645
@Override
@@ -60,33 +59,35 @@ public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
6059
return null;
6160
}
6261

63-
// Allow for partial chunk write
64-
int writeBytes = (int) Math.min(chunkSize, sizeToTransfer - offset);
62+
int nextChunkSize = chunkSize;
63+
int bytesRemaining = (int) (length() - progress());
64+
65+
// Is there less than a chunk size of data remaining?
66+
if (bytesRemaining < chunkSize) {
67+
// Limit the byte buffer and next chunk size
68+
byteBuffer.limit(bytesRemaining);
69+
nextChunkSize = bytesRemaining;
70+
}
6571

6672
// Should be empty
6773
int readBytes = byteBuffer.position();
6874

69-
// Read a whole chunk
75+
// Read the chunk
7076
while (true) {
7177
int localReadBytes = in.read(byteBuffer);
7278
if (localReadBytes < 0) {
7379
break;
7480
}
7581
readBytes += localReadBytes;
7682
offset += localReadBytes;
77-
if (readBytes == chunkSize) {
83+
if (readBytes == nextChunkSize) {
7884
break;
7985
}
8086
}
8187

8288
byteBuffer.flip();
8389
boolean release = true;
84-
ByteBuf buffer = allocator.buffer(writeBytes);
85-
86-
// Re-position the byte buffer for the partial chunk write
87-
if (writeBytes < chunkSize) {
88-
byteBuffer.position(chunkSize - writeBytes);
89-
}
90+
ByteBuf buffer = allocator.buffer(byteBuffer.remaining());
9091

9192
// Write the chunk
9293
try {

0 commit comments

Comments
 (0)