Java Convert via ByteBuffer toText(@Nonnull byte[] bytes)

Here you can find the source of toText(@Nonnull byte[] bytes)

Description

to Text

License

Apache License

Declaration

@Nonnull
    public static String toText(@Nonnull byte[] bytes) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import com.google.common.primitives.UnsignedBytes;

import java.nio.ByteBuffer;

import java.util.Arrays;

import javax.annotation.Nonnull;

public class Main {
    @Nonnull
    public static String toText(@Nonnull byte[] bytes) {
        StringBuilder buf = new StringBuilder();
        for (byte b : bytes) {
            if (Character.isValidCodePoint(b))
                buf.append((char) b);
            else/*from ww w . ja v a2  s  .c om*/
                buf.append("\\x").append(UnsignedBytes.toString(b, 16));
        }
        return buf.toString();
    }

    @Nonnull
    public static String toString(@Nonnull ByteBuffer buf, int len) {
        byte[] b = new byte[Math.min(buf.remaining(), len)];
        for (int i = 0; i < b.length; i++)
            b[i] = buf.get(buf.position() + i);
        return "[" + Arrays.toString(b) + "...(" + buf.remaining() + " bytes)]";
    }
}

Related

  1. toString(final Xid xid)
  2. toString(InputStream is)
  3. toString(Object o)
  4. toString(Reader r)
  5. toString(Reader reader)
  6. toThriftBinary(UUID uuid)
  7. toURI(String value)
  8. toUTF(byte[] bytes)
  9. toUTF8(String str)