Java ByteBuffer to String toString(ByteBuffer buffer)

Here you can find the source of toString(ByteBuffer buffer)

Description

to String

License

Apache License

Declaration

public static String toString(ByteBuffer buffer) 

Method Source Code


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

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import java.util.Map;
import java.util.Set;

public class Main {
    public static String toString(Object... args) {
        StringBuilder s = new StringBuilder();
        for (Object arg : args) {
            if (arg != null) {
                s.append(arg);/*  w w  w . jav  a 2 s  .  c  om*/
            }
        }
        return s.toString();
    }

    public static String toString(ByteBuffer buffer) {
        /// Create a StringBuffer so that we can convert the bytes to a String
        StringBuffer response = new StringBuffer();

        // Create a CharSet that knows how to encode and decode standard text (UTF-8)
        Charset charset = Charset.forName("UTF-8");

        // Decode the buffer to a String using the CharSet and append it to our buffer
        response.append(charset.decode(buffer));
        buffer.flip();
        return response.toString();
    }

    public static String toString(Map<String, String> map, String kvDelimeter, String eDelimeter) {
        StringBuilder s = new StringBuilder();
        Set<String> keys = map.keySet();
        for (String k : keys) {
            String v = map.get(k);
            if (s.length() == 0) {
                s.append(k).append(kvDelimeter).append(v);
            } else {
                s.append(eDelimeter).append(k).append(kvDelimeter).append(v);
            }
        }
        return s.toString();
    }
}

Related

  1. toString(ByteBuffer buf)
  2. toString(ByteBuffer buf, int len)
  3. toString(ByteBuffer buffer)
  4. toString(ByteBuffer buffer)
  5. toString(ByteBuffer buffer)
  6. toString(ByteBuffer buffer)
  7. toString(ByteBuffer buffer)
  8. toString(ByteBuffer buffer)
  9. toString(ByteBuffer buffer, Charset charset)