BaseRecievePacket.java 3.1 KB

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