BaseRecievePacket.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.util.logging.Logger;
  17. /**
  18. * This class ...
  19. * @version $Revision: 1.2.4.1 $ $Date: 2005/03/27 15:30:12 $
  20. */
  21. public abstract class BaseRecievePacket
  22. {
  23. private static final Logger _log = Logger.getLogger(BaseRecievePacket.class.getName());
  24. private final byte[] _decrypt;
  25. private int _off;
  26. public BaseRecievePacket(byte[] decrypt)
  27. {
  28. _decrypt = decrypt;
  29. _off = 1; // skip packet type id
  30. }
  31. public int readD()
  32. {
  33. int result = _decrypt[_off++] & 0xff;
  34. result |= (_decrypt[_off++] << 8) & 0xff00;
  35. result |= (_decrypt[_off++] << 0x10) & 0xff0000;
  36. result |= (_decrypt[_off++] << 0x18) & 0xff000000;
  37. return result;
  38. }
  39. public int readC()
  40. {
  41. int result = _decrypt[_off++] & 0xff;
  42. return result;
  43. }
  44. public int readH()
  45. {
  46. int result = _decrypt[_off++] & 0xff;
  47. result |= (_decrypt[_off++] << 8) & 0xff00;
  48. return result;
  49. }
  50. public double readF()
  51. {
  52. long result = _decrypt[_off++] & 0xff;
  53. result |= (_decrypt[_off++] & 0xffL) << 8L;
  54. result |= (_decrypt[_off++] & 0xffL) << 16L;
  55. result |= (_decrypt[_off++] & 0xffL) << 24L;
  56. result |= (_decrypt[_off++] & 0xffL) << 32L;
  57. result |= (_decrypt[_off++] & 0xffL) << 40L;
  58. result |= (_decrypt[_off++] & 0xffL) << 48L;
  59. result |= (_decrypt[_off++] & 0xffL) << 56L;
  60. return Double.longBitsToDouble(result);
  61. }
  62. public String readS()
  63. {
  64. String result = null;
  65. try
  66. {
  67. result = new String(_decrypt, _off, _decrypt.length - _off, "UTF-16LE");
  68. result = result.substring(0, result.indexOf(0x00));
  69. _off += (result.length() * 2) + 2;
  70. }
  71. catch (Exception e)
  72. {
  73. _log.warning(getClass().getSimpleName() + ": " + e.getMessage());
  74. }
  75. return result;
  76. }
  77. public final byte[] readB(int length)
  78. {
  79. byte[] result = new byte[length];
  80. for (int i = 0; i < length; i++)
  81. {
  82. result[i] = _decrypt[_off + i];
  83. }
  84. _off += length;
  85. return result;
  86. }
  87. public long readQ()
  88. {
  89. long result = _decrypt[_off++] & 0xff;
  90. result |= (_decrypt[_off++] & 0xffL) << 8L;
  91. result |= (_decrypt[_off++] & 0xffL) << 16L;
  92. result |= (_decrypt[_off++] & 0xffL) << 24L;
  93. result |= (_decrypt[_off++] & 0xffL) << 32L;
  94. result |= (_decrypt[_off++] & 0xffL) << 40L;
  95. result |= (_decrypt[_off++] & 0xffL) << 48L;
  96. result |= (_decrypt[_off++] & 0xffL) << 56L;
  97. return result;
  98. }
  99. }