1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
35
36
37
38
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
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
110 @Override
111 public void writeCompleted() {
112 this.endOfStream = true;
113 }
114
115 public void shutdown() {
116 this.endOfStream = true;
117 }
118
119 }