Package com.pnfsoftware.jeb.util.io
Class ChannelUtil
java.lang.Object
com.pnfsoftware.jeb.util.io.ChannelUtil
Utility methods for
SeekableByteChannels.-
Method Summary
Modifier and TypeMethodDescriptionstatic byteget(SeekableByteChannel channel) Read one byte at the current channel position.static byteget(SeekableByteChannel channel, long position) Read one byte at an absolute channel position.static byte[]getAllFrom(SeekableByteChannel channel, long position) Read all remaining bytes from a channel starting at an absolute position.static intgetBEInt(SeekableByteChannel channel) Read a big-endian 32-bit integer at the current channel position.static intgetBEInt(SeekableByteChannel channel, long position) Read a big-endian 32-bit integer at an absolute channel position.static longgetBELong(SeekableByteChannel channel) Read a big-endian 64-bit integer at the current channel position.static longgetBELong(SeekableByteChannel channel, long position) Read a big-endian 64-bit integer at an absolute channel position.static shortgetBEShort(SeekableByteChannel channel) Read a big-endian 16-bit integer at the current channel position.static shortgetBEShort(SeekableByteChannel channel, long position) Read a big-endian 16-bit integer at an absolute channel position.static byte[]getBytes(SeekableByteChannel channel, int size) Read bytes at the current channel position.static byte[]getBytes(SeekableByteChannel channel, long position, int size) Read bytes at an absolute channel position.static byte[]getBytesUntil(SeekableByteChannel channel, byte stopper, int maxsize, boolean throwOnEOS) Read from the channel until the provided byte stopped is encountered.static intgetInt(SeekableByteChannel channel, long position, ByteOrder endianness) Read a 32-bit integer at an absolute channel position.static intgetInt(SeekableByteChannel channel, ByteOrder endianness) Read a 32-bit integer at the current channel position.static intgetLEInt(SeekableByteChannel channel) Read a little-endian 32-bit integer at the current channel position.static intgetLEInt(SeekableByteChannel channel, long position) Read a little-endian 32-bit integer at an absolute channel position.static longgetLELong(SeekableByteChannel channel) Read a little-endian 64-bit integer at the current channel position.static longgetLELong(SeekableByteChannel channel, long position) Read a little-endian 64-bit integer at an absolute channel position.static shortgetLEShort(SeekableByteChannel channel) Read a little-endian 16-bit integer at the current channel position.static shortgetLEShort(SeekableByteChannel channel, long position) Read a little-endian 16-bit integer at an absolute channel position.static longgetLong(SeekableByteChannel channel, long position, ByteOrder endianness) Read a 64-bit integer at an absolute channel position.static longgetLong(SeekableByteChannel channel, ByteOrder endianness) Read a 64-bit integer at the current channel position.static shortgetShort(SeekableByteChannel channel, long position, ByteOrder endianness) Read a 16-bit integer at an absolute channel position.static shortgetShort(SeekableByteChannel channel, ByteOrder endianness) Read a 16-bit integer at the current channel position.static ByteBufferread(SeekableByteChannel channel, long position, int size, boolean isBigEndian) Read exactlysizebytes from a channel at an absolute position.static ByteBufferread(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) Read exactlysizebytes from a channel at an absolute position.static ByteBufferreadBestEffort(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) Read up tosizebytes from a channel at an absolute position.
-
Method Details
-
read
public static ByteBuffer read(SeekableByteChannel channel, long position, int size, boolean isBigEndian) throws IOException Read exactlysizebytes from a channel at an absolute position.- Parameters:
channel- input channelposition- absolute channel positionsize- number of bytes to readisBigEndian- true to return a big-endian buffer, false for little-endian- Returns:
- byte buffer containing the requested bytes
- Throws:
IOException- if the requested bytes cannot be fully read
-
read
public static ByteBuffer read(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) throws IOException Read exactlysizebytes from a channel at an absolute position.- Parameters:
channel- input channelposition- absolute channel positionsize- number of bytes to readisBigEndian- true to return a big-endian buffer, false for little-endianbuffer- optional reusable destination buffer- Returns:
- byte buffer containing the requested bytes
- Throws:
IOException- if the requested bytes cannot be fully read
-
readBestEffort
public static ByteBuffer readBestEffort(SeekableByteChannel channel, long position, int size, boolean isBigEndian, ByteBuffer buffer) throws IOException Read up tosizebytes from a channel at an absolute position.- Parameters:
channel- input channelposition- absolute channel positionsize- maximum number of bytes to readisBigEndian- true to return a big-endian buffer, false for little-endianbuffer- optional reusable destination buffer- Returns:
- byte buffer containing all bytes that could be read
- Throws:
IOException- if the channel cannot be read
-
getAllFrom
Read all remaining bytes from a channel starting at an absolute position.- Parameters:
channel- input channelposition- absolute start position- Returns:
- remaining bytes
- Throws:
IOException- if the bytes cannot be read or exceed 2 GiB
-
get
Read one byte at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- byte value
- Throws:
IOException- if the byte cannot be read
-
get
Read one byte at the current channel position.- Parameters:
channel- input channel- Returns:
- byte value
- Throws:
IOException- if the byte cannot be read
-
getBytes
public static byte[] getBytes(SeekableByteChannel channel, long position, int size) throws IOException Read bytes at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel positionsize- number of bytes to read- Returns:
- byte array
- Throws:
IOException- if the requested bytes cannot be fully read
-
getBytes
Read bytes at the current channel position.- Parameters:
channel- input channelsize- number of bytes to read- Returns:
- byte array
- Throws:
IOException- if the requested bytes cannot be fully read
-
getShort
public static short getShort(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException Read a 16-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel positionendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getInt
public static int getInt(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException Read a 32-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel positionendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLong
public static long getLong(SeekableByteChannel channel, long position, ByteOrder endianness) throws IOException Read a 64-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel positionendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getShort
Read a 16-bit integer at the current channel position.- Parameters:
channel- input channelendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getInt
Read a 32-bit integer at the current channel position.- Parameters:
channel- input channelendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLong
Read a 64-bit integer at the current channel position.- Parameters:
channel- input channelendianness- byte order- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLEShort
Read a little-endian 16-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLEInt
Read a little-endian 32-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLELong
Read a little-endian 64-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBEShort
Read a big-endian 16-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBEInt
Read a big-endian 32-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBELong
Read a big-endian 64-bit integer at an absolute channel position.- Parameters:
channel- input channelposition- absolute channel position- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLEShort
Read a little-endian 16-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLEInt
Read a little-endian 32-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getLELong
Read a little-endian 64-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBEShort
Read a big-endian 16-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBEInt
Read a big-endian 32-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBELong
Read a big-endian 64-bit integer at the current channel position.- Parameters:
channel- input channel- Returns:
- decoded value
- Throws:
IOException- if the value cannot be read
-
getBytesUntil
public static byte[] getBytesUntil(SeekableByteChannel channel, byte stopper, int maxsize, boolean throwOnEOS) throws IOException Read from the channel until the provided byte stopped is encountered.- Parameters:
channel- channelstopper- stopper bytemaxsize- maximum allowed size to readthrowOnEOS- instruct the method will throw anIOExceptionif the end of input is reached- Returns:
- the resulting array, whose last character may be the byte stopper (unless it was not encountered or the maximum allowed size was read); the channel's position will be on the subsequent byte (or EOS)
- Throws:
IOException- if the bytes cannot be read
-