Java ByteBuffer to String getStringFromBuffer(ByteBuffer buf, int len)

Here you can find the source of getStringFromBuffer(ByteBuffer buf, int len)

Description

get String From Buffer

License

Apache License

Declaration

public static String getStringFromBuffer(ByteBuffer buf, int len) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    public static String getStringFromBuffer(ByteBuffer buf, int len) {
        byte[] bytes = new byte[len];
        buf.get(bytes);//from   w ww  .  j ava2  s  . co m
        return esc0(new String(bytes));
    }

    public static String esc0(String s) {
        if (s == null || s.length() == 0) {
            s = "";
            return s;
        } else {
            int i = s.indexOf('\0');
            if (i > 0)
                s = s.substring(0, i);
            else
                s = s.replaceAll("\0", "");
        }
        return s;
    }
}

Related

  1. getString(final ByteBuffer buffer)
  2. getString(final ByteBuffer buffer, final int offset, final int length, final Charset encoding)
  3. getString(java.nio.ByteBuffer buffer, int offset, int len)
  4. getStringA(ByteBuffer byteBuffer, int length)
  5. getStringDTrimmed(ByteBuffer byteBuffer, int length)
  6. getStringFromBuffer(ByteBuffer buffer, int length)
  7. getStringFromByteBuffer(ByteBuffer bb)
  8. getStringFromByteBuffer(ByteBuffer data)
  9. getStringRepresentation(ByteBuffer key)