extract Null Terminated String from ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer String

Description

extract Null Terminated String from ByteBuffer

Demo Code


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

public class Main {
    public static String extractNullTerminatedString(ByteBuffer bb) {
        int start = bb.position();

        byte[] buffer = new byte[bb.remaining()];

        bb.get(buffer);//www .jav a 2 s . c  o m

        String s = new String(buffer);
        int nullPos = s.indexOf(0);

        s = s.substring(0, nullPos);

        bb.position(start + s.length() + 1);

        return s;
    }
}

Related Tutorials