Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package io.airlift.slice;
17
18 import java.io.DataOutput;
19 import java.io.DataOutputStream;
20 import java.io.FilterOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 import static io.airlift.slice.Preconditions.checkNotNull;
25
26 /**
27 * An implementation of {@link java.io.DataOutput} that uses little-endian byte ordering
28 * for writing {@code char}, {@code short}, {@code int}, {@code float}, {@code
29 * double}, and {@code long} values.
30 * <p/>
31 * <b>Note:</b> This class intentionally violates the specification of its
32 * supertype {@code DataOutput}, which explicitly requires big-endian byte
33 * order.
34 *
35 * @author Chris Nokleberg
36 * @author Keith Bottner
37 */
38 final class LittleEndianDataOutputStream
39 extends FilterOutputStream
40 implements DataOutput
41 {
42 /**
43 * Creates a {@code LittleEndianDataOutputStream} that wraps the given stream.
44 *
45 * @param out the stream to delegate to
46 */
47 public LittleEndianDataOutputStream(OutputStream out)
48 {
49 super(new DataOutputStream(checkNotNull(out, "out is null")));
50 }
51
52 @Override
53 public void write(byte[] b, int off, int len)
54 throws IOException
55 {
56 // Override slow FilterOutputStream impl
57 out.write(b, off, len);
58 }
59
60 @Override
61 public void writeBoolean(boolean v)
62 throws IOException
63 {
64 ((DataOutputStream) out).writeBoolean(v);
65 }
66
67 @Override
68 public void writeByte(int v)
69 throws IOException
70 {
71 ((DataOutputStream) out).writeByte(v);
72 }
73
74 /**
75 * @deprecated The semantics of {@code writeBytes(String s)} are considered
76 * dangerous. Please use {@link #writeUTF(String s)},
77 * {@link #writeChars(String s)} or another write method instead.
78 */
79 @Deprecated
80 @Override
81 public void writeBytes(String s)
82 throws IOException
83 {
84 ((DataOutputStream) out).writeBytes(s);
85 }
86
87 /**
88 * Writes a char as specified by {@link java.io.DataOutputStream#writeChar(int)},
89 * except using little-endian byte order.
90 *
91 * @throws java.io.IOException if an I/O error occurs
92 */
93 @Override
94 public void writeChar(int v)
95 throws IOException
96 {
97 writeShort(v);
98 }
99
100 /**
101 * Writes a {@code String} as specified by
102 * {@link java.io.DataOutputStream#writeChars(String)}, except each character is
103 * written using little-endian byte order.
104 *
105 * @throws java.io.IOException if an I/O error occurs
106 */
107 @Override
108 public void writeChars(String s)
109 throws IOException
110 {
111 for (int i = 0; i < s.length(); i++) {
112 writeChar(s.charAt(i));
113 }
114 }
115
116 /**
117 * Writes a {@code double} as specified by
118 * {@link java.io.DataOutputStream#writeDouble(double)}, except using little-endian
119 * byte order.
120 *
121 * @throws java.io.IOException if an I/O error occurs
122 */
123 @Override
124 public void writeDouble(double v)
125 throws IOException
126 {
127 writeLong(Double.doubleToLongBits(v));
128 }
129
130 /**
131 * Writes a {@code float} as specified by
132 * {@link java.io.DataOutputStream#writeFloat(float)}, except using little-endian byte
133 * order.
134 *
135 * @throws java.io.IOException if an I/O error occurs
136 */
137 @Override
138 public void writeFloat(float v)
139 throws IOException
140 {
141 writeInt(Float.floatToIntBits(v));
142 }
143
144 /**
145 * Writes an {@code int} as specified by
146 * {@link java.io.DataOutputStream#writeInt(int)}, except using little-endian byte
147 * order.
148 *
149 * @throws java.io.IOException if an I/O error occurs
150 */
151 @Override
152 public void writeInt(int v)
153 throws IOException
154 {
155 out.write(0xFF & v);
156 out.write(0xFF & (v >> 8));
157 out.write(0xFF & (v >> 16));
158 out.write(0xFF & (v >> 24));
159 }
160
161 /**
162 * Writes a {@code long} as specified by
163 * {@link java.io.DataOutputStream#writeLong(long)}, except using little-endian byte
164 * order.
165 *
166 * @throws java.io.IOException if an I/O error occurs
167 */
168 @Override
169 public void writeLong(long value)
170 throws IOException
171 {
172 value = Long.reverseBytes(value);
173
174 byte[] bytes = new byte[8];
175 for (int i = 7; i >= 0; i--) {
176 bytes[i] = (byte) (value & 0xffL);
177 value >>= 8;
178 }
179 write(bytes, 0, bytes.length);
180 }
181
182 /**
183 * Writes a {@code short} as specified by
184 * {@link java.io.DataOutputStream#writeShort(int)}, except using little-endian byte
185 * order.
186 *
187 * @throws java.io.IOException if an I/O error occurs
188 */
189 @Override
190 public void writeShort(int v)
191 throws IOException
192 {
193 out.write(0xFF & v);
194 out.write(0xFF & (v >> 8));
195 }
196
197 @Override
198 public void writeUTF(String str)
199 throws IOException
200 {
201 ((DataOutputStream) out).writeUTF(str);
202 }
203 }