Java ByteBuffer Get getJagexString(ByteBuffer buf)

Here you can find the source of getJagexString(ByteBuffer buf)

Description

Gets a null-terminated string from the specified buffer, using a modified ISO-8859-1 character set.

License

Open Source License

Parameter

Parameter Description
buf The buffer.

Return

The decoded string.

Declaration

public static String getJagexString(ByteBuffer buf) 

Method Source Code

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

import java.nio.ByteBuffer;

public class Main {
    /**/*from   ww w. j a  v  a  2 s. co  m*/
     * The modified set of 'extended ASCII' characters used by the client.
     */
    private static char CHARACTERS[] = { '\u20AC', '\0', '\u201A',
            '\u0192', '\u201E', '\u2026', '\u2020', '\u2021', '\u02C6',
            '\u2030', '\u0160', '\u2039', '\u0152', '\0', '\u017D', '\0',
            '\0', '\u2018', '\u2019', '\u201C', '\u201D', '\u2022',
            '\u2013', '\u2014', '\u02DC', '\u2122', '\u0161', '\u203A',
            '\u0153', '\0', '\u017E', '\u0178' };

    /**
     * Gets a null-terminated string from the specified buffer, using a
     * modified ISO-8859-1 character set.
     * @param buf The buffer.
     * @return The decoded string.
     */
    public static String getJagexString(ByteBuffer buf) {
        StringBuilder bldr = new StringBuilder();
        int b;
        while ((b = (buf.get() & 0xFF)) != 0) {
            if (b >= 127 && b < 160) {
                char curChar = CHARACTERS[b - 128];
                if (curChar != 0) {
                    bldr.append(curChar);
                }
            } else {
                bldr.append((char) b);
            }
        }
        return bldr.toString();
    }

    /**
     * Converts the contents of the specified byte buffer to a string, which is
     * formatted similarly to the output of the {@link Arrays#toString()}
     * method.
     * @param buffer The buffer.
     * @return The string.
     */
    public static String toString(ByteBuffer buffer) {
        StringBuilder builder = new StringBuilder("[");
        for (int i = 0; i < buffer.limit(); i++) {
            String hex = Integer.toHexString(buffer.get(i) & 0xFF)
                    .toUpperCase();
            if (hex.length() == 1)
                hex = "0" + hex;

            builder.append("0x").append(hex);
            if (i != buffer.limit() - 1) {
                builder.append(", ");
            }
        }
        builder.append("]");
        return builder.toString();
    }
}

Related

  1. getFromBack(ByteBuffer bb)
  2. getHashBytes(ByteBuffer bb)
  3. getHSBfromRGB(final ByteBuffer pixels, final float[] result, int pixelSize)
  4. getIncreasingByteBuffer(int len)
  5. getIPv4String(ByteBuffer buffer)
  6. getJceBufArray(ByteBuffer buffer)
  7. getLength(ByteBuffer buffer)
  8. getLengthFromBuffer(ByteBuffer in)
  9. getListFromByteBuffer(ByteBuffer data, List classes)