1package com.osroyale.util;
3import com.osroyale.game.world.entity.mob.player.relations.ChatMessage;
5import java.nio.ByteBuffer;
39* A utility
class for encoding/decoding messages.
43public final class ChatCodec {
48 private static final int MAX_COMPRESS_LEN = 80;
55 private static final char[] CHARACTER_TABLE = {
56 ' ',
'e',
't',
'a',
'o',
'i',
'h',
'n',
's',
'r',
57 'd',
'l',
'u',
'm',
'w',
'c',
'y',
'f',
'g',
'p',
58 'b',
'v',
'k',
'x',
'j',
'q',
'z',
'0',
'1',
'2',
59 '3',
'4',
'5',
'6',
'7',
'8',
'9',
' ',
'!',
'?',
60 '.',
',',
':',
';',
'(',
')',
'-',
'&',
'*',
'\\',
61 '\'',
'@',
'#',
'+',
'=',
'\243',
'$',
'%',
'"',
62 '[',
']',
'_',
'/',
'<',
'>',
'^',
'|'
80 public static byte[] encode(String input) {
81 if (input.length() > ChatMessage.CHARACTER_LIMIT) {
82 input = input.substring(0, ChatMessage.CHARACTER_LIMIT).toLowerCase();
84 input = input.toLowerCase();
87 ByteBuffer buffer = ByteBuffer.allocate(ChatMessage.CHARACTER_LIMIT);
90 for (
int i = 0; i < input.length(); i++) {
91 char key = input.charAt(i);
94 for (
int n = 0; n < CHARACTER_TABLE.length; n++) {
95 if (key != CHARACTER_TABLE[n]) {
104 buffer.put((
byte) index);
107 final byte[] temp =
new byte[count];
108 System.arraycopy(buffer.array(), 0, temp, 0, temp.length);
121 public static String decode(
byte[] input) {
122 if (input.length > MAX_COMPRESS_LEN) {
123 byte[] temp =
new byte[MAX_COMPRESS_LEN];
124 System.arraycopy(input, 0, temp, 0, temp.length);
128 final ByteBuffer buffer = ByteBuffer.wrap(input);
129 final StringBuilder builder =
new StringBuilder();
130 for (
int i = 0; i < buffer.capacity(); i++) {
131 final int index = buffer.get() & 0xFF;
132 if (index < CHARACTER_TABLE.length) {
133 builder.append(CHARACTER_TABLE[index]);
136 return filter(builder.toString());
147 public static String filter(String input) {
148 final StringBuilder builder =
new StringBuilder();
149 for (
char c : input.toLowerCase().toCharArray()) {
150 for (
char validChar : CHARACTER_TABLE) {
151 if (c == validChar) {
158 boolean capitalize =
true;
160 int length = builder.length();
162 for (
int index = 0; index < length; index++) {
163 char character = builder.charAt(index);
165 if (character ==
'.' || character ==
'!' || character ==
'?') {
167 }
else if (capitalize && !Character.isWhitespace(character)) {
168 builder.setCharAt(index, Character.toUpperCase(character));
171 builder.setCharAt(index, Character.toLowerCase(character));
175 return builder.toString();