View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.nio.util;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.nio.ContentEncoder;
32  
33  /**
34   * Basic implementation of the {@link ContentOutputBuffer} interface.
35   * <p>
36   * This class is not thread safe.
37   *
38   * @since 4.0
39   */
40  public class SimpleOutputBuffer extends ExpandableBuffer implements ContentOutputBuffer {
41  
42      private boolean endOfStream;
43  
44      public SimpleOutputBuffer(final int buffersize, final ByteBufferAllocator allocator) {
45          super(buffersize, allocator);
46          this.endOfStream = false;
47      }
48  
49      /**
50       * @since 4.3
51       */
52      public SimpleOutputBuffer(final int buffersize) {
53          this(buffersize, HeapByteBufferAllocator.INSTANCE);
54      }
55  
56      @Override
57      public int produceContent(final ContentEncoder encoder) throws IOException {
58          setOutputMode();
59          final int bytesWritten = encoder.write(this.buffer);
60          if (!hasData() && this.endOfStream) {
61              encoder.complete();
62          }
63          return bytesWritten;
64      }
65  
66      @Override
67      public void write(final byte[] b, final int off, final int len) throws IOException {
68          if (b == null) {
69              return;
70          }
71          if (this.endOfStream) {
72              return;
73          }
74          setInputMode();
75          ensureCapacity(this.buffer.position() + len);
76          this.buffer.put(b, off, len);
77      }
78  
79      public void write(final byte[] b) throws IOException {
80          if (b == null) {
81              return;
82          }
83          if (this.endOfStream) {
84              return;
85          }
86          write(b, 0, b.length);
87      }
88  
89      @Override
90      public void write(final int b) throws IOException {
91          if (this.endOfStream) {
92              return;
93          }
94          setInputMode();
95          ensureCapacity(this.capacity() + 1);
96          this.buffer.put((byte)b);
97      }
98  
99      @Override
100     public void reset() {
101         super.clear();
102         this.endOfStream = false;
103     }
104 
105     @Override
106     public void flush() {
107     }
108 
109     @Override
110     public void writeCompleted() {
111         this.endOfStream = true;
112     }
113 
114     public void shutdown() {
115         this.endOfStream = true;
116     }
117 
118 }