Java Utililty Methods File Read via ByteBuffer

List of utility methods to do File Read via ByteBuffer

Description

The list of methods to do File Read via ByteBuffer are organized into topic(s).

Method

intreadBytes(InputStream is)
read Bytes
try {
    byte[] data = new byte[4];
    is.read(data);
    int result = byteArrayToInt(data);
    if (result > SANITY_CHECK_MAX_INT) {
        throw new RuntimeException("Too large number");
    return result;
...
byte[]readBytes(Path file)
Read bytes from a path
SeekableByteChannel channel = Files.newByteChannel(file, StandardOpenOption.READ);
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
return (byte[]) buffer.flip().array();
byte[]readBytes(ReadableByteChannel channel, byte[] array)
read Bytes
return readBytes(channel, array, array.length);
ByteBufferreadBytes(String file)
read Bytes
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
FileChannel fChannel = new RandomAccessFile(file, "r").getChannel();
fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));
fChannel.close();
return ByteBuffer.wrap(dataOut.toByteArray());
intreadCode(byte[] message)
read Code
byte[] code = { message[0], message[1], message[2], message[3] };
return ByteBuffer.wrap(code).getInt();
ListreadColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData)
read Columnar Key Block Data For No Dictionary Cols
List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50);
ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length);
noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData);
noDictionaryValKeyStoreDataHolder.flip();
while (noDictionaryValKeyStoreDataHolder.hasRemaining()) {
    short dataLength = noDictionaryValKeyStoreDataHolder.getShort();
    byte[] noDictionaryValKeyData = new byte[dataLength];
    noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData);
...
StringreadContent(File file)
Read the content from the given file
if (!file.exists())
    throw new IOException("Source file does not exist");
FileInputStream stream = new FileInputStream(file);
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
...
StringreadContentsNIO(FileInputStream input, String encoding, boolean decodeNIO)
read Contents NIO
ByteBuffer bbuf;
FileChannel fc = input.getChannel();
bbuf = ByteBuffer.allocate((int) fc.size());
fc.read(bbuf);
bbuf.flip();
return decodeNIO ? decodeNIO(encoding, bbuf) : decodeIO(encoding, bbuf);
byte[]readData(String fileName, int size)
read Data
final RandomAccessFile fis = new RandomAccessFile(fileName, "r");
final FileChannel fc = fis.getChannel();
if (size < 0) {
    size = (int) fc.size();
MappedByteBuffer buf = fc.map(MapMode.READ_ONLY, 0, size);
final byte[] bytes = new byte[size];
buf.get(bytes);
...
byte[]readDelimitedFromInputStream(InputStream inputStream)
Read length delimited data from the input stream
byte[] sizeBytes = new byte[4];
int numRead = inputStream.read(sizeBytes, 0, 4);
if (numRead < 4) {
    throw new IOException("Failed to read the message size from the input stream!");
int messageLength = ByteBuffer.wrap(sizeBytes).getInt();
byte[] messageData = new byte[messageLength];
for (numRead = 0; numRead < messageLength;) {
...