Top Description Inners Fields Constructors Methods
org.apache.commons.compress.utils

public final Class ByteUtils

extends Object
Class Inheritance
Imports
java.io.DataInput, .DataOutput, .IOException, .InputStream, .OutputStream

Utility methods for reading and writing bytes.
Since
1.14

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ByteUtils.ByteConsumer

Used to consume bytes.

public static interface
ByteUtils.ByteSupplier

Used to supply bytes.

public static class
public static class

Field Summary

Modifier and TypeField and Description
public static final byte[]
EMPTY_BYTE_ARRAY

Empty array.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
checkReadLength(final int length)

public static long

Returns:

the number read
fromLittleEndian
(final byte[]
the byte array to convert
bytes
)

Reads the given byte array as a little-endian long.

public static long

Returns:

the number read
fromLittleEndian
(final byte[]
the byte array to convert
bytes
,
final int
the offset into the array that starts the value
off
,
final int
the number of bytes representing the value
length
)

Reads the given byte array as a little-endian long.

public static long

Returns:

the number read
fromLittleEndian
(final ByteUtils.ByteSupplier
the supplier for bytes
supplier
,
final int
the number of bytes representing the value
length
)

Reads the given number of bytes from the given supplier as a little-endian long.

public static long

Returns:

the number read
fromLittleEndian
(final DataInput
the input to read from
in
,
final int
the number of bytes representing the value
length
)

Reads the given number of bytes from the given input as little-endian long.

public static long

Returns:

the number read
fromLittleEndian
(final InputStream
the stream to read from
in
,
final int
the number of bytes representing the value
length
)

Deprecated Unused
Reads the given number of bytes from the given stream as a little-endian long.
public static void
toLittleEndian(final byte[]
the array to write into
b
,
final long
the value to insert
value
,
final int
the offset into the array that receives the first byte
off
,
final int
the number of bytes to use to represent the value
length
)

Inserts the given value into the array as a little-endian sequence of the given length starting at the given offset.

public static void
toLittleEndian(final ByteUtils.ByteConsumer
the consumer to provide the bytes to
consumer
,
final long
the value to provide
value
,
final int
the number of bytes to use to represent the value
length
)

Provides the given value to the given consumer as a little-endian sequence of the given length.

public static void
toLittleEndian(final DataOutput
the output to write to
out
,
final long
the value to write
value
,
final int
the number of bytes to use to represent the value
length
)

Deprecated Unused
Writes the given value to the given stream as a little-endian array of the given length.
public static void
toLittleEndian(final OutputStream
the stream to write to
out
,
final long
the value to write
value
,
final int
the number of bytes to use to represent the value
length
)

Writes the given value to the given stream as a little-endian array of the given length.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_BYTE_ARRAYback to summary
public static final byte[] EMPTY_BYTE_ARRAY

Empty array.

Since
1.21

Constructor Detail

ByteUtilsback to summary
private ByteUtils()

Method Detail

checkReadLengthback to summary
private static void checkReadLength(final int length)
fromLittleEndianback to summary
public static long fromLittleEndian(final byte[] bytes)

Reads the given byte array as a little-endian long.

Parameters
bytes:byte[]

the byte array to convert

Returns:long

the number read

fromLittleEndianback to summary
public static long fromLittleEndian(final byte[] bytes, final int off, final int length)

Reads the given byte array as a little-endian long.

Parameters
bytes:byte[]

the byte array to convert

off:int

the offset into the array that starts the value

length:int

the number of bytes representing the value

Returns:long

the number read

Exceptions
IllegalArgumentException:
if len is bigger than eight
fromLittleEndianback to summary
public static long fromLittleEndian(final ByteUtils.ByteSupplier supplier, final int length) throws IOException

Reads the given number of bytes from the given supplier as a little-endian long.

Typically used by our InputStreams that need to count the bytes read as well.

Parameters
supplier:ByteUtils.ByteSupplier

the supplier for bytes

length:int

the number of bytes representing the value

Returns:long

the number read

Exceptions
IOException:
if the supplier fails or doesn't supply the given number of bytes anymore
IllegalArgumentException:
if len is bigger than eight
fromLittleEndianback to summary
public static long fromLittleEndian(final DataInput in, final int length) throws IOException

Reads the given number of bytes from the given input as little-endian long.

Parameters
in:DataInput

the input to read from

length:int

the number of bytes representing the value

Returns:long

the number read

Exceptions
IOException:
if reading fails or the stream doesn't contain the given number of bytes anymore
IllegalArgumentException:
if len is bigger than eight
fromLittleEndianback to summary
public static long fromLittleEndian(final InputStream in, final int length) throws IOException

Deprecated

Unused

Reads the given number of bytes from the given stream as a little-endian long.

Parameters
in:InputStream

the stream to read from

length:int

the number of bytes representing the value

Returns:long

the number read

Annotations
@Deprecated
Exceptions
IOException:
if reading fails or the stream doesn't contain the given number of bytes anymore
IllegalArgumentException:
if len is bigger than eight
toLittleEndianback to summary
public static void toLittleEndian(final byte[] b, final long value, final int off, final int length)

Inserts the given value into the array as a little-endian sequence of the given length starting at the given offset.

Parameters
b:byte[]

the array to write into

value:long

the value to insert

off:int

the offset into the array that receives the first byte

length:int

the number of bytes to use to represent the value

toLittleEndianback to summary
public static void toLittleEndian(final ByteUtils.ByteConsumer consumer, final long value, final int length) throws IOException

Provides the given value to the given consumer as a little-endian sequence of the given length.

Parameters
consumer:ByteUtils.ByteConsumer

the consumer to provide the bytes to

value:long

the value to provide

length:int

the number of bytes to use to represent the value

Exceptions
IOException:
if writing fails
toLittleEndianback to summary
public static void toLittleEndian(final DataOutput out, final long value, final int length) throws IOException

Deprecated

Unused

Writes the given value to the given stream as a little-endian array of the given length.

Parameters
out:DataOutput

the output to write to

value:long

the value to write

length:int

the number of bytes to use to represent the value

Annotations
@Deprecated
Exceptions
IOException:
if writing fails
toLittleEndianback to summary
public static void toLittleEndian(final OutputStream out, final long value, final int length) throws IOException

Writes the given value to the given stream as a little-endian array of the given length.

Parameters
out:OutputStream

the stream to write to

value:long

the value to write

length:int

the number of bytes to use to represent the value

Exceptions
IOException:
if writing fails
org.apache.commons.compress.utils back to summary

public Interface ByteUtils.ByteConsumer

Known Direct Implementers
org.apache.commons.compress.utils.ByteUtils.OutputStreamByteConsumer

Used to consume bytes.
Since
1.14

Method Summary

Modifier and TypeMethod and Description
public void
accept(int
the byte to consume
b
)

The contract is similar to OutputStream#write(int), consume the lower eight bytes of the int as a byte.

Method Detail

acceptback to summary
public void accept(int b) throws IOException

The contract is similar to OutputStream#write(int), consume the lower eight bytes of the int as a byte.

Parameters
b:int

the byte to consume

Exceptions
IOException:
if consuming fails
org.apache.commons.compress.utils back to summary

public Interface ByteUtils.ByteSupplier

Known Direct Implementers
org.apache.commons.compress.utils.ByteUtils.InputStreamByteSupplier

Used to supply bytes.
Since
1.14

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the supplied byte or -1 if there are no more bytes
getAsByte
()

The contract is similar to InputStream#read(), return the byte as an unsigned int, -1 if there are no more bytes.

Method Detail

getAsByteback to summary
public int getAsByte() throws IOException

The contract is similar to InputStream#read(), return the byte as an unsigned int, -1 if there are no more bytes.

Returns:int

the supplied byte or -1 if there are no more bytes

Exceptions
IOException:
if supplying fails
org.apache.commons.compress.utils back to summary

public Class ByteUtils.InputStreamByteSupplier

extends Object
implements ByteSupplier
Class Inheritance
  • java.lang.Object
  • org.apache.commons.compress.utils.ByteUtils.InputStreamByteSupplier
All Implemented Interfaces
org.apache.commons.compress.utils.ByteUtils.ByteSupplier
Annotations
@Deprecated

Deprecated

Unused

ByteSupplier based on InputStream.
Since
1.14

Field Summary

Modifier and TypeField and Description
private final InputStream

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
getAsByte()

Implements org.apache.commons.compress.utils.ByteUtils.ByteSupplier.getAsByte.

The contract is similar to InputStream#read(), return the byte as an unsigned int, -1 if there are no more bytes.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

isback to summary
private final InputStream is

Constructor Detail

InputStreamByteSupplierback to summary
public InputStreamByteSupplier(final InputStream is)

Method Detail

getAsByteback to summary
public int getAsByte() throws IOException

Implements org.apache.commons.compress.utils.ByteUtils.ByteSupplier.getAsByte.

Doc from org.apache.commons.compress.utils.ByteUtils.ByteSupplier.getAsByte.

The contract is similar to InputStream#read(), return the byte as an unsigned int, -1 if there are no more bytes.

Returns:int

the supplied byte or -1 if there are no more bytes

Annotations
@Override
Exceptions
IOException:
if supplying fails
org.apache.commons.compress.utils back to summary

public Class ByteUtils.OutputStreamByteConsumer

extends Object
implements ByteConsumer
Class Inheritance
  • java.lang.Object
  • org.apache.commons.compress.utils.ByteUtils.OutputStreamByteConsumer
All Implemented Interfaces
org.apache.commons.compress.utils.ByteUtils.ByteConsumer

ByteConsumer based on OutputStream.
Since
1.14

Field Summary

Modifier and TypeField and Description
private final OutputStream

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
accept(final int
the byte to consume
b
)

Implements org.apache.commons.compress.utils.ByteUtils.ByteConsumer.accept.

The contract is similar to OutputStream#write(int), consume the lower eight bytes of the int as a byte.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

osback to summary
private final OutputStream os

Constructor Detail

OutputStreamByteConsumerback to summary
public OutputStreamByteConsumer(final OutputStream os)

Method Detail

acceptback to summary
public void accept(final int b) throws IOException

Implements org.apache.commons.compress.utils.ByteUtils.ByteConsumer.accept.

Doc from org.apache.commons.compress.utils.ByteUtils.ByteConsumer.accept.

The contract is similar to OutputStream#write(int), consume the lower eight bytes of the int as a byte.

Parameters
b:int

the byte to consume

Annotations
@Override
Exceptions
IOException:
if consuming fails