Java ByteBuffer to String getStringFromByteBuffer(ByteBuffer bb)

Here you can find the source of getStringFromByteBuffer(ByteBuffer bb)

Description

Returns a String representation of a ByteBuffer.

License

Open Source License

Parameter

Parameter Description
bb ByteBuffer to be converted to a string

Return

String message

Declaration

public static String getStringFromByteBuffer(ByteBuffer bb) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    /**/* w w w.j  a  va2s.  c o m*/
     * Returns a String representation of a ByteBuffer.
     * @param bb ByteBuffer to be converted to a string
     * @return String message
     */
    public static String getStringFromByteBuffer(ByteBuffer bb) {
        StringBuilder message = new StringBuilder();
        int bytes;
        while (true) {
            try {
                bytes = bb.get();
                // format the product of two bytes and a bitwise AND with 0xFF
                message.append("\\x" + String.format("%02x", bytes & 0xff));
            } catch (Exception e) {
                break;
            }
        }
        return message.toString();
    }
}

Related

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