20
20
* #L%
21
21
*/
22
22
23
+ import java .io .Closeable ;
23
24
import java .io .IOException ;
24
25
25
26
import static com .github .jinahya .bit .io .BitIoConstraints .requireValidSizeForByte ;
29
30
import static java .lang .Double .longBitsToDouble ;
30
31
import static java .lang .Float .intBitsToFloat ;
31
32
import static java .util .Objects .requireNonNull ;
33
+ import static java .util .concurrent .ThreadLocalRandom .current ;
32
34
33
35
/**
34
36
* An interface for reading values of an arbitrary number of bits.
35
37
*
36
38
* @author Jin Kwon <jinahya_at_gmail.com>
37
39
* @see BitOutput
38
40
*/
39
- public interface BitInput {
41
+ public interface BitInput extends Closeable {
40
42
41
43
/**
42
- * Reads a {@code 1}-bit {@code boolean} value. This method reads a {@code 1}-bit unsigned {@code int} and returns
43
- * {@code true} for {@code 0b1} and {@code false} for {@code 0b0} .
44
+ * Closes this input and releases any system resources associated with it. The {@code close} method of {@code
45
+ * BitInput} interface does nothing .
44
46
*
45
- * @return {@code true} for {@code 0b1}, {@code false} for {@code 0b0}
46
47
* @throws IOException if an I/O error occurs.
47
- * @see BitOutput#writeBoolean(boolean)
48
+ */
49
+ @ Override
50
+ default void close () throws IOException {
51
+ // does nothing.
52
+ }
53
+
54
+ /**
55
+ * Reads a {@code 1}-bit {@code boolean} value. This method reads a {@code 1}-bit unsigned {@code int} value and
56
+ * returns {@code true} for {@code 0b1} and {@code false} for {@code 0b0}.
57
+ *
58
+ * @return the value read.
59
+ * @throws IOException if an I/O error occurs.
48
60
*/
49
61
default boolean readBoolean () throws IOException {
50
62
return readInt (true , 1 ) == 0x01 ;
@@ -54,9 +66,9 @@ default boolean readBoolean() throws IOException {
54
66
* Reads a {@code byte} value of specified number of bits.
55
67
*
56
68
* @param unsigned a flag for indicating unsigned value; {@code true} for unsigned, {@code false} for signed.
57
- * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Byte#SIZE} - (unsigned ?
58
- * {@code 1} : {@code 0})), both inclusive.
59
- * @return a {@code byte} value of specified {@code size}.
69
+ * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Byte#SIZE} - ({@code
70
+ * unsigned ? 1 : 0})), both inclusive.
71
+ * @return a {@code byte} value of specified bit {@code size}.
60
72
* @throws IOException if an I/O error occurs.
61
73
*/
62
74
default byte readByte (final boolean unsigned , final int size ) throws IOException {
@@ -75,7 +87,7 @@ default byte readByte(final int size) throws IOException {
75
87
}
76
88
77
89
/**
78
- * Reads a signed {@value java.lang.Byte#SIZE}-bit {@code byte} value.
90
+ * Reads a {@value java.lang.Byte#SIZE}-bit signed {@code byte} value.
79
91
*
80
92
* @return a signed {@value java.lang.Byte#SIZE}-bit {@code byte} value.
81
93
* @throws IOException if an I/O error occurs.
@@ -121,9 +133,9 @@ default short readShort(final int size) throws IOException {
121
133
}
122
134
123
135
/**
124
- * Reads a signed {@value java.lang.Short#SIZE}-bit {@code short} value.
136
+ * Reads a {@value java.lang.Short#SIZE}-bit signed {@code short} value.
125
137
*
126
- * @return a signed {@value java.lang.Short#SIZE}-bit {@code short} value.
138
+ * @return a {@value java.lang.Short#SIZE}-bit signed {@code short} value read .
127
139
* @throws IOException if an I/O error occurs.
128
140
* @see #readShort(int)
129
141
*/
@@ -132,12 +144,18 @@ default short readShort16() throws IOException {
132
144
}
133
145
134
146
/**
135
- * Reads a signed {@value java.lang.Short#SIZE}-bit {@code short} value in little endian byte order.
147
+ * Reads a {@value java.lang.Short#SIZE}-bit signed {@code short} value in little endian byte order.
136
148
*
137
- * @return a signed {@value java.lang.Short#SIZE}-bit {@code short} value in little endian byte order.
149
+ * @return a {@value java.lang.Short#SIZE}-bit signed {@code short} value read in little endian byte order.
138
150
* @throws IOException if an I/O error occurs.
151
+ * @deprecated Reads a value with {@link #readShort16()} and reverse bytes with {@link Short#reverseBytes(short)}
152
+ * method.
139
153
*/
154
+ @ Deprecated // forRemoval = true
140
155
default short readShort16Le () throws IOException {
156
+ if (current ().nextBoolean ()) {
157
+ return Short .reverseBytes (readShort16 ());
158
+ }
141
159
return (short ) ((readByte8 () & 0xFF ) | (readByte8 () << Byte .SIZE ));
142
160
}
143
161
@@ -157,8 +175,8 @@ default short readUnsignedShort(final int size) throws IOException {
157
175
* Reads an {@code int} value of specified number of bits.
158
176
*
159
177
* @param unsigned a flag for indicating unsigned value; {@code true} for unsigned, {@code false} for signed.
160
- * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Integer#SIZE} - (unsigned ?
161
- * {@code 1} : {@code 0})), both inclusive.
178
+ * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Integer#SIZE} - ({@code
179
+ * unsigned ? 1: 0})), both inclusive.
162
180
* @return an {@code int} value of specified {@code size}.
163
181
* @throws IOException if an I/O error occurs.
164
182
*/
@@ -176,22 +194,28 @@ default int readInt(final int size) throws IOException {
176
194
}
177
195
178
196
/**
179
- * Reads a signed {@value java.lang.Integer#SIZE}-bit {@code int} value.
197
+ * Reads a {@value java.lang.Integer#SIZE}-bit signed {@code int} value.
180
198
*
181
- * @return a signed {@value java.lang.Integer#SIZE}-bit {@code int} value.
199
+ * @return a {@value java.lang.Integer#SIZE}-bit signed {@code int} value read .
182
200
* @throws IOException if an I/O error occurs.
183
201
*/
184
202
default int readInt32 () throws IOException {
185
203
return readInt (Integer .SIZE );
186
204
}
187
205
188
206
/**
189
- * Reads a signed {@value java.lang.Integer#SIZE}-bit {@code int} value in little endian byte order.
207
+ * Reads a {@value java.lang.Integer#SIZE}-bit signed {@code int} value in little endian byte order.
190
208
*
191
- * @return a signed {@value java.lang.Integer#SIZE}-bit {@code int} value.
209
+ * @return a {@value java.lang.Integer#SIZE}-bit signed {@code int} value.
192
210
* @throws IOException if an I/O error occurs.
211
+ * @deprecated Reads a value with {@link #readInt32()} method and reverse bytes with {@link
212
+ * Integer#reverseBytes(int)} method.
193
213
*/
214
+ @ Deprecated // forRemoval = true
194
215
default int readInt32Le () throws IOException {
216
+ if (current ().nextBoolean ()) {
217
+ return Integer .reverseBytes (readInt32 ());
218
+ }
195
219
return readShort16Le () & 0xFFFF | readShort16Le () << Short .SIZE ;
196
220
}
197
221
@@ -211,8 +235,8 @@ default int readUnsignedInt(final int size) throws IOException {
211
235
* Reads a {@code long} value of specified number of bits.
212
236
*
213
237
* @param unsigned a flag for indicating unsigned value; {@code true} for unsigned, {@code false} for signed.
214
- * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Long#SIZE} - (unsigned ?
215
- * {@code 1} : {@code 0})), both inclusive.
238
+ * @param size the number of bits to read; between {@code 1} and ({@value java.lang.Long#SIZE} - ({@code
239
+ * unsigned ? 1: 0})), both inclusive.
216
240
* @return a {@code long} value of specified bit size.
217
241
* @throws IOException if an I/O error occurs.
218
242
*/
@@ -250,22 +274,28 @@ default long readLong(final int size) throws IOException {
250
274
}
251
275
252
276
/**
253
- * Reads a signed {@value java.lang.Long#SIZE}-bit {@code long} value.
277
+ * Reads a {@value java.lang.Long#SIZE}-bit signed {@code long} value.
254
278
*
255
- * @return a signed {@value java.lang.Long#SIZE}-bit {@code long} value.
279
+ * @return a {@value java.lang.Long#SIZE}-bit signed {@code long} value.
256
280
* @throws IOException if an I/O error occurs.
257
281
*/
258
282
default long readLong64 () throws IOException {
259
283
return readLong (Long .SIZE );
260
284
}
261
285
262
286
/**
263
- * Reads a signed {@value java.lang.Long#SIZE}-bit {@code long} value in little endian byte order.
287
+ * Reads a {@value java.lang.Long#SIZE}-bit signed {@code long} value in little endian byte order.
264
288
*
265
- * @return a signed {@value java.lang.Long#SIZE}-bit {@code long} value.
289
+ * @return a {@value java.lang.Long#SIZE}-bit signed {@code long} value read .
266
290
* @throws IOException if an I/O error occurs.
291
+ * @deprecated Reads a value with {@link #readLong64()} and reverse bytes with {@link Long#reverseBytes(long)}
292
+ * method.
267
293
*/
294
+ @ Deprecated // forRemoval = true
268
295
default long readLong64Le () throws IOException {
296
+ if (current ().nextBoolean ()) {
297
+ return Long .reverseBytes (readLong64 ());
298
+ }
269
299
return readInt32Le () & 0xFFFFFFFFL | ((long ) readInt32Le ()) << Integer .SIZE ;
270
300
}
271
301
@@ -284,7 +314,7 @@ default long readUnsignedLong(final int size) throws IOException {
284
314
/**
285
315
* Reads a {@code char} value of specified bit size.
286
316
*
287
- * @param size the number of bits to read.
317
+ * @param size the number of bits to read; between {@code 1} and {@value java.lang.Character#SIZE}, both inclusive .
288
318
* @return a {@code char} value.
289
319
* @throws IOException if an I/O error occurs.
290
320
* @see #readChar16()
@@ -306,8 +336,8 @@ default char readChar16() throws IOException {
306
336
307
337
/**
308
338
* Reads a {@value java.lang.Float#SIZE}-bit {@code float} value. The {@code readFloat32()} method of {@code
309
- * BitInput} interface reads a {@value java.lang.Integer#SIZE}-bit {@link Float#intBitsToFloat( int) int bits} and
310
- * returns it as a {@code float} value .
339
+ * BitInput} interface reads a {@value java.lang.Integer#SIZE}-bit {@code int} value and returns a {@code float}
340
+ * value converted with {@link Float#intBitsToFloat(int)} method .
311
341
*
312
342
* @return a {@value java.lang.Float#SIZE}-bit {@code float} value
313
343
* @throws IOException if an I/O error occurs.
@@ -319,8 +349,8 @@ default float readFloat32() throws IOException {
319
349
320
350
/**
321
351
* Reads a {@value java.lang.Double#SIZE}-bit {@code double} value. The {@code readDouble64()} method of {@code
322
- * BitInput} interface reads a {@value java.lang.Long#SIZE}-bit {@link Double#longBitsToDouble( long) long bits} and
323
- * returns it as a {@code double} value .
352
+ * BitInput} interface reads a {@value java.lang.Long#SIZE}-bit {@code long} value and returns a {@code double}
353
+ * value converted with {@link Double#longBitsToDouble(long)} method .
324
354
*
325
355
* @return a {@value java.lang.Double#SIZE}-bit {@code double} value
326
356
* @throws IOException if an I/O error occurs.
@@ -373,7 +403,8 @@ default void skip(int bits) throws IOException {
373
403
long align (int bytes ) throws IOException ;
374
404
375
405
/**
376
- * Aligns to a single byte by discarding bits.
406
+ * Aligns to a single byte by discarding bits. The {@code align()} method of {@code BitInput} interface invokes
407
+ * {@link #align(int)} with {@value java.lang.Byte#BYTES}.
377
408
*
378
409
* @return the number of bits discarded while aligning.
379
410
* @throws IOException if an I/O error occurs.
0 commit comments