001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/nio/util/SimpleInputBuffer.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031: package org.apache.http.nio.util;
032:
033: import java.io.IOException;
034:
035: import org.apache.http.nio.ContentDecoder;
036:
037: public class SimpleInputBuffer extends ExpandableBuffer implements
038: ContentInputBuffer {
039:
040: private boolean endOfStream = false;
041:
042: public SimpleInputBuffer(int buffersize,
043: final ByteBufferAllocator allocator) {
044: super (buffersize, allocator);
045: }
046:
047: public void reset() {
048: this .endOfStream = false;
049: super .clear();
050: }
051:
052: public int consumeContent(final ContentDecoder decoder)
053: throws IOException {
054: setInputMode();
055: int totalRead = 0;
056: int bytesRead;
057: while ((bytesRead = decoder.read(this .buffer)) != -1) {
058: if (bytesRead == 0) {
059: if (!this .buffer.hasRemaining()) {
060: expand();
061: } else {
062: break;
063: }
064: } else {
065: totalRead += bytesRead;
066: }
067: }
068: if (bytesRead == -1 || decoder.isCompleted()) {
069: this .endOfStream = true;
070: }
071: return totalRead;
072: }
073:
074: public boolean isEndOfStream() {
075: return !hasData() && this .endOfStream;
076: }
077:
078: public int read() throws IOException {
079: if (isEndOfStream()) {
080: return -1;
081: }
082: return this .buffer.get() & 0xff;
083: }
084:
085: public int read(final byte[] b, int off, int len)
086: throws IOException {
087: if (isEndOfStream()) {
088: return -1;
089: }
090: if (b == null) {
091: return 0;
092: }
093: setOutputMode();
094: int chunk = len;
095: if (chunk > this .buffer.remaining()) {
096: chunk = this .buffer.remaining();
097: }
098: this .buffer.get(b, off, chunk);
099: return chunk;
100: }
101:
102: public int read(final byte[] b) throws IOException {
103: if (isEndOfStream()) {
104: return -1;
105: }
106: if (b == null) {
107: return 0;
108: }
109: return read(b, 0, b.length);
110: }
111:
112: public void shutdown() {
113: }
114:
115: }
|