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

booleanreadDouble(BufferedReader br, double[] buf)
Reads doubles from ASCII stream
String line = br.readLine();
if (line == null)
    return false;
String[] tokens = line.trim().split("\\s+");
if (tokens.length != buf.length)
    return false;
for (int i = 0; i < tokens.length; ++i)
    buf[i] = Double.valueOf(tokens[i]);
...
doublereadDouble(byte[] src, int pointer)
read Double
return Double.longBitsToDouble(readLong(src, pointer));
doublereadDouble(FileChannel fileChannel, ByteOrder byteOrder)
read Double
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.order(byteOrder);
fileChannel.read(byteBuffer);
return byteBuffer.getDouble(0);
doublereadDouble(final byte[] bytes, final int length, final int offset)
Returns a double representing the bytes.
double output = -1d;
if (bytes == null) {
    return output;
final byte[] doubleBytes = new byte[length];
for (int i = 0; i < doubleBytes.length; i++) {
    doubleBytes[i] = bytes[offset + i];
output = ByteBuffer.wrap(doubleBytes).getDouble();
return output;
java.nio.ByteBufferreadEntireFile(java.io.File file)
read Entire File
return readFileToBuffer(file);
StringreaderToString(Reader reader)
Read all data from the given reader into a buffer and return it as a single String.
assert reader != null;
StringWriter strWriter = new StringWriter();
char[] buffer = new char[65536];
int charsRead = reader.read(buffer);
while (charsRead > 0) {
    strWriter.write(buffer, 0, charsRead);
    charsRead = reader.read(buffer);
reader.close();
return strWriter.toString();
float[]readFC(String fname, int length)
read FC
float[] res = new float[length];
FileChannel inChannel = new RandomAccessFile(fname, "rw").getChannel();
ByteBuffer buffer = ByteBuffer.allocate(4 * res.length);
inChannel.read(buffer);
buffer.flip();
FloatBuffer buffer2 = buffer.asFloatBuffer();
for (int i = 0; i < res.length; i++) {
    res[i] = buffer2.get(i);
...
StringreadFile(File file)
read File
if (!file.exists() || file.isDirectory() || !file.canRead()) {
    return null;
FileInputStream fileInputStream = new FileInputStream(file);
return readFile(fileInputStream);
ByteBufferreadFile(File file)
read File
byte[] buffer = new byte[(int) file.length()];
int bytesRead;
try (FileInputStream fileReader = new FileInputStream(file)) {
    bytesRead = fileReader.read(buffer);
assert bytesRead == file.length() : file;
return ByteBuffer.wrap(buffer);
CharBufferreadFile(File file)
Read the file contents of the given file using the default character set.
FileChannel channel = null;
FileInputStream stream = null;
try {
    stream = new FileInputStream(file);
    channel = stream.getChannel();
    ByteBuffer byteBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
    CharBuffer charBuffer = Charset.defaultCharset().newDecoder().decode(byteBuffer);
    return charBuffer;
...