Advertisement
Guest User

Nexus ServerMessage based in Legion ServerMessage

a guest
Jul 24th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. private Logger log = LoggerFactory.getLogger(ServerMessage.class);
  2.     private int Header;
  3.     private ByteArrayOutputStream byteArray;
  4.    
  5.     public ServerMessage() {
  6.         this.byteArray = new ByteArrayOutputStream();
  7.     }
  8.        
  9.     public ServerMessage(int header) {        
  10.         // TODO Auto-generated constructor stub
  11.         this.byteArray = new ByteArrayOutputStream();
  12.         this.Header = header;        
  13.        
  14.     }
  15.    
  16.     public void writeByte(int Int) {
  17.         try {
  18.             this.byteArray.write(Int);
  19.         } catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.    
  24.     public void writeUtf(String UTF)
  25. /*    */   {
  26. /*    */     try {
  27. /* 19 */       this.byteArray.write(HabboEncoding.WriteShort(UTF.length()));
  28. /* 20 */       this.byteArray.write(UTF.getBytes());
  29. /*    */     }
  30. /*    */     catch (IOException e) {
  31. /* 23 */       e.printStackTrace();
  32. /*    */     }
  33. /*    */   }
  34. /*    */
  35. /*    */   public void writeInt(int Int)
  36. /*    */   {
  37. /*    */     try {
  38. /* 30 */       this.byteArray.write(HabboEncoding.WriteInt(Int));
  39. /*    */     }
  40. /*    */     catch (Exception e) {
  41. /* 33 */       e.printStackTrace();
  42. /*    */     }
  43. /*    */   }
  44. /*    */
  45. /*    */   public void writeShort(int Short)
  46. /*    */   {
  47. /*    */     try {
  48. /* 40 */       this.byteArray.write(HabboEncoding.WriteShort(Short));
  49. /*    */     }
  50. /*    */     catch (Exception e) {
  51. /* 43 */       e.printStackTrace();
  52. /*    */     }
  53. /*    */   }
  54. /*    */
  55. /*    */   public void writeLength()
  56. /*    */   {
  57. /* 49 */     int Length = this.byteArray.size();
  58. /*    */
  59. /* 51 */     ServerMessage Array = new ServerMessage();
  60. /*    */
  61. /* 53 */     Array.writeInt(Length);
  62. /* 54 */     Array.writeByteArray(this);
  63. /*    */
  64. /* 56 */     this.byteArray = Array.getByteArray();
  65. /*    */   }
  66. /*    */
  67. /*    */   public void writeByteArray(ServerMessage ByteArray)
  68. /*    */   {
  69. /*    */     try {
  70. /* 62 */       this.byteArray.write(ByteArray.getBytes());
  71. /*    */     }
  72. /*    */     catch (Exception e) {
  73. /* 65 */      
  74. /*    */     }
  75. /*    */   }
  76. /*    */
  77. /*    */   public void writeBoolean(Boolean Boolean) {
  78. /* 70 */     this.byteArray.write(Boolean.booleanValue() ? 1 : 0);
  79. /*    */   }
  80. /*    */
  81. /*    */   public byte[] getBytes()
  82. /*    */   {
  83. /* 75 */     return this.byteArray.toByteArray();
  84. /*    */   }
  85. /*    */
  86. /*    */   public ByteArrayOutputStream getByteArray()
  87. /*    */   {
  88. /* 80 */     return this.byteArray;
  89. /*    */   }
  90. /*    */
  91. /*    */   public void finalize() throws Throwable
  92. /*    */   {
  93. /* 85 */     Clear();
  94. /* 86 */     super.finalize();
  95. /*    */   }
  96. /*    */
  97. /*    */   private void Clear() {
  98. /* 90 */     this.byteArray.reset();
  99. /*    */   }
  100.     public void Send(Channel Session) {
  101.             ServerMessage ToSend = new ServerMessage();
  102.         ToSend.writeShort(Header);
  103.         ToSend.writeByteArray(this);
  104.         ToSend.writeLength();
  105.             Session.write(ChannelBuffers.wrappedBuffer(ToSend.getBytes()));    
  106.             if(nexus.environment.NexusEnv.active_packetlog)
  107.             log.info("[OUTGOING PACKET] [" + Header + "]" + ConnectionHandler.getCharFilter(ChannelBuffers.wrappedBuffer(ToSend.getBytes())));
  108.     }
  109.  
  110.     public void writeByte(byte[] bytes) {
  111.         // TODO Auto-generated method stub
  112.         try {
  113.             this.byteArray.write(bytes);
  114.         } catch (Exception e) {
  115.             e.printStackTrace();
  116.         }
  117.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement