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      @Override
59      public int produceContent(final ContentEncoder encoder) throws IOException {
60          setOutputMode();
61          final int bytesWritten = encoder.write(this.buffer);
62          if (!hasData() && this.endOfStream) {
63              encoder.complete();
64          }
65          return bytesWritten;
66      }
67  
68      @Override
69      public void write(final byte[] b, final int off, final int len) throws IOException {
70          if (b == null) {
71              return;
72          }
73          if (this.endOfStream) {
74              return;
75          }
76          setInputMode();
77          ensureCapacity(this.buffer.position() + len);
78          this.buffer.put(b, off, len);
79      }
80  
81      public void write(final byte[] b) throws IOException {
82          if (b == null) {
83              return;
84          }
85          if (this.endOfStream) {
86              return;
87          }
88          write(b, 0, b.length);
89      }
90  
91      @Override
92      public void write(final int b) throws IOException {
93          if (this.endOfStream) {
94              return;
95          }
96          setInputMode();
97          ensureCapacity(this.capacity() + 1);
98          this.buffer.put((byte)b);
99      }
100 
101     @Override
102     public void reset() {
103         super.clear();
104         this.endOfStream = false;
105     }
106 
107     @Override
108     public void flush() {
109     }
110 
111     @Override
112     public void writeCompleted() {
113         this.endOfStream = true;
114     }
115 
116     public void shutdown() {
117         this.endOfStream = true;
118     }
119 
120 }