BaseSendablePacket.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:11 $
  22. */
  23. public abstract class BaseSendablePacket
  24. {
  25. private static final Logger _log = Logger.getLogger(BaseSendablePacket.class.getName());
  26. private final ByteArrayOutputStream _bao;
  27. protected BaseSendablePacket()
  28. {
  29. _bao = new ByteArrayOutputStream();
  30. }
  31. protected void writeD(int value)
  32. {
  33. _bao.write(value & 0xff);
  34. _bao.write((value >> 8) & 0xff);
  35. _bao.write((value >> 16) & 0xff);
  36. _bao.write((value >> 24) & 0xff);
  37. }
  38. protected void writeH(int value)
  39. {
  40. _bao.write(value & 0xff);
  41. _bao.write((value >> 8) & 0xff);
  42. }
  43. protected void writeC(int value)
  44. {
  45. _bao.write(value & 0xff);
  46. }
  47. protected void writeF(double org)
  48. {
  49. long value = Double.doubleToRawLongBits(org);
  50. _bao.write((int) (value & 0xff));
  51. _bao.write((int) ((value >> 8) & 0xff));
  52. _bao.write((int) ((value >> 16) & 0xff));
  53. _bao.write((int) ((value >> 24) & 0xff));
  54. _bao.write((int) ((value >> 32) & 0xff));
  55. _bao.write((int) ((value >> 40) & 0xff));
  56. _bao.write((int) ((value >> 48) & 0xff));
  57. _bao.write((int) ((value >> 56) & 0xff));
  58. }
  59. protected void writeS(String text)
  60. {
  61. try
  62. {
  63. if (text != null)
  64. {
  65. _bao.write(text.getBytes("UTF-16LE"));
  66. }
  67. }
  68. catch (Exception e)
  69. {
  70. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  71. }
  72. _bao.write(0);
  73. _bao.write(0);
  74. }
  75. protected void writeB(byte[] array)
  76. {
  77. try
  78. {
  79. _bao.write(array);
  80. }
  81. catch (IOException e)
  82. {
  83. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  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. }