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

StringreadFlashString(DataInputStream s)
read Flash String
int len = s.readUnsignedShort();
if (len == 0)
    return null;
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++)
    bytes[i] = s.readByte();
return stringDecoder.decode(ByteBuffer.wrap(bytes)).toString();
float[]readFloat(BufferedReader br)
Read a float array from the stream without knowing its size ahead of time
String line = br.readLine();
if (line == null)
    return null;
String[] tokens = line.trim().split("\\s+");
float[] buf = new float[tokens.length];
for (int i = 0; i < tokens.length; ++i)
    buf[i] = Float.valueOf(tokens[i]);
return buf;
...
floatReadFloat(InputStream is)
Read Float
bb.position(0);
is.read(tempBuffer, 0, 4);
bb.put(tempBuffer, 0, 4);
return bb.getFloat(0);
StringreadFromBuffer(byte[] buffer, int start, int end)
Returns a string read from a buffer of bytes after replacing null character by the empty string.
String result = ASCII.decode(ByteBuffer.wrap(buffer, start, end)).toString();
Character c = Character.MIN_CODE_POINT;
result = result.replaceAll(c.toString(), "");
return result;
StringreadFromFile(Path path)
read From File
try (FileInputStream stream = new FileInputStream(path.toFile())) {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} catch (IOException e) {
    e.printStackTrace();
return "";
...
ByteBufferreadFromFile(String fileName)
read From File
RandomAccessFile fis = null;
MappedByteBuffer buf = null;
try {
    fis = new RandomAccessFile(fileName, "r");
    final FileChannel fc = fis.getChannel();
    int size = (int) fc.size();
    buf = fc.map(MapMode.READ_ONLY, 0, size);
    return buf;
...
byte[]readFromSocket(SocketChannel channel)
read From Socket
ByteBuffer bb = ByteBuffer.allocate(256);
int bytes = 0;
int totalBytes = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((bytes = channel.read(bb)) != -1) {
    totalBytes += bytes;
    if (totalBytes == 256 || !bb.hasRemaining()) {
        baos.write(bb.array(), 0, 256);
...
StringBuilderreadFull(Reader in)
read Full
char buf[] = new char[4 * 1024];
StringBuilder sb = new StringBuilder();
for (;;) {
    int read = in.read(buf);
    if (read == -1) {
        break;
    sb.append(buf, 0, read);
...
voidreadFully(final Reader input, final char[] buffer, final int offset, final int length)
Reads the requested number of characters or fail if there are not enough left.
final int actual = read(input, buffer, offset, length);
if (actual != length) {
    throw new EOFException("Length to read: " + length + " actual: " + actual);
byte[]readFully(InputStream in)
Reads until the end of the input stream, and returns the contents as a byte[]
List<ByteBuffer> buffers = new ArrayList<ByteBuffer>(4);
while (true) {
    ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
    int count = in.read(buffer.array(), 0, BUFFER_SIZE);
    if (count > 0) {
        buffer.limit(count);
        buffers.add(buffer);
    if (count < BUFFER_SIZE)
        break;
return getAsBytes(buffers);