Java ByteBuffer to String getString(ByteBuffer buffer, int size)

Here you can find the source of getString(ByteBuffer buffer, int size)

Description

get String

License

Open Source License

Declaration

public static String getString(ByteBuffer buffer, int size) 

Method Source Code

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

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

public class Main {
    public static String getString(ByteBuffer buffer, int size) {

        assert (0 < size);
        assert (buffer.capacity() - buffer.position() >= size);

        ByteBuffer buf = ByteBuffer.allocate(size);

        buffer.get(buf.array(), 0, size);

        try {//from w ww . j  a  v  a 2 s  . c o  m
            return Charset.defaultCharset().newDecoder().decode(buf).toString().trim();
        } catch (Exception e) {
            e.printStackTrace();
            assert (false);

            return "";
        }
    }
}

Related

  1. getString(ByteBuffer buffer)
  2. getString(ByteBuffer buffer)
  3. getString(ByteBuffer buffer, int length)
  4. getString(ByteBuffer buffer, int offset, int length, String encoding)
  5. getString(ByteBuffer buffer, int size)
  6. getString(ByteBuffer buffer, String charFormat)
  7. getString(ByteBuffer byteBuffer, int size)
  8. getString(ByteBuffer in, int maxLength)
  9. getString(final ByteBuffer buffer)