1package com.osroyale.fs.util;
3import java.io.ByteArrayOutputStream;
4import java.nio.ByteBuffer;
41* A
static-utility
class containing extension or helper methods for {@link
44 * @author Ryley Kimmel <ryley.kimmel@live.com>
46public final class ByteBufferUtil {
52 public static final char J_STRING_TERMINATOR =
'\n';
58 public static final char DEFAULT_STRING_TERMINATOR =
'\0';
67 public static int getMedium(ByteBuffer buffer) {
68 return (buffer.getShort() & 0xFFFF) << 8 | buffer.get() & 0xFF;
77 public static String getString(ByteBuffer buffer) {
78 return getString(buffer, DEFAULT_STRING_TERMINATOR);
87 public static String getJString(ByteBuffer buffer) {
88 return getString(buffer, J_STRING_TERMINATOR);
91 public static String readStringCp1252NullTerminated(ByteBuffer buffer) {
92 int var1 = buffer.position();
94 buffer.position(buffer.position() + 1);
95 byte b = buffer.get(buffer.position() - 1);
99 int var2 =buffer.position() - var1 - 1;
100 return var2 == 0 ?
"" : decodeStringCp1252(buffer, var1, var2);
103 public static String decodeStringCp1252(ByteBuffer buffer,
int var1,
int var2) {
104 char[] var3 =
new char[var2];
107 for(
int var5 = 0; var5 < var2; ++var5) {
108 int var6 = buffer.get(var5 + var1) & 255;
110 if (var6 >= 128 && var6 < 160) {
111 char var7 = cp1252AsciiExtension[var6 - 128];
119 var3[var4++] = (char)var6;
123 return new String(var3, 0, var4);
126 public static final char[] cp1252AsciiExtension =
new char[]{
'€',
'\u0000',
'‚',
'ƒ',
'„',
'…',
'†',
'‡',
'ˆ',
'‰',
'Š',
'‹',
'Œ',
'\u0000',
'Ž',
'\u0000',
'\u0000',
'‘',
'’',
'“',
'”',
'•',
'–',
'—',
'˜',
'™',
'š',
'›',
'œ',
'\u0000',
'ž',
'Ÿ'};
136 public static byte[]
get(ByteBuffer buffer,
int length) {
137 byte[] data =
new byte[length];
153 public static String getString(ByteBuffer buffer,
char terminator) {
154 ByteArrayOutputStream os =
new ByteArrayOutputStream();
156 int read = buffer.get() & 0xFF;
157 if (read == terminator) {
162 return new String(os.toByteArray());
172 public static int getSmart(
final ByteBuffer buffer) {
173 final int position = buffer.position();
174 if (position >= buffer.limit()) {
177 final int peek = buffer.get(position) & 0xFF;
179 return buffer.get() & 0xFF;
181 return (buffer.getShort() & 0xFFFF) - 32768;
190 public static void putSmart(ByteBuffer buffer,
int value) {
192 buffer.put((
byte) value);
195 buffer.put((
byte) (value >> 8));
196 buffer.put((
byte) value);
203 private ByteBufferUtil() {