public class BufferUtil extends Object
These methods are not thread-safe unless explicityly marked as such.
Constructor and Description |
---|
BufferUtil() |
Modifier and Type | Method and Description |
---|---|
static char[] |
getChars(ByteBuffer buf,
int off,
int count)
Returns a character array from the buffer.
|
static String |
getUTF8String(ByteBuffer buf,
int off,
int len)
Extracts a specified sequence of bytes from a buffer and converts it
to a Java
String using UTF-8 encoding. |
static MappedByteBuffer |
map(File file,
long offset,
long length,
FileChannel.MapMode mode)
Memory maps a segment of a file.
|
static byte[] |
toArray(ByteBuffer buf)
Creates and fills a byte array that consists of all bytes from 0 to the
buffer's limit.
|
public static MappedByteBuffer map(File file, long offset, long length, FileChannel.MapMode mode) throws IOException
This method is thread-safe.
file
- The file to be mapped.offset
- The starting location of the mapping within the file.length
- The number of bytes to be mapped. This is limited to
Integer.MAX_VALUE
, but is a long
so that you can pass File.length()
.mode
- The mapping mode. The underlying channel will be opened
using the same mode (so that you can map a read-only file).IOException
public static String getUTF8String(ByteBuffer buf, int off, int len)
String
using UTF-8 encoding.buf
- The bufferoff
- Offset within the buffer where the string startslen
- Number of bytes to be converted; caller is responsible
for ensuring that the buffer has those bytes availablepublic static char[] getChars(ByteBuffer buf, int off, int count)
getChar()
.buf
- The bufferoff
- Offset within the buffer where conversion will startcount
- The number of characters to retrieve (unlike
other methods, which specify the number of bytes)public static byte[] toArray(ByteBuffer buf)