Debian Codegraph @

src/main/java/io/airlift/slice/BasicSliceOutput.java

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.charset.Charset;
19
20 import static io.airlift.slice.Preconditions.checkNotNull;
21 import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
22 import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
23 import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT;
24 import static io.airlift.slice.SizeOf.SIZE_OF_INT;
25 import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
26 import static io.airlift.slice.SizeOf.SIZE_OF_SHORT;
27
28 public class BasicSliceOutput
29 extends SliceOutput
30 {
31 private final Slice slice;
32 private int size;
33
34 protected BasicSliceOutput(Slice slice)
35 {
36 this.slice = checkNotNull(slice, "slice is null");
37 }
38
39 @Override
40 public void reset()
41 {
42 size = 0;
43 }
44
45 @Override
46 public int size()
47 {
48 return size;
49 }
50
51 @Override
52 public boolean isWritable()
53 {
54 return writableBytes() > 0;
55 }
56
57 @Override
58 public int writableBytes()
59 {
60 return slice.length() - size;
61 }
62
63 @Override
64 public void writeByte(int value)
65 {
66 slice.setByte(size, value);
67 size += SIZE_OF_BYTE;
68 }
69
70 @Override
71 public void writeShort(int value)
72 {
73 slice.setShort(size, value);
74 size += SIZE_OF_SHORT;
75 }
76
77 @Override
78 public void writeInt(int value)
79 {
80 slice.setInt(size, value);
81 size += SIZE_OF_INT;
82 }
83
84 @Override
85 public void writeLong(long value)
86 {
87 slice.setLong(size, value);
88 size += SIZE_OF_LONG;
89 }
90
91 @Override
92 public void writeFloat(float value)
93 {
94 slice.setFloat(size, value);
95 size += SIZE_OF_FLOAT;
96 }
97
98 @Override
99 public void writeDouble(double value)
100 {
101 slice.setDouble(size, value);
102 size += SIZE_OF_DOUBLE;
103 }
104
105 @Override
106 public void writeBytes(byte[] source, int sourceIndex, int length)
107 {
108 slice.setBytes(size, source, sourceIndex, length);
109 size += length;
110 }
111
112 @Override
113 public void writeBytes(byte[] source)
114 {
115 writeBytes(source, 0, source.length);
116 }
117
118 @Override
119 public void writeBytes(Slice source)
120 {
121 writeBytes(source, 0, source.length());
122 }
123
124 @Override
125 public void writeBytes(Slice source, int sourceIndex, int length)
126 {
127 slice.setBytes(size, source, sourceIndex, length);
128 size += length;
129 }
130
131 @Override
132 public void writeBytes(InputStream in, int length)
133 throws IOException
134 {
135 slice.setBytes(size, in, length);
136 size += length;
137 }
138
139 @Override
140 public BasicSliceOutput appendLong(long value)
141 {
142 writeLong(value);
143 return this;
144 }
145
146 @Override
147 public SliceOutput appendDouble(double value)
148 {
149 writeDouble(value);
150 return this;
151 }
152
153 @Override
154 public BasicSliceOutput appendInt(int value)
155 {
156 writeInt(value);
157 return this;
158 }
159
160 @Override
161 public BasicSliceOutput appendShort(int value)
162 {
163 writeShort(value);
164 return this;
165 }
166
167 @Override
168 public BasicSliceOutput appendByte(int value)
169 {
170 writeByte(value);
171 return this;
172 }
173
174 @Override
175 public BasicSliceOutput appendBytes(byte[] source, int sourceIndex, int length)
176 {
177 write(source, sourceIndex, length);
178 return this;
179 }
180
181 @Override
182 public BasicSliceOutput appendBytes(byte[] source)
183 {
184 writeBytes(source);
185 return this;
186 }
187
188 @Override
189 public BasicSliceOutput appendBytes(Slice slice)
190 {
191 writeBytes(slice);
192 return this;
193 }
194
195 @Override
196 public Slice slice()
197 {
198 return slice.slice(0, size);
199 }
200
201 @Override
202 public Slice getUnderlyingSlice()
203 {
204 return slice;
205 }
206
207 @Override
208 public String toString()
209 {
210 StringBuilder builder = new StringBuilder("BasicSliceOutput{");
211 builder.append("size=").append(size);
212 builder.append(", capacity=").append(slice.length());
213 builder.append('}');
214 return builder.toString();
215 }
216
217 @Override
218 public String toString(Charset charset)
219 {
220 return slice.toString(0, size, charset);
221 }
222 }