RuneHive-Game
Loading...
Searching...
No Matches
ByteBufferUtil.java
Go to the documentation of this file.
1package com.runehive.fs.util;
2
3import java.io.ByteArrayOutputStream;
4import java.nio.ByteBuffer;
5
6/**
7 * A static-utility class containing extension or helper methods for {@link
8 * ByteBuffer}s.
9 *
10 * @author Ryley Kimmel <ryley.kimmel@live.com>
11 */
12public final class ByteBufferUtil {
13
14 /**
15 * The terminator used within the client, equal to <tt>10</tt> and otherwise
16 * know as the Jagex {@code String} terminator.
17 */
18 public static final char J_STRING_TERMINATOR = '\n';
19
20 /**
21 * The default {@code String} terminator, equal to <tt>0</tt> and otherwise
22 * known as the 'null' {@code String} terminator.
23 */
24 public static final char DEFAULT_STRING_TERMINATOR = '\0';
25
26 /**
27 * Gets a 24-bit medium integer from the specified {@link ByteBuffer}, this
28 * method does not mark the ByteBuffers current position.
29 *
30 * @param buffer The ByteBuffer to read from.
31 * @return The read 24-bit medium integer.
32 */
33 public static int getMedium(ByteBuffer buffer) {
34 return (buffer.getShort() & 0xFFFF) << 8 | buffer.get() & 0xFF;
35 }
36
37 /**
38 * Gets a null-terminated String from the specified ByteBuffer.
39 *
40 * @param buffer The ByteBuffer to read from.
41 * @return The null-terminated String.
42 */
43 public static String getString(ByteBuffer buffer) {
45 }
46
47 /**
48 * Gets a newline-terminated String from the specified ByteBuffer.
49 *
50 * @param buffer The ByteBuffer to read from.
51 * @return The newline-terminated String.
52 */
53 public static String getJString(ByteBuffer buffer) {
54 return getString(buffer, J_STRING_TERMINATOR);
55 }
56
57 public static String readStringCp1252NullTerminated(ByteBuffer buffer) {
58 int var1 = buffer.position();
59 while (true) {
60 buffer.position(buffer.position() + 1);
61 byte b = buffer.get(buffer.position() - 1);
62 if (b == 0) break;
63 }
64
65 int var2 =buffer.position() - var1 - 1;
66 return var2 == 0 ? "" : decodeStringCp1252(buffer, var1, var2);
67 }
68
69 public static String decodeStringCp1252(ByteBuffer buffer, int var1, int var2) {
70 char[] var3 = new char[var2];
71 int var4 = 0;
72
73 for(int var5 = 0; var5 < var2; ++var5) {
74 int var6 = buffer.get(var5 + var1) & 255;
75 if (var6 != 0) {
76 if (var6 >= 128 && var6 < 160) {
77 char var7 = cp1252AsciiExtension[var6 - 128];
78 if (var7 == 0) {
79 var7 = '?';
80 }
81
82 var6 = var7;
83 }
84
85 var3[var4++] = (char)var6;
86 }
87 }
88
89 return new String(var3, 0, var4);
90 }
91
92 public static final char[] cp1252AsciiExtension = new char[]{'€', '\u0000', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u0000', 'Ž', '\u0000', '\u0000', '‘', '’', '“', '”', '•', '–', '—', '˜', '™', 'š', '›', 'œ', '\u0000', 'ž', 'Ÿ'};
93
94
95 /**
96 * Reads {@code length} bytes from the specified {@link ByteBuffer}.
97 *
98 * @param buffer The ByteBuffer to read from.
99 * @param length The amount of bytes to read.
100 * @return The read bytes.
101 */
102 public static byte[] get(ByteBuffer buffer, int length) {
103 byte[] data = new byte[length];
104 buffer.get(data);
105 return data;
106 }
107
108 /**
109 * Gets a {@link String} from the specified {@link ByteBuffer}, the
110 * ByteBuffer will continue to get until the specified {@code terminator} is
111 * reached. <p> We use a {@link ByteArrayOutputStream} as it is self
112 * expanding. We don't want to waste precious time determining a fixed
113 * length for the {@code String}. </p>
114 *
115 * @param buffer The ByteBuffer to read from.
116 * @param terminator The terminator which denotes when to stop reading.
117 * @return The read String.
118 */
119 public static String getString(ByteBuffer buffer, char terminator) {
120 ByteArrayOutputStream os = new ByteArrayOutputStream();
121 for (; ; ) {
122 int read = buffer.get() & 0xFF;
123 if (read == terminator) {
124 break;
125 }
126 os.write(read);
127 }
128 return new String(os.toByteArray());
129 }
130
131 /**
132 * Reads a 'smart' (either a {@code byte} or {@code short} depending on the
133 * value) from the specified buffer.
134 *
135 * @param buffer The buffer.
136 * @return The 'smart'.
137 */
138 public static int getSmart(final ByteBuffer buffer) {
139 final int position = buffer.position();
140 if (position >= buffer.limit()) {
141 return 0;
142 }
143 final int peek = buffer.get(position) & 0xFF;
144 if (peek < 128) {
145 return buffer.get() & 0xFF;
146 }
147 return (buffer.getShort() & 0xFFFF) - 32768;
148 }
149
150 /**
151 * Puts a 'smart' (either a {@code byte} or {@code short}.
152 *
153 * @param buffer The buffer.
154 * @param value The value to write.
155 */
156 public static void putSmart(ByteBuffer buffer, int value) {
157 if (value < 128) {
158 buffer.put((byte) value);
159 } else {
160 value += 32768;
161 buffer.put((byte) (value >> 8));
162 buffer.put((byte) value);
163 }
164 }
165
166 /**
167 * Sole private constructor to discourage instantiation of this class.
168 */
169 private ByteBufferUtil() {
170 }
171
172}
static String getString(ByteBuffer buffer, char terminator)
Gets a String from the specified ByteBuffer, the ByteBuffer will continue to get until the specified ...
static final char J_STRING_TERMINATOR
The terminator used within the client, equal to 10 and otherwise know as the Jagex String terminator.
static String readStringCp1252NullTerminated(ByteBuffer buffer)
static void putSmart(ByteBuffer buffer, int value)
Puts a 'smart' (either a byte or short.
ByteBufferUtil()
Sole private constructor to discourage instantiation of this class.
static int getMedium(ByteBuffer buffer)
Gets a 24-bit medium integer from the specified ByteBuffer, this method does not mark the ByteBuffers...
static String getString(ByteBuffer buffer)
Gets a null-terminated String from the specified ByteBuffer.
static final char DEFAULT_STRING_TERMINATOR
The default String terminator, equal to 0 and otherwise known as the 'null' String terminator.
static int getSmart(final ByteBuffer buffer)
Reads a 'smart' (either a byte or short depending on the value) from the specified buffer.
static String getJString(ByteBuffer buffer)
Gets a newline-terminated String from the specified ByteBuffer.
static String decodeStringCp1252(ByteBuffer buffer, int var1, int var2)