ByteBuffer to String from offset - Java java.nio

Java examples for java.nio:ByteBuffer String

Description

ByteBuffer to String from offset

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class Main {
    public static String toString(ByteBuffer buf, int startInc, int endExc,
            Charset charset) {//  ww w.jav a  2 s  . c o m
        int len = endExc - startInc;
        if (len == 0) {
            return "";
        }

        byte[] bytes = new byte[len];

        for (int i = 0; i < len; i++) {
            bytes[i] = buf.get(startInc + i);
        }

        return new String(bytes, charset);
    }
}

Related Tutorials