RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
ByteBufferUtil.java
1package com.osroyale.fs.util;
2
3import java.io.ByteArrayOutputStream;
4import java.nio.ByteBuffer;
5
40
41* A static-utility class containing extension or helper methods for {@link
42 * ByteBuffer}s.
43 *
44 * @author Ryley Kimmel <ryley.kimmel@live.com>
45 */
46public final class ByteBufferUtil {
47
52 public static final char J_STRING_TERMINATOR = '\n';
53
58 public static final char DEFAULT_STRING_TERMINATOR = '\0';
59
67 public static int getMedium(ByteBuffer buffer) {
68 return (buffer.getShort() & 0xFFFF) << 8 | buffer.get() & 0xFF;
69 }
70
77 public static String getString(ByteBuffer buffer) {
78 return getString(buffer, DEFAULT_STRING_TERMINATOR);
79 }
80
87 public static String getJString(ByteBuffer buffer) {
88 return getString(buffer, J_STRING_TERMINATOR);
89 }
90
91 public static String readStringCp1252NullTerminated(ByteBuffer buffer) {
92 int var1 = buffer.position();
93 while (true) {
94 buffer.position(buffer.position() + 1);
95 byte b = buffer.get(buffer.position() - 1);
96 if (b == 0) break;
97 }
98
99 int var2 =buffer.position() - var1 - 1;
100 return var2 == 0 ? "" : decodeStringCp1252(buffer, var1, var2);
101 }
102
103 public static String decodeStringCp1252(ByteBuffer buffer, int var1, int var2) {
104 char[] var3 = new char[var2];
105 int var4 = 0;
106
107 for(int var5 = 0; var5 < var2; ++var5) {
108 int var6 = buffer.get(var5 + var1) & 255;
109 if (var6 != 0) {
110 if (var6 >= 128 && var6 < 160) {
111 char var7 = cp1252AsciiExtension[var6 - 128];
112 if (var7 == 0) {
113 var7 = '?';
114 }
115
116 var6 = var7;
117 }
118
119 var3[var4++] = (char)var6;
120 }
121 }
122
123 return new String(var3, 0, var4);
124 }
125
126 public static final char[] cp1252AsciiExtension = new char[]{'€', '\u0000', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u0000', 'Ž', '\u0000', '\u0000', '‘', '’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›', 'œ', '\u0000', 'ž', 'Ÿ'};
127
128
136 public static byte[] get(ByteBuffer buffer, int length) {
137 byte[] data = new byte[length];
138 buffer.get(data);
139 return data;
140 }
141
153 public static String getString(ByteBuffer buffer, char terminator) {
154 ByteArrayOutputStream os = new ByteArrayOutputStream();
155 for (; ; ) {
156 int read = buffer.get() & 0xFF;
157 if (read == terminator) {
158 break;
159 }
160 os.write(read);
161 }
162 return new String(os.toByteArray());
163 }
164
172 public static int getSmart(final ByteBuffer buffer) {
173 final int position = buffer.position();
174 if (position >= buffer.limit()) {
175 return 0;
176 }
177 final int peek = buffer.get(position) & 0xFF;
178 if (peek < 128) {
179 return buffer.get() & 0xFF;
180 }
181 return (buffer.getShort() & 0xFFFF) - 32768;
182 }
183
190 public static void putSmart(ByteBuffer buffer, int value) {
191 if (value < 128) {
192 buffer.put((byte) value);
193 } else {
194 value += 32768;
195 buffer.put((byte) (value >> 8));
196 buffer.put((byte) value);
197 }
198 }
199
203 private ByteBufferUtil() {
204 }
205
206}