Java ByteBuffer to String toText(ByteBuffer data, StringBuilder result, int cnt)

Here you can find the source of toText(ByteBuffer data, StringBuilder result, int cnt)

Description

Gets last cnt read bytes from the data buffer and puts into result buffer in special format:
  • if byte represents char from partition 0x1F to 0x80 (which are normal ascii chars) then it's put into buffer as it is
  • otherwise dot is put into buffer

License

Open Source License

Parameter

Parameter Description
data a parameter
result a parameter
cnt a parameter

Declaration

private static void toText(ByteBuffer data, StringBuilder result, int cnt) 

Method Source Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**/*from   ww w.j  a  va 2  s .c  o  m*/
     * Gets last <tt>cnt</tt> read bytes from the <tt>data</tt> buffer and puts into <tt>result</tt> buffer in special
     * format:
     * <ul>
     * <li>if byte represents char from partition 0x1F to 0x80 (which are normal ascii chars) then it's put into buffer as
     * it is</li>
     * <li>otherwise dot is put into buffer</li>
     * </ul>
     *
     * @param data
     * @param result
     * @param cnt
     */
    private static void toText(ByteBuffer data, StringBuilder result, int cnt) {
        int charPos = data.position() - cnt;
        for (int a = 0; a < cnt; a++) {
            int c = data.get(charPos++);
            if (c > 0x1f && c < 0x80)
                result.append((char) c);
            else
                result.append('.');
        }
    }
}

Related

  1. toString(final ByteBuffer buffer)
  2. toString(final ByteBuffer buffer)
  3. toString(final ByteBuffer buffer)
  4. toStringBinary(ByteBuffer buf)
  5. toStringBinary(ByteBuffer buf)
  6. toTextAll(ByteBuffer bytes)