Guest User

Untitled

a guest
Jan 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 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 ClientMessage
  12. {
  13.     private Integer id;
  14.     private ChannelBuffer body;
  15.    
  16.     public ClientMessage(Integer id, ChannelBuffer body)
  17.     {
  18.         this.id = id;
  19.        
  20.         if (body == null)
  21.         {
  22.             this.body = ChannelBuffers.EMPTY_BUFFER;
  23.         }
  24.         else
  25.         {
  26.             this.body = body;
  27.         }
  28.        
  29.         this.body.markReaderIndex();
  30.     }
  31.    
  32.     public ChannelBuffer readBytes(int len)
  33.     {
  34.         if (len > this.body.readableBytes())
  35.         {
  36.             len = this.body.readableBytes();
  37.         }
  38.  
  39.         ChannelBuffer buf = this.body.readBytes(len);
  40.         this.body.discardReadBytes();
  41.  
  42.         return buf;
  43.     }
  44.  
  45.     public ChannelBuffer readRawBytes(int len)
  46.     {
  47.         if (len > this.body.readableBytes())
  48.         {
  49.             len = this.body.readableBytes();
  50.         }
  51.  
  52.         ChannelBuffer res = this.body.readBytes(len);
  53.         this.body.resetReaderIndex();
  54.  
  55.         return res;
  56.     }
  57.  
  58.     public ChannelBuffer readFixedValue()
  59.     {
  60.         int b64 = B64Encoding.DecodeInt32(this.readBytes(2).array());
  61.         return this.readBytes(b64);
  62.     }
  63.  
  64.     public int popInt()
  65.     {
  66.         return B64Encoding.DecodeInt32(this.readBytes(2).array());
  67.     }
  68.  
  69.     public int popFixedInt()
  70.     {
  71.         int i = 0;
  72.         String s = this.popFixedString();
  73.  
  74.         i = Integer.parseInt(s);
  75.  
  76.         return i;
  77.     }
  78.  
  79.     public int popWiredInt()
  80.     {
  81.         if (this.body.readableBytes() < 1)
  82.         {
  83.             return 0;
  84.         }
  85.  
  86.         byte[] vl64 = this.readRawBytes(WiredEncoding.MAX_INTEGER_BYTE_AMOUNT).array();
  87.  
  88.         int i = WiredEncoding.DecodeInt32(vl64, 0);
  89.         byte[] totalBytes = WiredEncoding.EncodeInt32(i);
  90.  
  91.         this.body.readBytes(totalBytes.length);
  92.         this.body.discardReadBytes();
  93.  
  94.         return i;
  95.     }
  96.  
  97.     public String popFixedString()
  98.     {
  99.         return new String(this.readFixedValue().toString(Charset.defaultCharset()));
  100.     }
  101.  
  102.     public boolean popBase64Boolean()
  103.     {
  104.         if (this.body.readableBytes() > 0 && this.body.readByte() == B64Encoding.POSITIVE)
  105.         {
  106.  
  107.             return true;
  108.         }
  109.  
  110.         return false;
  111.     }
  112.  
  113.     public boolean popWiredBoolean()
  114.     {
  115.         if (this.body.readableBytes() > 0 && this.body.readByte() == WiredEncoding.POSITIVE)
  116.         {
  117.  
  118.             return true;
  119.         }
  120.  
  121.         return false;
  122.     }
  123.  
  124.     public int getType()
  125.     {
  126.         return this.id;
  127.     }
  128.  
  129.     public String getHeader()
  130.     {
  131.         return new String(B64Encoding.EncodeInt32(this.getType(), 2));
  132.     }
  133.  
  134.     public String getBodyString()
  135.     {
  136.         return new String(this.body.toString(Charset.defaultCharset()));
  137.     }
  138.  
  139.     public int getCurrentLength()
  140.     {
  141.         return this.body.readableBytes();
  142.     }
  143. }
Add Comment
Please, Sign In to add comment