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 ww  w .  j a v  a 2  s .  c om*/
 * Copyright 2010 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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 DEFAULT_CHARSET = "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();
        for (int i = off; i < off + len; ++i) {
            int ch = b[i] & 0xFF;
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
                    || " `~!@#$%^&*()-_=+[]{}|;:'\",.<>/?".indexOf(ch) >= 0) {
                result.append((char) ch);
            } else {
                result.append(String.format("\\x%02X", ch));
            }
        }
        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) throws UnsupportedEncodingException {
        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)
            throws UnsupportedEncodingException {
        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 DEFAULT_CHARSET sequence
     * @return String made from <code>b</code> or null
     */
    public static String toString(final byte[] b, int off, int len) throws UnsupportedEncodingException {
        if (b == null) {
            return null;
        }
        if (len == 0) {
            return "";
        }
        try {
            return new String(b, off, len, DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            throw e;
        }
    }
}

Related

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