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