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

booleanreadAiffHeader(DataInputStream inStrm, FileChannel fc)
read Aiff Header
byte[] buf = new byte[10];
boolean isAIFC = false;
try {
    inStrm.readInt();
    inStrm.readFully(buf, 0, 4);
    if ("AIFC".equals(new String(buf, 0, 4)))
        isAIFC = true;
    else if (!"AIFF".equals(new String(buf, 0, 4)))
...
voidreadAndTransfer(final String dir)
read And Transfer
File file = new File(dir);
try (FileInputStream fis = new FileInputStream(file)) {
    FileChannel fc = fis.getChannel();
    ByteBuffer bb = ByteBuffer.allocate(1024);
    SocketChannel sc = null;
    while (fc.read(bb) != -1) {
} catch (Exception e) {
...
byte[]readAsByteArray(final InputStream source)
Reads everything from the input stream using NIO and returns an array of bytes.
final ByteBuffer bb = readAsByteBuffer(source);
final byte[] result = new byte[bb.limit()];
bb.get(result);
bb.clear();
return result;
shortreadBigEndianWord(byte[] buf)
read Big Endian Word
ByteBuffer bb = ByteBuffer.wrap(buf);
bb.order(ByteOrder.BIG_ENDIAN);
if (bb.hasRemaining()) {
    short v = bb.getShort();
    return v;
return 0;
ByteBufferreadBinaryFile(File file)
read Binary File
ByteBuffer result = ByteBuffer.allocate((int) file.length());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
    result.put(buf, 0, len);
in.close();
...
float[]readBinaryFileAsFloats(String fileName)
read Binary File As Floats
FileInputStream fis = new FileInputStream(new File(fileName));
byte data[] = readFully(fis);
ByteBuffer bb = ByteBuffer.wrap(data);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
float result[] = new float[fb.capacity()];
fb.get(result);
return result;
...
byte[]readByte(String filePath)
read Byte
try {
    FileInputStream fis = new FileInputStream(filePath);
    FileChannel channel = fis.getChannel();
    ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
    while ((channel.read(byteBuffer)) > 0) {
    channel.close();
    fis.close();
...
byte[]readBytes(File f)
read Bytes
FileInputStream fin = null;
FileChannel ch = null;
try {
    fin = new FileInputStream(f);
    ch = fin.getChannel();
    int size = (int) ch.size();
    MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
    byte[] bytes = new byte[size];
...
ByteBufferreadBytes(final InputStream in, final int length)
read Bytes
final byte[] buffer = new byte[length];
in.read(buffer);
return ByteBuffer.wrap(buffer).asReadOnlyBuffer().order(ByteOrder.LITTLE_ENDIAN);
ByteBufferreadBytes(final String file)
read Bytes
final ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
try (RandomAccessFile f = new RandomAccessFile(file, "r"); final FileChannel fChannel = f.getChannel();) {
    fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));
return ByteBuffer.wrap(dataOut.toByteArray());