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
46
47
48
49 public ByteBufOutputStream(ByteBuf buffer) {
50 this.buffer = ObjectUtil.checkNotNull(buffer, "buffer");
51 startIndex = buffer.writerIndex();
52 }
53
54
55
56
57 public int writtenBytes() {
58 return buffer.writerIndex() - startIndex;
59 }
60
61 @Override
62 public void write(byte[] b, int off, int len) throws IOException {
63 if (len == 0) {
64 return;
65 }
66
67 buffer.writeBytes(b, off, len);
68 }
69
70 @Override
71 public void write(byte[] b) throws IOException {
72 buffer.writeBytes(b);
73 }
74
75 @Override
76 public void write(int b) throws IOException {
77 buffer.writeByte(b);
78 }
79
80 @Override
81 public void writeBoolean(boolean v) throws IOException {
82 buffer.writeBoolean(v);
83 }
84
85 @Override
86 public void writeByte(int v) throws IOException {
87 buffer.writeByte(v);
88 }
89
90 @Override
91 public void writeBytes(String s) throws IOException {
92 buffer.writeCharSequence(s, CharsetUtil.US_ASCII);
93 }
94
95 @Override
96 public void writeChar(int v) throws IOException {
97 buffer.writeChar(v);
98 }
99
100 @Override
101 public void writeChars(String s) throws IOException {
102 int len = s.length();
103 for (int i = 0 ; i < len ; i ++) {
104 buffer.writeChar(s.charAt(i));
105 }
106 }
107
108 @Override
109 public void writeDouble(double v) throws IOException {
110 buffer.writeDouble(v);
111 }
112
113 @Override
114 public void writeFloat(float v) throws IOException {
115 buffer.writeFloat(v);
116 }
117
118 @Override
119 public void writeInt(int v) throws IOException {
120 buffer.writeInt(v);
121 }
122
123 @Override
124 public void writeLong(long v) throws IOException {
125 buffer.writeLong(v);
126 }
127
128 @Override
129 public void writeShort(int v) throws IOException {
130 buffer.writeShort((short) v);
131 }
132
133 @Override
134 public void writeUTF(String s) throws IOException {
135 DataOutputStream out = utf8out;
136 if (out == null) {
137 if (closed) {
138 throw new IOException("The stream is closed");
139 }
140
141 utf8out = out = new DataOutputStream(this);
142 }
143 out.writeUTF(s);
144 }
145
146
147
148
149 public ByteBuf buffer() {
150 return buffer;
151 }
152
153 @Override
154 public void close() throws IOException {
155 if (closed) {
156 return;
157 }
158 closed = true;
159
160 try {
161 super.close();
162 } finally {
163 if (utf8out != null) {
164 utf8out.close();
165 }
166 }
167 }
168 }