Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mango.messages;
- import java.nio.charset.Charset;
- import org.jboss.netty.buffer.ChannelBuffer;
- import org.jboss.netty.buffer.ChannelBuffers;
- import com.mango.utils.B64Encoding;
- import com.mango.utils.WiredEncoding;
- public class ServerMessage
- {
- private ChannelBuffer body;
- public ServerMessage(int id)
- {
- this.body = ChannelBuffers.dynamicBuffer();
- this.body.writeBytes(B64Encoding.EncodeInt32(id, 2));
- }
- public void appendByte(Byte b)
- {
- this.body.writeByte(b);
- }
- public void appendBytes(byte[] b)
- {
- this.body.writeBytes(b);
- }
- public void appendRawString(String str)
- {
- this.body.writeBytes(str.getBytes(Charset.forName("UTF-8")));
- }
- public void appendRawDouble(Double d)
- {
- Long db = Math.round(d);
- String raw = db.toString();
- if (raw.length() == 1)
- {
- raw += ".0";
- }
- appendString(raw.replace(",", "."));
- }
- public void appendString(String str)
- {
- appendString(str, (byte)2);
- }
- public void appendString(String str, byte breakChar)
- {
- appendRawString(str);
- appendByte(breakChar);
- }
- public void appendInt(Integer i)
- {
- appendBytes(WiredEncoding.EncodeInt32(i));
- }
- public void appendRawInt(Integer i)
- {
- appendRawString(i.toString());
- }
- public void appendBoolean(boolean state)
- {
- this.body.writeByte(state == true ? WiredEncoding.POSITIVE : WiredEncoding.NEGATIVE);
- }
- private void writeClosing()
- {
- this.body.writeByte(1);
- }
- public ChannelBuffer get()
- {
- writeClosing();
- return this.body;
- }
- }
Add Comment
Please, Sign In to add comment