Debian Codegraph @

src/main/java/io/airlift/slice/SliceOutput.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.DataOutput;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.nio.charset.Charset;
21
22 @SuppressWarnings("JavaDoc") // IDEA-81310
23 public abstract class SliceOutput
24 extends OutputStream
25 implements DataOutput
26 {
27 /**
28 * Resets this stream to the initial position.
29 */
30 public abstract void reset();
31
32 /**
33 * Returns the {@code writerIndex} of this buffer.
34 */
35 public abstract int size();
36
37 /**
38 * Returns the number of writable bytes which is equal to
39 * {@code (this.capacity - this.writerIndex)}.
40 */
41 public abstract int writableBytes();
42
43 /**
44 * Returns {@code true}
45 * if and only if {@code (this.capacity - this.writerIndex)} is greater
46 * than {@code 0}.
47 */
48 public abstract boolean isWritable();
49
50 @Override
51 public final void writeBoolean(boolean value)
52 {
53 writeByte(value ? 1 : 0);
54 }
55
56 @Override
57 public final void write(int value)
58 {
59 writeByte(value);
60 }
61
62 /**
63 * Sets the specified byte at the current {@code writerIndex}
64 * and increases the {@code writerIndex} by {@code 1} in this buffer.
65 * The 24 high-order bits of the specified value are ignored.
66 *
67 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 1}
68 */
69 @Override
70 public abstract void writeByte(int value);
71
72 /**
73 * Sets the specified 16-bit short integer at the current
74 * {@code writerIndex} and increases the {@code writerIndex} by {@code 2}
75 * in this buffer. The 16 high-order bits of the specified value are ignored.
76 *
77 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 2}
78 */
79 @Override
80 public abstract void writeShort(int value);
81
82 /**
83 * Sets the specified 32-bit integer at the current {@code writerIndex}
84 * and increases the {@code writerIndex} by {@code 4} in this buffer.
85 *
86 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 4}
87 */
88 @Override
89 public abstract void writeInt(int value);
90
91 /**
92 * Sets the specified 64-bit long integer at the current
93 * {@code writerIndex} and increases the {@code writerIndex} by {@code 8}
94 * in this buffer.
95 *
96 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 8}
97 */
98 @Override
99 public abstract void writeLong(long value);
100
101 /**
102 * Sets the specified 32-bit float at the current
103 * {@code writerIndex} and increases the {@code writerIndex} by {@code 4}
104 * in this buffer.
105 *
106 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 4}
107 */
108 @Override
109 public abstract void writeFloat(float v);
110
111 /**
112 * Sets the specified 64-bit double at the current
113 * {@code writerIndex} and increases the {@code writerIndex} by {@code 8}
114 * in this buffer.
115 *
116 * @throws IndexOutOfBoundsException if {@code this.writableBytes} is less than {@code 8}
117 */
118 @Override
119 public abstract void writeDouble(double value);
120
121 /**
122 * Transfers the specified source buffer's data to this buffer starting at
123 * the current {@code writerIndex} until the source buffer becomes
124 * unreadable, and increases the {@code writerIndex} by the number of
125 * the transferred bytes. This method is basically same with
126 * {@link #writeBytes(Slice, int, int)}, except that this method
127 * increases the {@code readerIndex} of the source buffer by the number of
128 * the transferred bytes while {@link #writeBytes(Slice, int, int)}
129 * does not.
130 *
131 * @throws IndexOutOfBoundsException if {@code source.readableBytes} is greater than {@code this.writableBytes}
132 */
133 public abstract void writeBytes(Slice source);
134
135 /**
136 * Transfers the specified source buffer's data to this buffer starting at
137 * the current {@code writerIndex} and increases the {@code writerIndex}
138 * by the number of the transferred bytes (= {@code length}).
139 *
140 * @param sourceIndex the first index of the source
141 * @param length the number of bytes to transfer
142 * @throws IndexOutOfBoundsException if the specified {@code sourceIndex} is less than {@code 0},
143 * if {@code sourceIndex + length} is greater than {@code source.capacity}, or
144 * if {@code length} is greater than {@code this.writableBytes}
145 */
146 public abstract void writeBytes(Slice source, int sourceIndex, int length);
147
148 @Override
149 public final void write(byte[] source)
150 throws IOException
151 {
152 writeBytes(source);
153 }
154
155 /**
156 * Transfers the specified source array's data to this buffer starting at
157 * the current {@code writerIndex} and increases the {@code writerIndex}
158 * by the number of the transferred bytes (= {@code source.length}).
159 *
160 * @throws IndexOutOfBoundsException if {@code source.length} is greater than {@code this.writableBytes}
161 */
162 public abstract void writeBytes(byte[] source);
163
164 @Override
165 public final void write(byte[] source, int sourceIndex, int length)
166 {
167 writeBytes(source, sourceIndex, length);
168 }
169
170 /**
171 * Transfers the specified source array's data to this buffer starting at
172 * the current {@code writerIndex} and increases the {@code writerIndex}
173 * by the number of the transferred bytes (= {@code length}).
174 *
175 * @param sourceIndex the first index of the source
176 * @param length the number of bytes to transfer
177 * @throws IndexOutOfBoundsException if the specified {@code sourceIndex} is less than {@code 0},
178 * if {@code sourceIndex + length} is greater than {@code source.length}, or
179 * if {@code length} is greater than {@code this.writableBytes}
180 */
181 public abstract void writeBytes(byte[] source, int sourceIndex, int length);
182
183 /**
184 * Transfers the content of the specified stream to this buffer
185 * starting at the current {@code writerIndex} and increases the
186 * {@code writerIndex} by the number of the transferred bytes.
187 *
188 * @param length the number of bytes to transfer
189 * @throws IndexOutOfBoundsException if {@code length} is greater than {@code this.writableBytes}
190 * @throws java.io.IOException if the specified stream threw an exception during I/O
191 */
192 public abstract void writeBytes(InputStream in, int length)
193 throws IOException;
194
195 /**
196 * Fills this buffer with <tt>NUL (0x00)</tt> starting at the current
197 * {@code writerIndex} and increases the {@code writerIndex} by the
198 * specified {@code length}.
199 *
200 * @param length the number of <tt>NUL</tt>s to write to the buffer
201 * @throws IndexOutOfBoundsException if {@code length} is greater than {@code this.writableBytes}
202 */
203 public void writeZero(int length)
204 {
205 if (length == 0) {
206 return;
207 }
208 if (length < 0) {
209 throw new IllegalArgumentException(
210 "length must be 0 or greater than 0.");
211 }
212 int nLong = length >>> 3;
213 int nBytes = length & 7;
214 for (int i = nLong; i > 0; i--) {
215 writeLong(0);
216 }
217 if (nBytes == 4) {
218 writeInt(0);
219 }
220 else if (nBytes < 4) {
221 for (int i = nBytes; i > 0; i--) {
222 writeByte((byte) 0);
223 }
224 }
225 else {
226 writeInt(0);
227 for (int i = nBytes - 4; i > 0; i--) {
228 writeByte((byte) 0);
229 }
230 }
231 }
232
233 /**
234 * Returns a slice of this buffer's readable bytes. Modifying the content
235 * of the returned buffer or this buffer affects each other's content
236 * while they maintain separate indexes and marks. This method is
237 * identical to {@code buf.slice(buf.readerIndex(), buf.readableBytes())}.
238 * This method does not modify {@code readerIndex} or {@code writerIndex} of
239 * this buffer.
240 */
241 public abstract Slice slice();
242
243 /**
244 * Returns the raw underlying slice of this output stream. The slice may
245 * be larger than the size of this stream.
246 */
247 public abstract Slice getUnderlyingSlice();
248
249 /**
250 * Decodes this buffer's readable bytes into a string with the specified
251 * character set name. This method is identical to
252 * {@code buf.toString(buf.readerIndex(), buf.readableBytes(), charsetName)}.
253 * This method does not modify {@code readerIndex} or {@code writerIndex} of
254 * this buffer.
255 */
256 public abstract String toString(Charset charset);
257
258 public abstract SliceOutput appendLong(long value);
259
260 public abstract SliceOutput appendDouble(double value);
261
262 public abstract SliceOutput appendInt(int value);
263
264 public abstract SliceOutput appendShort(int value);
265
266 public abstract SliceOutput appendByte(int value);
267
268 public abstract SliceOutput appendBytes(byte[] source, int sourceIndex, int length);
269
270 public abstract SliceOutput appendBytes(byte[] source);
271
272 public abstract SliceOutput appendBytes(Slice slice);
273
274 //
275 // Unsupported operations
276 //
277
278 /**
279 * Unsupported operation
280 *
281 * @throws UnsupportedOperationException always
282 */
283 @Override
284 public void writeChar(int value)
285 {
286 throw new UnsupportedOperationException();
287 }
288
289 /**
290 * Unsupported operation
291 *
292 * @throws UnsupportedOperationException always
293 */
294 @Override
295 public void writeChars(String s)
296 {
297 throw new UnsupportedOperationException();
298 }
299
300 /**
301 * Unsupported operation
302 *
303 * @throws UnsupportedOperationException always
304 */
305 @Override
306 public void writeUTF(String s)
307 {
308 throw new UnsupportedOperationException();
309 }
310
311 /**
312 * Unsupported operation
313 *
314 * @throws UnsupportedOperationException always
315 */
316 @Override
317 public void writeBytes(String s)
318 {
319 throw new UnsupportedOperationException();
320 }
321 }