BaseRecievePacket.java 2.8 KB

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