Guest User

Untitled

a guest
Jan 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package com.mango.messages;
  2.  
  3. import java.nio.charset.Charset;
  4.  
  5. import org.jboss.netty.buffer.ChannelBuffer;
  6. import org.jboss.netty.buffer.ChannelBuffers;
  7.  
  8. import com.mango.utils.B64Encoding;
  9. import com.mango.utils.WiredEncoding;
  10.  
  11. public class ServerMessage
  12. {
  13.     private ChannelBuffer body;
  14.    
  15.     public ServerMessage(int id)
  16.     {
  17.         this.body = ChannelBuffers.dynamicBuffer();
  18.         this.body.writeBytes(B64Encoding.EncodeInt32(id, 2));
  19.     }
  20.    
  21.     public void appendByte(Byte b)
  22.     {
  23.         this.body.writeByte(b);
  24.     }
  25.  
  26.     public void appendBytes(byte[] b)
  27.     {
  28.         this.body.writeBytes(b);
  29.     }
  30.  
  31.     public void appendRawString(String str)
  32.     {
  33.         this.body.writeBytes(str.getBytes(Charset.forName("UTF-8")));
  34.     }
  35.  
  36.     public void appendRawDouble(Double d)
  37.     {
  38.         Long db = Math.round(d);
  39.         String raw = db.toString();
  40.  
  41.         if (raw.length() == 1)
  42.         {
  43.             raw += ".0";
  44.         }
  45.  
  46.         appendString(raw.replace(",", "."));
  47.     }
  48.  
  49.     public void appendString(String str)
  50.     {
  51.         appendString(str, (byte)2);
  52.     }
  53.  
  54.     public void appendString(String str, byte breakChar)
  55.     {
  56.         appendRawString(str);
  57.         appendByte(breakChar);
  58.     }
  59.  
  60.     public void appendInt(Integer i)
  61.     {
  62.         appendBytes(WiredEncoding.EncodeInt32(i));
  63.     }
  64.  
  65.     public void appendRawInt(Integer i)
  66.     {
  67.         appendRawString(i.toString());
  68.     }
  69.    
  70.     public void appendBoolean(boolean state)
  71.     {
  72.         this.body.writeByte(state == true ? WiredEncoding.POSITIVE : WiredEncoding.NEGATIVE);
  73.     }
  74.    
  75.     private void writeClosing()
  76.     {
  77.         this.body.writeByte(1);
  78.     }
  79.  
  80.     public ChannelBuffer get()
  81.     {
  82.         writeClosing();
  83.         return this.body;
  84.     }
  85. }
Add Comment
Please, Sign In to add comment