Java ByteBuffer Read readChars(ByteBuffer bb, int length)

Here you can find the source of readChars(ByteBuffer bb, int length)

Description

read Chars

License

LGPL

Declaration

public static String readChars(ByteBuffer bb, int length)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.IOException;

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

import java.nio.channels.SeekableByteChannel;

public class Main {
    private static final String CHARSET_ISO8859 = "ISO-8859-1";

    public static String readChars(SeekableByteChannel channel, int length)
            throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(length);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        channel.read(bb);// www. j  a  va  2  s. c o m
        bb.rewind();
        return readChars(bb, bb.remaining());
    }

    public static String readChars(ByteBuffer bb, int length)
            throws IOException {
        byte[] b = new byte[length];
        bb.get(b);

        // lookup null character for string termination
        int strLength = 0;
        for (int i = 0; i < b.length; i++) {
            if (b[i] == 0) {
                break;
            }
            strLength++;
        }

        return new String(b, 0, strLength, CHARSET_ISO8859);
    }
}

Related

  1. readBuf(ByteBuffer buffer)
  2. readBuffer(ByteChannel channel, ByteBuffer buffer)
  3. readBufferFully(FileChannel fc, ByteBuffer buf, int startPos)
  4. readByteBuffer(ByteBuffer buf)
  5. readByteBufferFromFile(String filepath)
  6. readCharsUTF8(ByteBuffer bb, int length)
  7. readCInt(ByteBuffer buffer)
  8. readCString(ByteBuffer buf, int len)
  9. readDERString(ByteBuffer buf)