28import java.io.BufferedInputStream;
29import java.io.BufferedOutputStream;
30import java.io.IOException;
31import java.io.InputStream;
32import java.io.OutputStream;
33import java.nio.ByteBuffer;
34import java.util.Locale;
36import org.jau.sys.Debug;
50 private static final boolean DEBUG =
Debug.
debug(
"Bitstream");
53 public static final int EOS = -1;
73 void close() throws IOException;
79 void flush() throws IOException;
112 long position(
long newPosition) throws UnsupportedOperationException, IllegalArgumentException;
120 long skip(final
long n) throws IOException;
127 void mark(final
int readLimit) throws UnsupportedOperationException;
138 void reset() throws UnsupportedOperationException, IllegalStateException, IOException;
149 int read() throws UnsupportedOperationException, IOException;
160 int write(final
byte val) throws UnsupportedOperationException, IOException;
170 private byte[] media;
207 public long position(
final long newPosition)
throws UnsupportedOperationException, IllegalArgumentException {
208 if( newPosition >= media.length ) {
211 pos = (int)newPosition;
212 if( posMark > pos ) {
219 public long skip(
final long n) {
222 final int remaining = media.length - pos;
223 skip = Math.min(remaining, (
int)n);
225 final int n2 = (int)n * -1;
226 skip = -1 * Math.min(pos, n2);
233 public void mark(
final int readLimit) {
238 public void reset() throws IllegalStateException {
240 throw new IllegalStateException(
"markpos not set");
242 if(DEBUG) { System.err.println(
"rewind: "+pos+
" -> "+posMark); }
249 if( media.length > pos ) {
250 r = 0xff & media[pos++];
256 System.err.println(
"u8["+(pos-1)+
"] -> "+
toHexBinString(
true, r, 8));
258 System.err.println(
"u8["+(pos-0)+
"] -> EOS");
267 if( media.length > pos ) {
275 System.err.println(
"u8["+(pos-1)+
"] <- "+
toHexBinString(
true, r, 8));
277 System.err.println(
"u8["+(pos-0)+
"] <- EOS");
291 private ByteBuffer media;
328 public long position(
final long newPosition)
throws UnsupportedOperationException, IllegalArgumentException {
329 if( newPosition >= media.limit() ) {
332 media.position((
int)newPosition);
333 pos = (int)newPosition;
334 if( posMark > pos ) {
341 public long skip(
final long n) {
344 final int remaining = media.limit() - pos;
345 skip = Math.min(remaining, (
int)n);
347 final int n2 = (int)n * -1;
348 skip = -1 * Math.min(pos, n2);
355 public void mark(
final int readLimit) {
360 public void reset() throws IllegalStateException {
362 throw new IllegalStateException(
"markpos not set");
364 if(DEBUG) { System.err.println(
"rewind: "+pos+
" -> "+posMark); }
365 media.position(posMark);
372 if( media.limit() > pos ) {
373 r = 0xff & media.get(pos++);
379 System.err.println(
"u8["+(pos-1)+
"] -> "+
toHexBinString(
true, r, 8));
381 System.err.println(
"u8["+(pos-0)+
"] -> EOS");
390 if( media.limit() > pos ) {
391 media.put(pos++, val);
398 System.err.println(
"u8["+(pos-1)+
"] <- "+
toHexBinString(
true, r, 8));
400 System.err.println(
"u8["+(pos-0)+
"] <- EOS");
414 private BufferedInputStream media;
416 private long posMark;
424 if( stream instanceof BufferedInputStream ) {
425 media = (BufferedInputStream) stream;
426 }
else if(
null != stream ) {
427 media =
new BufferedInputStream(stream);
439 public void close() throws IOException {
440 if(
null != media ) {
460 public long position(
final long newPosition)
throws UnsupportedOperationException, IllegalArgumentException {
461 throw new UnsupportedOperationException(
"N/a for "+getClass().getCanonicalName());
465 public long skip(
final long n)
throws IOException {
466 final long skip = media.skip(n);
472 public void mark(
final int readLimit) {
473 media.mark(readLimit);
478 public void reset() throws IllegalStateException, IOException {
480 throw new IllegalStateException(
"markpos not set");
482 if(DEBUG) { System.err.println(
"rewind: "+pos+
" -> "+posMark); }
488 public int read() throws IOException {
489 final int r = media.read();
494 System.err.println(
"u8["+pos+
"] -> EOS");
504 public int write(
final byte val)
throws UnsupportedOperationException {
505 throw new UnsupportedOperationException(
"not allowed with input stream");
516 private BufferedOutputStream media;
517 private long pos = 0;
525 if( stream instanceof BufferedOutputStream ) {
526 media = (BufferedOutputStream) stream;
527 }
else if(
null != stream ) {
528 media =
new BufferedOutputStream(stream);
536 public void close() throws IOException {
537 if(
null != media ) {
543 public void flush() throws IOException {
544 if(
null != media ) {
559 public long position(
final long newPosition)
throws UnsupportedOperationException, IllegalArgumentException {
560 throw new UnsupportedOperationException(
"N/a for "+getClass().getCanonicalName());
564 public long skip(
final long n)
throws IOException {
570 final long skip = n-i;
579 public void mark(
final int readLimit)
throws UnsupportedOperationException {
580 throw new UnsupportedOperationException(
"not allowed with output stream");
584 public void reset() throws UnsupportedOperationException {
585 throw new UnsupportedOperationException(
"not allowed with output stream");
589 public int read() throws UnsupportedOperationException {
590 throw new UnsupportedOperationException(
"not allowed with output stream");
594 public int write(
final byte val)
throws IOException {
595 final int r = 0xff & val;
605 private ByteStream<T> bytes;
607 private int bitBuffer;
608 private int bitsDataMark;
611 private int bitCount;
612 private int bitsCountMark;
614 private boolean outputMode;
615 private boolean throwIOExceptionOnEOF;
624 this.outputMode = outputMode;
627 throwIOExceptionOnEOF =
false;
630 private final void resetLocal() {
636 private final void validateMode() throws IllegalArgumentException {
638 throw new IllegalArgumentException(
"stream can neither input nor output: "+
this);
641 throw new IllegalArgumentException(
"stream cannot output as requested: "+
this);
644 throw new IllegalArgumentException(
"stream cannot input as requested: "+
this);
655 throwIOExceptionOnEOF = enable;
670 public final void setStream(
final T stream,
final boolean outputMode)
throws IllegalArgumentException, IOException {
671 if(
null != bytes && this.outputMode ) {
674 this.bytes.setStream(stream);
675 this.outputMode = outputMode;
699 public final void close() throws IOException {
700 if(
null != bytes && this.outputMode ) {
718 public final int flush() throws IllegalStateException, IOException {
719 if( !outputMode ||
null == bytes ) {
720 throw new IllegalStateException(
"not in output-mode: "+
this);
723 if( 0 != bitCount ) {
724 final int r = bytes.write((
byte)bitBuffer);
728 if( throwIOExceptionOnEOF ) {
729 throw new IOException(
"EOS "+
this);
738 public final boolean canInput() {
return null != bytes ? bytes.canInput() :
false; }
741 public final boolean canOutput() {
return null != bytes ? bytes.canOutput() :
false; }
748 public final void mark(
final int readLimit)
throws IllegalStateException {
749 if( outputMode ||
null == bytes ) {
750 throw new IllegalStateException(
"not in input-mode: "+
this);
752 bytes.mark(readLimit);
753 bitsDataMark = bitBuffer;
754 bitsCountMark = bitCount;
766 public final void reset() throws IllegalStateException, IOException {
767 if( outputMode ||
null == bytes ) {
768 throw new IllegalStateException(
"not in input-mode: "+
this);
770 if( 0 > bitsCountMark ) {
771 throw new IllegalStateException(
"markpos not set: "+
this);
774 bitBuffer = bitsDataMark;
775 bitCount = bitsCountMark;
808 if( 0 == bitCount ) {
827 if(
null == bytes ) {
829 }
else if( 0 == bitCount ) {
830 return bytes.position() << 3;
832 final long bytePos = bytes.position() - ( outputMode ? 0 : 1 );
833 return ( bytePos << 3 ) + 8 - bitCount;
859 public final long position(
final long newPosition)
throws UnsupportedOperationException, IllegalArgumentException, IllegalStateException, IOException {
860 if( 0 > newPosition ) {
861 throw new IllegalArgumentException(
"new position not positive: "+newPosition);
865 if( newPosition >
skip(newPosition) ) {
877 public final int readBit(
final boolean msbFirst)
throws UnsupportedOperationException, IllegalStateException, IOException {
878 if( outputMode ||
null == bytes ) {
879 throw new IllegalStateException(
"not in input-mode: "+
this);
881 if ( 0 < bitCount ) {
884 return ( bitBuffer >>> bitCount ) & 0x01;
886 return ( bitBuffer >>> ( 7 - bitCount ) ) & 0x01;
889 bitBuffer = bytes.read();
890 if(
EOS == bitBuffer ) {
891 if( throwIOExceptionOnEOF ) {
892 throw new IOException(
"EOS "+
this);
898 return bitBuffer >>> 7;
900 return bitBuffer & 0x01;
913 public final int writeBit(
final boolean msbFirst,
final int bit)
throws IllegalStateException, IOException {
914 if( !outputMode ||
null == bytes ) {
915 throw new IllegalStateException(
"not in output-mode: "+
this);
917 if ( 0 < bitCount ) {
920 bitBuffer |= ( 0x01 & bit ) << bitCount;
922 bitBuffer |= ( 0x01 & bit ) << ( 7 - bitCount );
924 if( 0 == bitCount ) {
925 final int r = bytes.write((
byte)bitBuffer);
926 if( throwIOExceptionOnEOF &&
EOS == r ) {
927 throw new IOException(
"EOS "+
this);
934 bitBuffer = ( 0x01 & bit ) << 7;
936 bitBuffer = 0x01 & bit;
950 public long skip(
final long n)
throws IllegalStateException, IOException {
951 if(
null == bytes ) {
952 throw new IllegalStateException(
"closed: "+
this);
955 System.err.println(
"Bitstream.skip.0: "+n+
" - "+
toStringImpl());
958 if( n <= bitCount ) {
961 System.err.println(
"Bitstream.skip.F_N1: "+n+
" - "+
toStringImpl());
967 if(
EOS == bytes.write((
byte)bitBuffer) ) {
973 final long n2 = n - bitCount;
974 final long n3 = n2 >>> 3;
975 final long n4 = bytes.skip(n3);
976 final int n5 = (int) ( n2 - ( n3 << 3 ) );
977 final long nX = ( n4 << 3 ) + n5 + bitCount;
987 System.err.println(
"Bitstream.skip.F_EOS: "+n+
" - "+
toStringImpl());
989 if( throwIOExceptionOnEOF ) {
990 throw new IOException(
"EOS "+
this);
994 bitCount = ( 8 - n5 ) & 7;
996 if( !outputMode && 0 < bitCount ) {
997 bitBuffer = bytes.read();
998 if(
EOS == bitBuffer ) {
999 notReadBits = bitCount;
1004 System.err.println(
"Bitstream.skip.F_N2: "+n+
", notReadBits "+notReadBits+
" - "+
toStringImpl());
1006 return nX - notReadBits;
1015 private static final boolean useFastPathStream =
true;
1016 private static final boolean useFastPathTypes =
true;
1029 public int readBits31(
final int n)
throws IllegalArgumentException, IOException {
1031 throw new IllegalArgumentException(
"n > 31: "+n);
1033 if( outputMode ||
null == bytes ) {
1034 throw new IllegalStateException(
"not in input-mode: "+
this);
1039 if( !useFastPathStream ) {
1042 for(
int i=0; i < n; i++) {
1043 final int b =
readBit(
false );
1045 if( throwIOExceptionOnEOF ) {
1046 throw new IOException(
"EOS "+
this);
1056 final int n1 = Math.min(n, bitCount);
1059 final int m1 = ( 1 << n1 ) - 1;
1060 final int s1 = 7 - bitCount + 1;
1064 r = ( m1 & ( bitBuffer >>> s1 ) );
1071 assert( 0 == bitCount );
1074 bitBuffer = bytes.read();
1075 if(
EOS == bitBuffer ) {
1076 if( throwIOExceptionOnEOF ) {
1077 throw new IOException(
"EOS "+
this);
1081 final int n2 = Math.min(c, 8);
1082 final int m2 = ( 1 << n2 ) - 1;
1086 r |= ( m2 & bitBuffer ) << s;
1106 public int writeBits31(
final int n,
final int bits)
throws IllegalStateException, IllegalArgumentException, IOException {
1108 throw new IllegalArgumentException(
"n > 31: "+n);
1110 if( !outputMode ||
null == bytes ) {
1111 throw new IllegalStateException(
"not in output-mode: "+
this);
1114 if( !useFastPathStream ) {
1116 for(
int i=0; i < n; i++) {
1117 final int b =
writeBit(
false , ( bits >>> i ) & 0x1);
1125 final int n1 = Math.min(n, bitCount);
1127 final int m1 = ( 1 << n1 ) - 1;
1128 final int s1 = 7 - bitCount + 1;
1132 bitBuffer |= ( m1 & bits ) << s1 ;
1133 if( 0 == bitCount ) {
1134 if(
EOS == bytes.write((
byte)bitBuffer) ) {
1135 if( throwIOExceptionOnEOF ) {
1136 throw new IOException(
"EOS "+
this);
1145 assert( 0 == bitCount );
1148 final int n2 = Math.min(c, 8);
1149 final int m2 = ( 1 << n2 ) - 1;
1153 bitBuffer = ( m2 & ( bits >>> s ) );
1155 if( 0 == bitCount ) {
1156 if(
EOS == bytes.write((
byte)bitBuffer) ) {
1157 if( throwIOExceptionOnEOF ) {
1158 throw new IOException(
"EOS "+
this);
1179 public final int readUInt8() throws IllegalStateException, IOException {
1180 if( 0 == bitCount && useFastPathTypes ) {
1182 if( outputMode ||
null == bytes ) {
1183 throw new IllegalStateException(
"not in input-mode: "+
this);
1185 final int r = bytes.read();
1186 if( throwIOExceptionOnEOF &&
EOS == r ) {
1187 throw new IOException(
"EOS "+
this);
1201 public final int writeInt8(
final byte int8)
throws IllegalStateException, IOException {
1202 if( 0 == bitCount && useFastPathTypes ) {
1204 if( !outputMode ||
null == bytes ) {
1205 throw new IllegalStateException(
"not in output-mode: "+
this);
1207 final int r = bytes.write(int8);
1208 if( throwIOExceptionOnEOF &&
EOS == r ) {
1209 throw new IOException(
"EOS "+
this);
1229 public final int readUInt16(
final boolean bigEndian)
throws IllegalStateException, IOException {
1230 if( 0 == bitCount && useFastPathTypes ) {
1232 if( outputMode ||
null == bytes ) {
1233 throw new IllegalStateException(
"not in input-mode: "+
this);
1235 final int b1 = bytes.read();
1236 final int b2 =
EOS != b1 ? bytes.read() :
EOS;
1238 if( throwIOExceptionOnEOF ) {
1239 throw new IOException(
"EOS "+
this);
1242 }
else if( bigEndian ) {
1243 return b1 << 8 | b2;
1245 return b2 << 8 | b1;
1251 }
else if( bigEndian ) {
1252 final int b1 = 0xff & ( i16 >>> 8 );
1253 final int b2 = 0xff & i16;
1254 return b2 << 8 | b1;
1270 public static final int readUInt16(
final boolean bigEndian,
final byte[] bytes,
final int offset)
throws IndexOutOfBoundsException {
1273 final int b1 = bytes[offset] & 0xff;
1274 final int b2 = bytes[offset+1] & 0xff;
1276 return b1 << 8 | b2;
1278 return b2 << 8 | b1;
1290 public final int writeInt16(
final boolean bigEndian,
final short int16)
throws IllegalStateException, IOException {
1291 if( 0 == bitCount && useFastPathTypes ) {
1293 if( !outputMode ||
null == bytes ) {
1294 throw new IllegalStateException(
"not in output-mode: "+
this);
1296 final byte hi = (byte) ( 0xff & ( int16 >>> 8 ) );
1297 final byte lo = (byte) ( 0xff & int16 );
1306 if(
EOS != bytes.write(b1) ) {
1307 if(
EOS != bytes.write(b2) ) {
1311 if( throwIOExceptionOnEOF ) {
1312 throw new IOException(
"EOS "+
this);
1315 }
else if( bigEndian ) {
1316 final int b1 = 0xff & ( int16 >>> 8 );
1317 final int b2 = 0xff & int16;
1336 public final long readUInt32(
final boolean bigEndian)
throws IllegalStateException, IOException {
1337 if( 0 == bitCount && useFastPathTypes ) {
1339 if( outputMode ||
null == bytes ) {
1340 throw new IllegalStateException(
"not in input-mode: "+
this);
1342 final int b1 = bytes.read();
1343 final int b2 =
EOS != b1 ? bytes.read() :
EOS;
1344 final int b3 =
EOS != b2 ? bytes.read() :
EOS;
1345 final int b4 =
EOS != b3 ? bytes.read() :
EOS;
1347 if( throwIOExceptionOnEOF ) {
1348 throw new IOException(
"EOS "+
this);
1351 }
else if( bigEndian ) {
1352 return 0xffffffffL & ( b1 << 24 | b2 << 16 | b3 << 8 | b4 );
1354 return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 );
1361 }
else if( bigEndian ) {
1362 final int b1 = 0xff & ( i16b >>> 8 );
1363 final int b2 = 0xff & i16b;
1364 final int b3 = 0xff & ( i16a >>> 8 );
1365 final int b4 = 0xff & i16a;
1366 return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 );
1368 return 0xffffffffL & ( i16b << 16 | i16a );
1382 public static final long readUInt32(
final boolean bigEndian,
final byte[] bytes,
final int offset)
throws IndexOutOfBoundsException {
1384 final int b1 = bytes[offset];
1385 final int b2 = bytes[offset+1];
1386 final int b3 = bytes[offset+2];
1387 final int b4 = bytes[offset+3];
1389 return 0xffffffffL & ( b1 << 24 | b2 << 16 | b3 << 8 | b4 );
1391 return 0xffffffffL & ( b4 << 24 | b3 << 16 | b2 << 8 | b1 );
1403 public final int writeInt32(
final boolean bigEndian,
final int int32)
throws IllegalStateException, IOException {
1404 if( 0 == bitCount && useFastPathTypes ) {
1406 if( !outputMode ||
null == bytes ) {
1407 throw new IllegalStateException(
"not in output-mode: "+
this);
1409 final byte p1 = (byte) ( 0xff & ( int32 >>> 24 ) );
1410 final byte p2 = (byte) ( 0xff & ( int32 >>> 16 ) );
1411 final byte p3 = (byte) ( 0xff & ( int32 >>> 8 ) );
1412 final byte p4 = (byte) ( 0xff & int32 );
1413 final byte b1, b2, b3, b4;
1425 if(
EOS != bytes.write(b1) ) {
1426 if(
EOS != bytes.write(b2) ) {
1427 if(
EOS != bytes.write(b3) ) {
1428 if(
EOS != bytes.write(b4) ) {
1434 if( throwIOExceptionOnEOF ) {
1435 throw new IOException(
"EOS "+
this);
1438 }
else if( bigEndian ) {
1439 final int p1 = 0xff & ( int32 >>> 24 );
1440 final int p2 = 0xff & ( int32 >>> 16 );
1441 final int p3 = 0xff & ( int32 >>> 8 );
1442 final int p4 = 0xff & int32 ;
1450 final int hi = 0x0000ffff & ( int32 >>> 16 );
1451 final int lo = 0x0000ffff & int32 ;
1469 return 0xffffffffL & int32;
1488 if( Integer.MAX_VALUE >= uint32 ) {
1502 if(
null == bytes ) {
1506 mode = outputMode ?
"output" :
"input";
1507 bpos = bytes.position();
1509 return String.format((Locale)
null,
"%s, pos %d [byteP %d, bitCnt %d], bitbuf %s",
1513 private static final String strZeroPadding=
"0000000000000000000000000000000000000000000000000000000000000000";
1514 public static String
toBinString(
final boolean msbFirst,
final int v,
final int bitCount) {
1515 if( 0 == bitCount ) {
1519 final int mask = (int) ( ( 1L << bitCount ) - 1L );
1520 final String s0 = Integer.toBinaryString( mask & v );
1521 return strZeroPadding.substring(0, bitCount-s0.length())+s0;
1523 final char[] c =
new char[32];
1524 for(
int i=0; i<bitCount; i++) {
1525 c[i] = 0 != ( v & ( 1 << i ) ) ?
'1' :
'0';
1527 final String s0 =
new String(c, 0, bitCount);
1528 return s0+strZeroPadding.substring(0, bitCount-s0.length());
1531 public static String
toHexBinString(
final boolean msbFirst,
final int v,
final int bitCount) {
1532 final int nibbles = 0 == bitCount ? 2 : ( bitCount + 3 ) / 4;
1533 return String.format((Locale)
null,
"[0x%0"+nibbles+
"X, msbFirst %b, %s]", v, msbFirst,
toBinString(msbFirst, v, bitCount));
1535 public static final String
toHexBinString(
final boolean msbFirst,
final byte[] data,
final int offset,
final int len) {
1536 final StringBuilder sb =
new StringBuilder();
1538 for(
int i=0; i<len; i++) {
1539 final int v = 0xFF & data[offset+i];
1543 return sb.toString();
1545 public static final String
toHexBinString(
final boolean msbFirst,
final ByteBuffer data,
final int offset,
final int len) {
1546 final StringBuilder sb =
new StringBuilder();
1548 for(
int i=0; i<len; i++) {
1549 final int v = 0xFF & data.get(offset+i);
1553 return sb.toString();
1556 public static void checkBounds(
final byte[] sb,
final int offset,
final int remaining)
throws IndexOutOfBoundsException {
1557 if( offset + remaining > sb.length ) {
1558 throw new IndexOutOfBoundsException(
"Buffer of size "+sb.length+
" cannot hold offset "+offset+
" + remaining "+remaining);
long position(final long newPosition)
void setStream(final byte[] stream)
ByteArrayStream(final byte[] stream)
void mark(final int readLimit)
int write(final byte val)
int write(final byte val)
void setStream(final ByteBuffer stream)
void mark(final int readLimit)
ByteBufferStream(final ByteBuffer stream)
long position(final long newPosition)
void setStream(final OutputStream stream)
void mark(final int readLimit)
int write(final byte val)
long position(final long newPosition)
ByteOutputStream(final OutputStream stream)
Versatile Bitstream implementation supporting:
int readBits31(final int n)
Return incoming bits as read via readBit(boolean) LSB-first as little-endian.
final void mark(final int readLimit)
Set markpos to current position, allowing the stream to be reset().
final ByteStream< T > getStream()
Returns the currently used ByteStream.
final int flush()
Synchronizes all underlying output stream operations, or do nothing.
static final int toUInt32Int(final int int32)
Returns the reinterpreted given int32_t value as uint32_t if ≤ Integer#MAX_VALUE as within an int sto...
final int writeInt8(final byte int8)
Write the given 8 bits via writeBits31(int, int).
static final int readUInt16(final boolean bigEndian, final byte[] bytes, final int offset)
Return incoming uint16_t value and swap bytes according to bigEndian.
int writeBits31(final int n, final int bits)
Write the given bits via writeBit(boolean, int) LSB-first as little-endian.
final int getBitPosition()
Return the next bit number to be read or write counting from [0..7].
final void reset()
Reset stream position to markpos as set via mark(int).
final int readUInt16(final boolean bigEndian)
Return incoming uint16_t as read via readBits31(int) LSB-first as little-endian, hence bytes are swap...
final int writeInt16(final boolean bigEndian, final short int16)
Write the given 16 bits via writeBits31(int, int) LSB-first as little-endian, hence bytes are swapped...
final int getLastBitPos()
Return the last bit number read or written counting from [0..7].
final boolean canOutput()
Return true if stream can handle output, i.e.
static String toHexBinString(final boolean msbFirst, final int v, final int bitCount)
static final int uint32LongToInt(final long uint32)
Returns the given uint32_t value long value as int if ≤ Integer#MAX_VALUE.
final long position()
Returns the bit position in the stream.
final boolean canInput()
Return true if stream can handle input, i.e.
static final long readUInt32(final boolean bigEndian, final byte[] bytes, final int offset)
Return incoming uint32_t and swap bytes according to bigEndian.
static final String toHexBinString(final boolean msbFirst, final byte[] data, final int offset, final int len)
static final long toUInt32Long(final int int32)
Reinterpret the given int32_t value as uint32_t, i.e.
final boolean getThrowIOExceptionOnEOF()
Returns true if I/O methods throw an IOException if EOS appears, otherwise false (default).
final int getBitBuffer()
Returns the current bit buffer.
long skip(final long n)
It is implementation dependent, whether backward skip giving a negative number is supported or not.
final long position(final long newPosition)
Sets this stream's bit position.
final void setThrowIOExceptionOnEOF(final boolean enable)
Enables or disables throwing an IOException in case EOS appears.
final void setStream(final T stream, final boolean outputMode)
Sets the underlying stream, without close()ing the previous one.
final int readBit(final boolean msbFirst)
Bitstream(final ByteStream< T > stream, final boolean outputMode)
final long readUInt32(final boolean bigEndian)
Return incoming uint32_t as read via readBits31(int) LSB-first as little-endian, hence bytes are swap...
final void close()
Closing the underlying stream, implies flush().
static String toBinString(final boolean msbFirst, final int v, final int bitCount)
static final String toHexBinString(final boolean msbFirst, final ByteBuffer data, final int offset, final int len)
final T getSubStream()
Returns the currently used ByteStream's ByteStream#getStream().
final int writeInt32(final boolean bigEndian, final int int32)
Write the given 32 bits via writeBits31(int, int) LSB-first as little-endian, hence bytes are swapped...
static final int EOS
End of stream marker, {@value} or 0xFFFFFFFF.
final int getBitCount()
Number of remaining bits in cache to read before next byte-read (input mode) or number of remaining b...
static void checkBounds(final byte[] sb, final int offset, final int remaining)
final int writeBit(final boolean msbFirst, final int bit)
final int readUInt8()
Return incoming uint8_t as read via readBits31(int).
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)
long skip(final long n)
It is implementation dependent, whether backward skip giving a negative number is supported or not.
void flush()
Synchronizes all underlying output stream operations, or do nothing.
void reset()
Reset stream position to markpos as set via mark(int).
boolean canOutput()
Return true if stream can handle output, i.e.
void close()
Closing the underlying stream, implies flush().
int read()
Reads one byte from the stream.
long position()
Returns the byte position in the stream.
void setStream(final T stream)
Sets the underlying stream, without close()ing the previous one.
void mark(final int readLimit)
Set markpos to current position, allowing the stream to be reset().
boolean canInput()
Return true if stream can handle input, i.e.
T getStream()
Returns the underlying stream.
int write(final byte val)
Writes one byte, to the stream.