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.annotation.NotThreadSafe;
32  import org.apache.http.nio.ContentEncoder;
33  
34  /**
35   * Basic implementation of the {@link ContentOutputBuffer} interface.
36   * <p>
37   * This class is not thread safe.
38   *
39   * @since 4.0
40   */
41  @NotThreadSafe
42  public class SimpleOutputBuffer extends ExpandableBuffer implements ContentOutputBuffer {
43  
44      private boolean endOfStream;
45  
46      public SimpleOutputBuffer(final int buffersize, final ByteBufferAllocator allocator) {
47          super(buffersize, allocator);
48          this.endOfStream = false;
49      }
50  
51      /**
52       * @since 4.3
53       */
54      public SimpleOutputBuffer(final int buffersize) {
55          this(buffersize, HeapByteBufferAllocator.INSTANCE);
56      }
57  
58      public int produceContent(final ContentEncoder encoder) throws IOException {
59          setOutputMode();
60          final int bytesWritten = encoder.write(this.buffer);
61          if (!hasData() && this.endOfStream) {
62              encoder.complete();
63          }
64          return bytesWritten;
65      }
66  
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      public void write(final int b) throws IOException {
90          if (this.endOfStream) {
91              return;
92          }
93          setInputMode();
94          ensureCapacity(this.capacity() + 1);
95          this.buffer.put((byte)b);
96      }
97  
98      public void reset() {
99          super.clear();
100         this.endOfStream = false;
101     }
102 
103     public void flush() {
104     }
105 
106     public void writeCompleted() {
107         this.endOfStream = true;
108     }
109 
110     public void shutdown() {
111         this.endOfStream = true;
112     }
113 
114 }