1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.buffer;
17
18 import java.io.DataOutput;
19 import java.io.DataOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 public class ByteBufOutputStream extends OutputStream implements DataOutput {
37
38 private final ByteBuf buffer;
39 private final int startIndex;
40 private final DataOutputStream utf8out = new DataOutputStream(this);
41
42
43
44
45 public ByteBufOutputStream(ByteBuf buffer) {
46 if (buffer == null) {
47 throw new NullPointerException("buffer");
48 }
49 this.buffer = buffer;
50 startIndex = buffer.writerIndex();
51 }
52
53
54
55
56 public int writtenBytes() {
57 return buffer.writerIndex() - startIndex;
58 }
59
60 @Override
61 public void write(byte[] b, int off, int len) throws IOException {
62 if (len == 0) {
63 return;
64 }
65
66 buffer.writeBytes(b, off, len);
67 }
68
69 @Override
70 public void write(byte[] b) throws IOException {
71 buffer.writeBytes(b);
72 }
73
74 @Override
75 public void write(int b) throws IOException {
76 buffer.writeByte(b);
77 }
78
79 @Override
80 public void writeBoolean(boolean v) throws IOException {
81 buffer.writeBoolean(v);
82 }
83
84 @Override
85 public void writeByte(int v) throws IOException {
86 buffer.writeByte(v);
87 }
88
89 @Override
90 public void writeBytes(String s) throws IOException {
91 ByteBufUtil.writeAscii(buffer, s);
92 }
93
94 @Override
95 public void writeChar(int v) throws IOException {
96 buffer.writeChar(v);
97 }
98
99 @Override
100 public void writeChars(String s) throws IOException {
101 int len = s.length();
102 for (int i = 0 ; i < len ; i ++) {
103 buffer.writeChar(s.charAt(i));
104 }
105 }
106
107 @Override
108 public void writeDouble(double v) throws IOException {
109 buffer.writeDouble(v);
110 }
111
112 @Override
113 public void writeFloat(float v) throws IOException {
114 buffer.writeFloat(v);
115 }
116
117 @Override
118 public void writeInt(int v) throws IOException {
119 buffer.writeInt(v);
120 }
121
122 @Override
123 public void writeLong(long v) throws IOException {
124 buffer.writeLong(v);
125 }
126
127 @Override
128 public void writeShort(int v) throws IOException {
129 buffer.writeShort((short) v);
130 }
131
132 @Override
133 public void writeUTF(String s) throws IOException {
134 utf8out.writeUTF(s);
135 }
136
137
138
139
140 public ByteBuf buffer() {
141 return buffer;
142 }
143 }