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(int buffersize, final ByteBufferAllocator allocator) {
47          super(buffersize, allocator);
48          this.endOfStream = false;
49      }
50  
51      public int produceContent(final ContentEncoder encoder) throws IOException {
52          setOutputMode();
53          int bytesWritten = encoder.write(this.buffer);
54          if (!hasData() && this.endOfStream) {
55              encoder.complete();
56          }
57          return bytesWritten;
58      }
59  
60      public void write(final byte[] b, int off, int len) throws IOException {
61          if (b == null) {
62              return;
63          }
64          if (this.endOfStream) {
65              return;
66          }
67          setInputMode();
68          ensureCapacity(this.buffer.position() + len);
69          this.buffer.put(b, off, len);
70      }
71  
72      public void write(final byte[] b) throws IOException {
73          if (b == null) {
74              return;
75          }
76          if (this.endOfStream) {
77              return;
78          }
79          write(b, 0, b.length);
80      }
81  
82      public void write(int b) throws IOException {
83          if (this.endOfStream) {
84              return;
85          }
86          setInputMode();
87          ensureCapacity(this.capacity() + 1);
88          this.buffer.put((byte)b);
89      }
90  
91      public void reset() {
92          super.clear();
93          this.endOfStream = false;
94      }
95  
96      public void flush() {
97      }
98  
99      public void writeCompleted() {
100         this.endOfStream = true;
101     }
102 
103     public void shutdown() {
104         this.endOfStream = true;
105     }
106 
107 }