Java FloatBuffer to String toString(FloatBuffer buffer)

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

Description

Utility method to toString a float buffer

License

Open Source License

Parameter

Parameter Description
buffer - the float buffer to toString to System.out

Declaration

public static String toString(FloatBuffer buffer) 

Method Source Code

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

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import java.nio.charset.StandardCharsets;

public class Main {
    /**//from  ww w .j av  a  2 s.  com
     * Utility method to toString a float buffer
     *
     * @param buffer - the float buffer to toString to System.out
     */
    public static String toString(FloatBuffer buffer) {
        StringBuilder str = new StringBuilder(buffer.capacity() * 2);
        str.append('[');
        for (int i = 0; i < buffer.capacity(); i++) {
            str.append(buffer.get(i));
            if (i != buffer.capacity() - 1) {
                str.append(", ");
            }
        }
        str.append(']');
        return str.toString();
    }

    public static String toString(ByteBuffer buffer) {
        byte[] inBytes = new byte[buffer.capacity()];
        buffer.get(inBytes);
        byte[] outBytes = inBytes;
        for (int i = 0; i < inBytes.length; i++) {
            byte b = inBytes[i];
            if (b == 0x00) {
                outBytes = new byte[i];
                System.arraycopy(inBytes, 0, outBytes, 0, i);
                break;
            }
        }
        return new String(outBytes, StandardCharsets.UTF_8);
    }
}

Related

  1. print(FloatBuffer buffer)
  2. printFloatBuffer(FloatBuffer fb)
  3. show(final FloatBuffer buffer)
  4. toString(FloatBuffer buffer)
  5. toString(FloatBuffer floatBuffer)