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/SimpleOutputBuffer.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.ContentEncoder;
036:
037: public class SimpleOutputBuffer extends ExpandableBuffer implements
038: ContentOutputBuffer {
039:
040: private boolean endOfStream;
041:
042: public SimpleOutputBuffer(int buffersize,
043: final ByteBufferAllocator allocator) {
044: super (buffersize, allocator);
045: this .endOfStream = false;
046: }
047:
048: public int produceContent(final ContentEncoder encoder)
049: throws IOException {
050: setOutputMode();
051: int bytesWritten = encoder.write(this .buffer);
052: if (!hasData() && this .endOfStream) {
053: encoder.complete();
054: }
055: return bytesWritten;
056: }
057:
058: public void write(final byte[] b, int off, int len)
059: throws IOException {
060: if (b == null) {
061: return;
062: }
063: if (this .endOfStream) {
064: return;
065: }
066: setInputMode();
067: ensureCapacity(this .buffer.position() + len);
068: this .buffer.put(b, off, len);
069: }
070:
071: public void write(final byte[] b) throws IOException {
072: if (b == null) {
073: return;
074: }
075: if (this .endOfStream) {
076: return;
077: }
078: write(b, 0, b.length);
079: }
080:
081: public void write(int b) throws IOException {
082: if (this .endOfStream) {
083: return;
084: }
085: setInputMode();
086: ensureCapacity(this .capacity() + 1);
087: this .buffer.put((byte) b);
088: }
089:
090: public void reset() {
091: super .clear();
092: this .endOfStream = false;
093: }
094:
095: public void flush() {
096: }
097:
098: public void writeCompleted() {
099: this .endOfStream = true;
100: }
101:
102: public void shutdown() {
103: this .endOfStream = true;
104: }
105:
106: }
|