A copy of the SharedInputBuffer implementation of Apache HttpComponents - HttpCore/NIO
found at http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-nio/
src/main/java/org/apache/http/nio/util/SharedInputBuffer.java
To include the fix described here : http://svn.apache.org/viewvc/httpcomponents/httpcore/
trunk/module-nio/src/main/java/org/apache/http/nio/util/SharedInputBuffer.java
?view=diff&r1=659956&r2=659957&pathrev=659957
with the HttpCore version 4.0-beta1
TODO : This class to be removed as soon as we update the HttpCore dependency from 4.0-beta1
Method from org.apache.synapse.transport.nhttp.util.SharedInputBuffer Detail: |
public int consumeContent(ContentDecoder decoder) throws IOException {
if (this.shutdown) {
return -1;
}
synchronized (this.mutex) {
setInputMode();
int totalRead = 0;
int bytesRead;
while ((bytesRead = decoder.read(this.buffer)) > 0) {
totalRead += bytesRead;
}
if (bytesRead == -1 || decoder.isCompleted()) {
this.endOfStream = true;
}
if (!this.buffer.hasRemaining()) {
this.ioctrl.suspendInput();
}
this.mutex.notifyAll();
if (totalRead > 0) {
return totalRead;
} else {
if (this.endOfStream) {
return -1;
} else {
return 0;
}
}
}
}
|
protected boolean isEndOfStream() {
return this.shutdown || (!hasData() && this.endOfStream);
}
|
protected boolean isShutdown() {
return this.shutdown;
}
|
public int read() throws IOException {
if (this.shutdown) {
return -1;
}
synchronized (this.mutex) {
if (!hasData()) {
waitForData();
}
if (isEndOfStream()) {
return -1;
}
return this.buffer.get() & 0xff;
}
}
|
public int read(byte[] b) throws IOException {
if (this.shutdown) {
return -1;
}
if (b == null) {
return 0;
}
return read(b, 0, b.length);
}
|
public int read(byte[] b,
int off,
int len) throws IOException {
if (this.shutdown) {
return -1;
}
if (b == null) {
return 0;
}
synchronized (this.mutex) {
if (!hasData()) {
waitForData();
}
if (isEndOfStream()) {
return -1;
}
setOutputMode();
int chunk = len;
if (chunk > this.buffer.remaining()) {
chunk = this.buffer.remaining();
}
this.buffer.get(b, off, chunk);
return chunk;
}
}
|
public void reset() {
if (this.shutdown) {
return;
}
synchronized (this.mutex) {
clear();
this.endOfStream = false;
}
}
|
public void shutdown() {
if (this.shutdown) {
return;
}
this.shutdown = true;
synchronized (this.mutex) {
this.mutex.notifyAll();
}
}
|
protected void waitForData() throws IOException {
synchronized (this.mutex) {
try {
while (!hasData() && !this.endOfStream) {
if (this.shutdown) {
throw new InterruptedIOException("Input operation aborted");
}
this.ioctrl.requestInput();
this.mutex.wait();
}
} catch (InterruptedException ex) {
throw new IOException("Interrupted while waiting for more data");
}
}
}
|