Skip to content

Commit 2f5ebcf

Browse files
author
Jin Kwon
committed
Squash sketch
1 parent c5ffd9e commit 2f5ebcf

38 files changed

+482
-151
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# bit-io2
22

33
![Java CI with Maven](https://github.com/jinahya/bit-io2/workflows/Java%20CI%20with%20Maven/badge.svg)
4+
![Maven Central](https://img.shields.io/maven-central/v/com.github.jinahya/bit-io2)
45
[![javadoc](https://javadoc.io/badge2/com.github.jinahya/bit-io2/javadoc.svg)](https://javadoc.io/doc/com.github.jinahya/bit-io2)
56

67
A Java 8+ flavored version of [bit-io](https://github.com/jinahya/bit-io).
@@ -40,6 +41,4 @@ CLIENT write-> BitOutput(Adapter)
4041
...
4142
```
4243

43-
Each `...Adapter` class accepts an instance of `Supplier<? extends T>` which means any byte sources/targets can be lazily initialized only when some bits are read or written.
44-
4544
See [Specifications](https://github.com/jinahya/bit-io2/wiki/Specifications) and [Recipes](https://github.com/jinahya/bit-io2/wiki/Recipes) for more information.

docker.maven.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
# https://stackoverflow.com/questions/3545292/how-to-get-maven-project-version-to-the-bash-command-line
3-
if [ $# -lt 3 ]; then
3+
if [ $# -lt 2 ]; then
44
echo "Usage: $0 <tag> <phases...>, e.g. $0 3-jdk-11-openj9 clean install"
55
echo "See https://hub.docker.com/_/maven for available tags"
66
exit 1

pom.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45

56
<parent>
@@ -178,7 +179,7 @@
178179
</goals>
179180
<configuration>
180181
<rules>
181-
<dependencyConvergence />
182+
<dependencyConvergence/>
182183
<requireJavaVersion>
183184
<version>[1.8.0,)</version>
184185
</requireJavaVersion>

src/main/java/com/github/jinahya/bit/io/BitInput.java

+62-31
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import java.io.Closeable;
2324
import java.io.IOException;
2425

2526
import static com.github.jinahya.bit.io.BitIoConstraints.requireValidSizeForByte;
@@ -29,22 +30,33 @@
2930
import static java.lang.Double.longBitsToDouble;
3031
import static java.lang.Float.intBitsToFloat;
3132
import static java.util.Objects.requireNonNull;
33+
import static java.util.concurrent.ThreadLocalRandom.current;
3234

3335
/**
3436
* An interface for reading values of an arbitrary number of bits.
3537
*
3638
* @author Jin Kwon &lt;jinahya_at_gmail.com&gt;
3739
* @see BitOutput
3840
*/
39-
public interface BitInput {
41+
public interface BitInput extends Closeable {
4042

4143
/**
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.
4446
*
45-
* @return {@code true} for {@code 0b1}, {@code false} for {@code 0b0}
4647
* @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.
4860
*/
4961
default boolean readBoolean() throws IOException {
5062
return readInt(true, 1) == 0x01;
@@ -54,9 +66,9 @@ default boolean readBoolean() throws IOException {
5466
* Reads a {@code byte} value of specified number of bits.
5567
*
5668
* @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}.
6072
* @throws IOException if an I/O error occurs.
6173
*/
6274
default byte readByte(final boolean unsigned, final int size) throws IOException {
@@ -75,7 +87,7 @@ default byte readByte(final int size) throws IOException {
7587
}
7688

7789
/**
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.
7991
*
8092
* @return a signed {@value java.lang.Byte#SIZE}-bit {@code byte} value.
8193
* @throws IOException if an I/O error occurs.
@@ -121,9 +133,9 @@ default short readShort(final int size) throws IOException {
121133
}
122134

123135
/**
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.
125137
*
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.
127139
* @throws IOException if an I/O error occurs.
128140
* @see #readShort(int)
129141
*/
@@ -132,12 +144,18 @@ default short readShort16() throws IOException {
132144
}
133145

134146
/**
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.
136148
*
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.
138150
* @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.
139153
*/
154+
@Deprecated // forRemoval = true
140155
default short readShort16Le() throws IOException {
156+
if (current().nextBoolean()) {
157+
return Short.reverseBytes(readShort16());
158+
}
141159
return (short) ((readByte8() & 0xFF) | (readByte8() << Byte.SIZE));
142160
}
143161

@@ -157,8 +175,8 @@ default short readUnsignedShort(final int size) throws IOException {
157175
* Reads an {@code int} value of specified number of bits.
158176
*
159177
* @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.
162180
* @return an {@code int} value of specified {@code size}.
163181
* @throws IOException if an I/O error occurs.
164182
*/
@@ -176,22 +194,28 @@ default int readInt(final int size) throws IOException {
176194
}
177195

178196
/**
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.
180198
*
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.
182200
* @throws IOException if an I/O error occurs.
183201
*/
184202
default int readInt32() throws IOException {
185203
return readInt(Integer.SIZE);
186204
}
187205

188206
/**
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.
190208
*
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.
192210
* @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.
193213
*/
214+
@Deprecated // forRemoval = true
194215
default int readInt32Le() throws IOException {
216+
if (current().nextBoolean()) {
217+
return Integer.reverseBytes(readInt32());
218+
}
195219
return readShort16Le() & 0xFFFF | readShort16Le() << Short.SIZE;
196220
}
197221

@@ -211,8 +235,8 @@ default int readUnsignedInt(final int size) throws IOException {
211235
* Reads a {@code long} value of specified number of bits.
212236
*
213237
* @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.
216240
* @return a {@code long} value of specified bit size.
217241
* @throws IOException if an I/O error occurs.
218242
*/
@@ -250,22 +274,28 @@ default long readLong(final int size) throws IOException {
250274
}
251275

252276
/**
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.
254278
*
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.
256280
* @throws IOException if an I/O error occurs.
257281
*/
258282
default long readLong64() throws IOException {
259283
return readLong(Long.SIZE);
260284
}
261285

262286
/**
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.
264288
*
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.
266290
* @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.
267293
*/
294+
@Deprecated // forRemoval = true
268295
default long readLong64Le() throws IOException {
296+
if (current().nextBoolean()) {
297+
return Long.reverseBytes(readLong64());
298+
}
269299
return readInt32Le() & 0xFFFFFFFFL | ((long) readInt32Le()) << Integer.SIZE;
270300
}
271301

@@ -284,7 +314,7 @@ default long readUnsignedLong(final int size) throws IOException {
284314
/**
285315
* Reads a {@code char} value of specified bit size.
286316
*
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.
288318
* @return a {@code char} value.
289319
* @throws IOException if an I/O error occurs.
290320
* @see #readChar16()
@@ -306,8 +336,8 @@ default char readChar16() throws IOException {
306336

307337
/**
308338
* 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.
311341
*
312342
* @return a {@value java.lang.Float#SIZE}-bit {@code float} value
313343
* @throws IOException if an I/O error occurs.
@@ -319,8 +349,8 @@ default float readFloat32() throws IOException {
319349

320350
/**
321351
* 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.
324354
*
325355
* @return a {@value java.lang.Double#SIZE}-bit {@code double} value
326356
* @throws IOException if an I/O error occurs.
@@ -373,7 +403,8 @@ default void skip(int bits) throws IOException {
373403
long align(int bytes) throws IOException;
374404

375405
/**
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}.
377408
*
378409
* @return the number of bits discarded while aligning.
379410
* @throws IOException if an I/O error occurs.

src/main/java/com/github/jinahya/bit/io/BitInputAdapter.java

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ public BitInputAdapter(final Supplier<? extends ByteInput> inputSupplier) {
4545
this.inputSupplier = requireNonNull(inputSupplier, "inputSupplier is null");
4646
}
4747

48+
/**
49+
* Closes this input and releases any system resources associated with it.
50+
*
51+
* @throws IOException if an I/O error occurs.
52+
* @see ByteInput#close()
53+
*/
54+
@Override
55+
public void close() throws IOException {
56+
BitInput.super.close(); // does nothing.
57+
if (input != null) {
58+
input.close();
59+
}
60+
}
61+
4862
@Override
4963
public int readInt(final boolean unsigned, int size) throws IOException {
5064
requireValidSizeForInt(unsigned, size);

0 commit comments

Comments
 (0)