Java File Read via ByteBuffer readString(ByteArrayInputStream bin)

Here you can find the source of readString(ByteArrayInputStream bin)

Description

read String

License

Open Source License

Declaration

public static String readString(ByteArrayInputStream bin) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
    private final static String ENCODING = "UTF-8";

    public static String readString(ByteArrayInputStream bin) throws IOException {
        int length = readInt(bin);
        if (length == -1)
            return null;
        if (length == 0)
            return "";
        byte[] buffer = new byte[length];
        bin.read(buffer);//  w  w  w.jav  a2s.  c  o  m
        return new String(buffer, ENCODING);
    }

    /**
     * Reads the next 4 bytes from the stream as integer.
     */
    public static int readInt(ByteArrayInputStream bin) throws IOException {
        byte[] buffer = new byte[4];
        bin.read(buffer);
        return ByteBuffer.wrap(buffer).getInt();
    }
}

Related

  1. readNIOFile(String filePath)
  2. readShort(byte[] bytes, int start)
  3. readShort(DataInput input)
  4. readShortBuffer(DataInputStream input)
  5. readShortByteArray(DataInput in)
  6. readString(ReadableByteChannel channel)
  7. readStringFromFile(String path)
  8. readStringFromSocketChannel(SocketChannel sc)
  9. readStringInUTF8(DataInput in)