Arcturus-Community/src/main/java/com/eu/habbo/messages/ClientMessage.java

81 lines
1.8 KiB
Java
Raw Normal View History

2018-07-06 15:30:00 +02:00
package com.eu.habbo.messages;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.Charset;
2019-05-26 20:14:53 +02:00
public class ClientMessage {
2018-07-06 15:30:00 +02:00
private final int header;
private final ByteBuf buffer;
2019-05-26 20:14:53 +02:00
public ClientMessage(int messageId, ByteBuf buffer) {
this.header = messageId;
this.buffer = ((buffer == null) || (buffer.readableBytes() == 0) ? Unpooled.EMPTY_BUFFER : buffer);
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public ByteBuf getBuffer() {
return this.buffer;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public int getMessageId() {
return this.header;
2018-07-06 15:30:00 +02:00
}
2019-05-26 20:14:53 +02:00
public ClientMessage clone() throws CloneNotSupportedException {
2018-07-06 15:30:00 +02:00
return new ClientMessage(this.header, this.buffer.duplicate());
}
2019-05-26 20:14:53 +02:00
public int readShort() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readShort();
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return 0;
}
2019-05-26 20:14:53 +02:00
public Integer readInt() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readInt();
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return 0;
}
2019-05-26 20:14:53 +02:00
public boolean readBoolean() {
try {
2018-07-06 15:30:00 +02:00
return this.buffer.readByte() == 1;
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
}
return false;
}
2019-05-26 20:14:53 +02:00
public String readString() {
try {
2019-03-18 02:22:00 +01:00
int length = this.readShort();
2018-07-06 15:30:00 +02:00
byte[] data = new byte[length];
this.buffer.readBytes(data);
2019-03-18 02:22:00 +01:00
return new String(data);
2019-05-26 20:14:53 +02:00
} catch (Exception e) {
2018-07-06 15:30:00 +02:00
return "";
}
}
2019-05-26 20:14:53 +02:00
public String getMessageBody() {
2018-07-06 15:30:00 +02:00
String consoleText = this.buffer.toString(Charset.defaultCharset());
for (int i = -1; i < 31; i++) {
2019-05-26 20:14:53 +02:00
consoleText = consoleText.replace(Character.toString((char) i), "[" + i + "]");
2018-07-06 15:30:00 +02:00
}
return consoleText;
}
2019-05-26 20:14:53 +02:00
public int bytesAvailable() {
2018-07-06 15:30:00 +02:00
return this.buffer.readableBytes();
}
}