1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.buffer;
17
18 import io.netty.util.CharsetUtil;
19 import io.netty.util.internal.ObjectUtil;
20
21 import java.io.DataOutput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class ByteBufOutputStream extends OutputStream implements DataOutput {
40
41 private final ByteBuf buffer;
42 private final int startIndex;
43 private DataOutputStream utf8out;
44 private boolean closed;
45 private final boolean releaseOnClose;
46
47
48
49
50 public ByteBufOutputStream(ByteBuf buffer) {
51 this(buffer, false);
52 }
53
54
55
56
57
58
59
60
61 public ByteBufOutputStream(ByteBuf buffer, boolean releaseOnClose) {
62 this.releaseOnClose = releaseOnClose;
63 this.buffer = ObjectUtil.checkNotNull(buffer, "buffer");
64 startIndex = buffer.writerIndex();
65 }
66
67
68
69
70 public int writtenBytes() {
71 return buffer.writerIndex() - startIndex;
72 }
73
74 @Override
75 public void write(byte[] b, int off, int len) throws IOException {
76 if (len == 0) {
77 return;
78 }
79
80 buffer.writeBytes(b, off, len);
81 }
82
83 @Override
84 public void write(byte[] b) throws IOException {
85 buffer.writeBytes(b);
86 }
87
88 @Override
89 public void write(int b) throws IOException {
90 buffer.writeByte(b);
91 }
92
93 @Override
94 public void writeBoolean(boolean v) throws IOException {
95 buffer.writeBoolean(v);
96 }
97
98 @Override
99 public void writeByte(int v) throws IOException {
100 buffer.writeByte(v);
101 }
102
103 @Override
104 public void writeBytes(String s) throws IOException {
105 buffer.writeCharSequence(s, CharsetUtil.US_ASCII);
106 }
107
108 @Override
109 public void writeChar(int v) throws IOException {
110 buffer.writeChar(v);
111 }
112
113 @Override
114 public void writeChars(String s) throws IOException {
115 int len = s.length();
116 for (int i = 0; i < len; i++) {
117 buffer.writeChar(s.charAt(i));
118 }
119 }
120
121 @Override
122 public void writeDouble(double v) throws IOException {
123 buffer.writeDouble(v);
124 }
125
126 @Override
127 public void writeFloat(float v) throws IOException {
128 buffer.writeFloat(v);
129 }
130
131 @Override
132 public void writeInt(int v) throws IOException {
133 buffer.writeInt(v);
134 }
135
136 @Override
137 public void writeLong(long v) throws IOException {
138 buffer.writeLong(v);
139 }
140
141 @Override
142 public void writeShort(int v) throws IOException {
143 buffer.writeShort((short) v);
144 }
145
146 @Override
147 public void writeUTF(String s) throws IOException {
148 DataOutputStream out = utf8out;
149 if (out == null) {
150 if (closed) {
151 throw new IOException("The stream is closed");
152 }
153
154 utf8out = out = new DataOutputStream(this);
155 }
156 out.writeUTF(s);
157 }
158
159
160
161
162 public ByteBuf buffer() {
163 return buffer;
164 }
165
166 @Override
167 public void close() throws IOException {
168 if (closed) {
169 return;
170 }
171 closed = true;
172
173 try {
174 super.close();
175 } finally {
176 if (utf8out != null) {
177 utf8out.close();
178 }
179 if (releaseOnClose) {
180 buffer.release();
181 }
182 }
183 }
184 }