BaseSendablePacket.java 3.3 KB

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