Java ByteBuffer to String toString(ByteBuffer value, String charsetName)

Here you can find the source of toString(ByteBuffer value, String charsetName)

Description

convert a ByteBuffer to a String ByteBuffer is reset to its original state.

License

Open Source License

Declaration

static public String toString(ByteBuffer value, String charsetName) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    /**//from  ww  w  . ja  v a 2 s  . c o  m
     * convert a ByteBuffer to a String
     * ByteBuffer is reset to its original state.
     */
    static public String toString(ByteBuffer value, String charsetName) {
        String result = null;
        try {
            result = new String(toBytes(value), charsetName);
        } catch (Exception e) {
        }
        return result;
    }

    static public String toString(int value, int radix, int minChars) {
        String result = Integer.toString(value, radix);
        int leadZeroes = minChars - result.length();
        if (leadZeroes > 0) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < leadZeroes; i++) {
                buf.append("0");
            }
            buf.append(result);
            result = buf.toString();
        }
        return result;
    }

    /**
     * Convert a ByteBuffer to a byte[]
     * ByteBuffer is reset to its original state.
     */
    static public byte[] toBytes(ByteBuffer value) {
        byte[] result = null;
        if (value != null && value.remaining() > 0) {
            result = new byte[value.remaining()];
            value.mark();
            value.get(result);
            value.reset();
        }
        return result;
    }
}

Related

  1. toString(ByteBuffer buffer, int offset, int length)
  2. toString(ByteBuffer buffer, String encoding)
  3. toString(ByteBuffer bytes)
  4. toString(ByteBuffer bytes)
  5. toString(ByteBuffer sequence)
  6. toString(final ByteBuffer buffer)
  7. toString(final ByteBuffer buffer)
  8. toString(final ByteBuffer buffer)
  9. toStringBinary(ByteBuffer buf)