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