Fixed newly introduced bug where by a blocking channel deadlocks because all content was read, but we attempted to read more anyway.
This commit is contained in:
@@ -127,15 +127,36 @@ public int get(ByteBuffer buffer) {
|
|||||||
}//else//
|
}//else//
|
||||||
}//if//
|
}//if//
|
||||||
else {
|
else {
|
||||||
|
boolean readFromChannel = this.buffer.remaining() == 0;
|
||||||
|
|
||||||
//Lazily create a buffer if necessary.//
|
//Lazily create a buffer if necessary.//
|
||||||
if(this.buffer == null) {
|
if(this.buffer == null) {
|
||||||
this.buffer = ByteBuffer.allocate(2000);
|
this.buffer = ByteBuffer.allocate(2000);
|
||||||
}//if//
|
}//if//
|
||||||
|
|
||||||
//Read as many bytes from the channel as possible.//
|
//If there are bytes remaining in this.buffer AND we are about to read a new chunk header THEN figure out if we have at least enough bytes to read the chunk header (don't read more from the stream if true - could cause a deadlock, otherwise read more since there must be more to read).//
|
||||||
this.buffer.compact();
|
if(!readFromChannel && chunkSize == 0) {
|
||||||
channel.read(this.buffer);
|
StringBuffer chunkHeader = new StringBuffer(100);
|
||||||
this.buffer.flip();
|
this.buffer.mark();
|
||||||
|
|
||||||
|
//Read the next chunk header.//
|
||||||
|
while(this.buffer.hasRemaining() && (chunkHeader.length() < 2 || !(chunkHeader.charAt(chunkHeader.length() - 2) == '\r' && chunkHeader.charAt(chunkHeader.length() - 1) == '\n'))) {
|
||||||
|
chunkHeader.append((char) this.buffer.get());
|
||||||
|
}//while//
|
||||||
|
|
||||||
|
this.buffer.reset();
|
||||||
|
|
||||||
|
//If a chunk header was not readable then do read from the channel.//
|
||||||
|
readFromChannel = chunkHeader.length() < 2 || !(chunkHeader.charAt(chunkHeader.length() - 2) == '\r' && chunkHeader.charAt(chunkHeader.length() - 1) == '\n');
|
||||||
|
}//if//
|
||||||
|
|
||||||
|
//Only attempt a read IF there are no bytes in this.buffer OR we are reading the next chunk header and there are insufficient bytes in this.buffer to do so.//
|
||||||
|
if(readFromChannel) {
|
||||||
|
//Read as many bytes from the channel as possible.//
|
||||||
|
this.buffer.compact();
|
||||||
|
channel.read(this.buffer);
|
||||||
|
this.buffer.flip();
|
||||||
|
}//if//
|
||||||
|
|
||||||
//Check to see if the chunk ends within this buffer.//
|
//Check to see if the chunk ends within this buffer.//
|
||||||
if(this.buffer.remaining() > chunkSize) {
|
if(this.buffer.remaining() > chunkSize) {
|
||||||
|
|||||||
Reference in New Issue
Block a user