BaseSendablePacket.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util.network;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. /**
  19. * This class ...
  20. *
  21. * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:11 $
  22. */
  23. public abstract class BaseSendablePacket
  24. {
  25. ByteArrayOutputStream _bao;
  26. protected BaseSendablePacket()
  27. {
  28. _bao = new ByteArrayOutputStream();
  29. }
  30. protected void writeD(int value)
  31. {
  32. _bao.write(value &0xff);
  33. _bao.write(value >> 8 &0xff);
  34. _bao.write(value >> 16 &0xff);
  35. _bao.write(value >> 24 &0xff);
  36. }
  37. protected void writeH(int value)
  38. {
  39. _bao.write(value &0xff);
  40. _bao.write(value >> 8 &0xff);
  41. }
  42. protected void writeC(int value)
  43. {
  44. _bao.write(value &0xff);
  45. }
  46. protected void writeF(double org)
  47. {
  48. long value = Double.doubleToRawLongBits(org);
  49. _bao.write((int)(value &0xff));
  50. _bao.write((int)(value >> 8 &0xff));
  51. _bao.write((int)(value >> 16 &0xff));
  52. _bao.write((int)(value >> 24 &0xff));
  53. _bao.write((int)(value >> 32 &0xff));
  54. _bao.write((int)(value >> 40 &0xff));
  55. _bao.write((int)(value >> 48 &0xff));
  56. _bao.write((int)(value >> 56 &0xff));
  57. }
  58. protected void writeS(String text)
  59. {
  60. try
  61. {
  62. if (text != null)
  63. {
  64. _bao.write(text.getBytes("UTF-16LE"));
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. e.printStackTrace();
  70. }
  71. _bao.write(0);
  72. _bao.write(0);
  73. }
  74. protected void writeB(byte[] array)
  75. {
  76. try
  77. {
  78. _bao.write(array);
  79. }
  80. catch (IOException e)
  81. {
  82. // TODO Auto-generated catch block
  83. e.printStackTrace();
  84. }
  85. }
  86. protected void writeQ(long value)
  87. {
  88. _bao.write((int) (value & 0xff));
  89. _bao.write((int) (value >> 8 & 0xff));
  90. _bao.write((int) (value >> 16 & 0xff));
  91. _bao.write((int) (value >> 24 & 0xff));
  92. _bao.write((int) (value >> 32 & 0xff));
  93. _bao.write((int) (value >> 40 & 0xff));
  94. _bao.write((int) (value >> 48 & 0xff));
  95. _bao.write((int) (value >> 56 & 0xff));
  96. }
  97. public int getLength()
  98. {
  99. return _bao.size()+2;
  100. }
  101. public byte[] getBytes()
  102. {
  103. //if (this instanceof Init)
  104. // writeD(0x00); //reserve for XOR initial key
  105. writeD(0x00); // reserve for checksum
  106. int padding = _bao.size() % 8;
  107. if (padding != 0)
  108. {
  109. for (int i = padding; i<8;i++)
  110. {
  111. writeC(0x00);
  112. }
  113. }
  114. return _bao.toByteArray();
  115. }
  116. public abstract byte[] getContent() throws IOException;
  117. }