Java File Read via ByteBuffer readIntoBuffer(RandomAccessFile file, int offset, int size)

Here you can find the source of readIntoBuffer(RandomAccessFile file, int offset, int size)

Description

Reading bytes from a file into a ByteBuffer

License

Open Source License

Parameter

Parameter Description
file Opened and readable file
size Number of bytes to be read

Exception

Parameter Description
IOException if something went wrong during reading

Return

The bytes read, wrapped into a

Declaration

static ByteBuffer readIntoBuffer(RandomAccessFile file, int offset, int size) throws IOException 

Method Source Code


//package com.java2s;
/*//from   ww w.j  a  v  a 2s.c o m
 * Copyright (c) 2018 Patrick Scheibe
 * Affiliation: Saxonian Incubator for Clinical Translation, University Leipzig, Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.IOException;
import java.io.RandomAccessFile;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    private static final ByteOrder BYTE_ORDER = ByteOrder.LITTLE_ENDIAN;

    /**
     * Reading bytes from a file into a {@link ByteBuffer}
     *
     * @param file Opened and readable file
     * @param size Number of bytes to be read
     * @return The bytes read, wrapped into a {@link ByteBuffer}
     * @throws IOException if something went wrong during reading
     */
    static ByteBuffer readIntoBuffer(RandomAccessFile file, int offset, int size) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(size);
        buffer.order(BYTE_ORDER);
        file.seek(offset);
        file.read(buffer.array());
        return buffer;
    }
}

Related

  1. readInt(ReadableByteChannel channel)
  2. readInt(ReadableByteChannel channel)
  3. readInt16(DataInput di)
  4. readIntArray(DataInput input)
  5. readIntLittleEndian(InputStream in)
  6. readJsonFromFile(String path)
  7. readLargeFile(File file)
  8. readLong(byte[] src, int pointer)
  9. readLong(InputStream input)