* Added ChunkedInput.isEndOfInput() and changed the contract of ChunkedInput.nextChunk() and hasNextChunk() to support slow streams

This commit is contained in:
Trustin Lee 2010-02-18 04:24:41 +00:00
parent 23b543c4ba
commit 4ef4a92281
6 changed files with 31 additions and 6 deletions

View File

@ -129,6 +129,10 @@ public class ChunkedFile implements ChunkedInput {
return offset < endOffset && file.getChannel().isOpen();
}
public boolean isEndOfInput() throws Exception {
return hasNextChunk();
}
public void close() throws Exception {
file.close();
}

View File

@ -30,7 +30,9 @@ public interface ChunkedInput {
/**
* Returns {@code true} if and only if there is any data left in the
* stream.
* stream. Please note that {@code false} does not necessarily mean that
* the stream has reached at its end. In a slow stream, the next chunk
* might be unavailable just momentarily.
*/
boolean hasNextChunk() throws Exception;
@ -42,9 +44,18 @@ public interface ChunkedInput {
*
* @return the fetched chunk, which is usually {@link ChannelBuffer}.
* {@code null} if there is no data left in the stream.
* Please note that {@code null} does not necessarily mean that the
* stream has reached at its end. In a slow stream, the next chunk
* might be unavailable just momentarily.
*/
Object nextChunk() throws Exception;
/**
* Return {@code true} if and only if there is no data left in the stream
* and the stream has reached at its end.
*/
boolean isEndOfInput() throws Exception;
/**
* Releases the resources associated with the stream.
*/

View File

@ -135,6 +135,10 @@ public class ChunkedNioFile implements ChunkedInput {
return offset < endOffset && in.isOpen();
}
public boolean isEndOfInput() throws Exception {
return hasNextChunk();
}
public void close() throws Exception {
in.close();
}

View File

@ -96,6 +96,10 @@ public class ChunkedNioStream implements ChunkedInput {
return false;
}
public boolean isEndOfInput() throws Exception {
return hasNextChunk();
}
public void close() throws Exception {
in.close();
}

View File

@ -84,6 +84,10 @@ public class ChunkedStream implements ChunkedInput {
}
}
public boolean isEndOfInput() throws Exception {
return hasNextChunk();
}
public void close() throws Exception {
in.close();
}

View File

@ -171,15 +171,13 @@ public class ChunkedWriteHandler implements ChannelUpstreamHandler, ChannelDowns
if (m instanceof ChunkedInput) {
ChunkedInput chunks = (ChunkedInput) m;
Object chunk;
boolean last;
boolean endOfInput;
try {
chunk = chunks.nextChunk();
if (chunk == null) {
chunk = ChannelBuffers.EMPTY_BUFFER;
last = true;
} else {
last = !chunks.hasNextChunk();
}
endOfInput = chunks.isEndOfInput();
} catch (Throwable t) {
MessageEvent currentEvent = this.currentEvent;
this.currentEvent = null;
@ -193,7 +191,7 @@ public class ChunkedWriteHandler implements ChannelUpstreamHandler, ChannelDowns
ChannelFuture writeFuture;
final MessageEvent currentEvent = this.currentEvent;
if (last) {
if (endOfInput) {
this.currentEvent = null;
closeInput(chunks);
writeFuture = currentEvent.getFuture();