BaseSendablePacket.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. import java.util.logging.Logger;
  19. /**
  20. * This class ...
  21. *
  22. * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:11 $
  23. */
  24. public abstract class BaseSendablePacket
  25. {
  26. private static final Logger _log = Logger.getLogger(BaseSendablePacket.class.getName());
  27. private final ByteArrayOutputStream _bao;
  28. protected BaseSendablePacket()
  29. {
  30. _bao = new ByteArrayOutputStream();
  31. }
  32. protected void writeD(int value)
  33. {
  34. _bao.write(value &0xff);
  35. _bao.write(value >> 8 &0xff);
  36. _bao.write(value >> 16 &0xff);
  37. _bao.write(value >> 24 &0xff);
  38. }
  39. protected void writeH(int value)
  40. {
  41. _bao.write(value &0xff);
  42. _bao.write(value >> 8 &0xff);
  43. }
  44. protected void writeC(int value)
  45. {
  46. _bao.write(value &0xff);
  47. }
  48. protected void writeF(double org)
  49. {
  50. long value = Double.doubleToRawLongBits(org);
  51. _bao.write((int)(value &0xff));
  52. _bao.write((int)(value >> 8 &0xff));
  53. _bao.write((int)(value >> 16 &0xff));
  54. _bao.write((int)(value >> 24 &0xff));
  55. _bao.write((int)(value >> 32 &0xff));
  56. _bao.write((int)(value >> 40 &0xff));
  57. _bao.write((int)(value >> 48 &0xff));
  58. _bao.write((int)(value >> 56 &0xff));
  59. }
  60. protected void writeS(String text)
  61. {
  62. try
  63. {
  64. if (text != null)
  65. {
  66. _bao.write(text.getBytes("UTF-16LE"));
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  72. }
  73. _bao.write(0);
  74. _bao.write(0);
  75. }
  76. protected void writeB(byte[] array)
  77. {
  78. try
  79. {
  80. _bao.write(array);
  81. }
  82. catch (IOException e)
  83. {
  84. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  85. }
  86. }
  87. protected void writeQ(long value)
  88. {
  89. _bao.write((int) (value & 0xff));
  90. _bao.write((int) (value >> 8 & 0xff));
  91. _bao.write((int) (value >> 16 & 0xff));
  92. _bao.write((int) (value >> 24 & 0xff));
  93. _bao.write((int) (value >> 32 & 0xff));
  94. _bao.write((int) (value >> 40 & 0xff));
  95. _bao.write((int) (value >> 48 & 0xff));
  96. _bao.write((int) (value >> 56 & 0xff));
  97. }
  98. public int getLength()
  99. {
  100. return _bao.size()+2;
  101. }
  102. public byte[] getBytes()
  103. {
  104. //if (this instanceof Init)
  105. // writeD(0x00); //reserve for XOR initial key
  106. writeD(0x00); // reserve for checksum
  107. int padding = _bao.size() % 8;
  108. if (padding != 0)
  109. {
  110. for (int i = padding; i<8;i++)
  111. {
  112. writeC(0x00);
  113. }
  114. }
  115. return _bao.toByteArray();
  116. }
  117. public abstract byte[] getContent() throws IOException;
  118. }