Java ByteBuffer to String toStringBinary(ByteBuffer buf)

Here you can find the source of toStringBinary(ByteBuffer buf)

Description

Converts the given byte buffer, from its array offset to its limit, to a string.

License

Apache License

Parameter

Parameter Description
buf a byte buffer

Return

a string representation of the buffer's binary contents

Declaration

public static String toStringBinary(ByteBuffer buf) 

Method Source Code

//package com.java2s;
/*/*from   w w w. j  a v  a2  s  .c  o  m*/
 * Copyright 2015 The RPC Project
 *
 * The RPC Project licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

import java.io.UnsupportedEncodingException;

import java.nio.ByteBuffer;

public class Main {
    public static final String UTF8_ENCODING = "UTF-8";

    /**
     * Write a printable representation of a byte array.
     *
     * @param b byte array
     * @return string
     * @see #toStringBinary(byte[], int, int)
     */
    public static String toStringBinary(final byte[] b) {
        if (b == null)
            return "null";
        return toStringBinary(b, 0, b.length);
    }

    /**
     * Converts the given byte buffer, from its array offset to its limit, to
     * a string. The position and the mark are ignored.
     *
     * @param buf a byte buffer
     * @return a string representation of the buffer's binary contents
     */
    public static String toStringBinary(ByteBuffer buf) {
        if (buf == null)
            return "null";
        return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
    }

    /**
     * Write a printable representation of a byte array. Non-printable
     * characters are hex escaped in the format \\x%02X, eg:
     * \x00 \x05 etc
     *
     * @param b array to write out
     * @param off offset to start at
     * @param len length to write
     * @return string output
     */
    public static String toStringBinary(final byte[] b, int off, int len) {
        StringBuilder result = new StringBuilder();
        try {
            String first = new String(b, off, len, "ISO-8859-1");
            for (int i = 0; i < first.length(); ++i) {
                int ch = first.charAt(i) & 0xFF;
                if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
                        || " `~!@#$%^&*()-_=+[]{}\\|;:'\",.<>/?".indexOf(ch) >= 0) {
                    result.append(first.charAt(i));
                } else {
                    result.append(String.format("\\x%02X", ch));
                }
            }
        } catch (UnsupportedEncodingException e) {
            //LOG.error("ISO-8859-1 not supported?", e);
        }
        return result.toString();
    }

    /**
     * @param b Presumed UTF-8 encoded byte array.
     * @return String made from <code>b</code>
     */
    public static String toString(final byte[] b) {
        if (b == null) {
            return null;
        }
        return toString(b, 0, b.length);
    }

    /**
     * Joins two byte arrays together using a separator.
     * @param b1 The first byte array.
     * @param sep The separator to use.
     * @param b2 The second byte array.
     */
    public static String toString(final byte[] b1, String sep, final byte[] b2) {
        return toString(b1, 0, b1.length) + sep + toString(b2, 0, b2.length);
    }

    /**
     * This method will convert utf8 encoded bytes into a string. If
     * an UnsupportedEncodingException occurs, this method will eat it
     * and return null instead.
     *
     * @param b Presumed UTF-8 encoded byte array.
     * @param off offset into array
     * @param len length of utf-8 sequence
     * @return String made from <code>b</code> or null
     */
    public static String toString(final byte[] b, int off, int len) {
        return toString(b, off, len, UTF8_ENCODING);
    }

    public static String toString(final byte[] b, int off, int len, String encoding) {
        if (b == null) {
            return null;
        }
        if (len == 0) {
            return "";
        }
        try {
            return new String(b, off, len, encoding);
        } catch (UnsupportedEncodingException e) {
            //LOG.error("UTF-8 not supported?", e);
            return null;
        }
    }

    public static String toString(final byte[] b, int off, int len, String encoding, boolean trim) {
        if (b == null) {
            return null;
        }
        if (len == 0) {
            return "";
        }
        try {
            if (trim) {
                return new String(b, off, len, encoding).trim();
            } else {
                return new String(b, off, len, encoding);
            }
        } catch (UnsupportedEncodingException e) {
            //LOG.error("UTF-8 not supported?", e);
            return null;
        }
    }

    public static String toString(final byte[] b, int off, int len, boolean trim) {
        if (b == null) {
            return null;
        }
        if (len == 0) {
            return "";
        }
        try {
            if (trim) {
                return new String(b, off, len, UTF8_ENCODING).trim();
            } else {
                return new String(b, off, len, UTF8_ENCODING);
            }

        } catch (UnsupportedEncodingException e) {
            //LOG.error("UTF-8 not supported?", e);
            return null;
        }
    }
}

Related

  1. toString(ByteBuffer value, String charsetName)
  2. toString(final ByteBuffer buffer)
  3. toString(final ByteBuffer buffer)
  4. toString(final ByteBuffer buffer)
  5. toStringBinary(ByteBuffer buf)
  6. toText(ByteBuffer data, StringBuilder result, int cnt)
  7. toTextAll(ByteBuffer bytes)