BufferedByteReader.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.communityserver.util.buffer;
  16. /**
  17. * @author Forsaiken
  18. */
  19. public final class BufferedByteReader extends AbstractBufferedByteReader
  20. {
  21. public BufferedByteReader(final byte[] data)
  22. {
  23. super(data);
  24. }
  25. @Override
  26. public final int readC()
  27. {
  28. return buf[rIndex++] & 0x000000FF;
  29. }
  30. @Override
  31. public final int readH()
  32. {
  33. int result = buf[rIndex++] & 0x000000FF;
  34. result |= buf[rIndex++] << 8 & 0x0000FF00;
  35. return result;
  36. }
  37. @Override
  38. public final int readD()
  39. {
  40. int result = buf[rIndex++] & 0x000000FF;
  41. result |= buf[rIndex++] << 8 & 0x0000FF00;
  42. result |= buf[rIndex++] << 16 & 0x00FF0000;
  43. result |= buf[rIndex++] << 24 & 0xFF000000;
  44. return result;
  45. }
  46. @Override
  47. public final double readF()
  48. {
  49. long result = buf[rIndex++] & 0x00000000000000FF;
  50. result |= buf[rIndex++] << 8 & 0x000000000000FF00;
  51. result |= buf[rIndex++] << 16 & 0x0000000000FF0000;
  52. result |= buf[rIndex++] << 24 & 0x00000000FF000000;
  53. result |= buf[rIndex++] << 32 & 0x000000FF00000000L;
  54. result |= buf[rIndex++] << 40 & 0x0000FF0000000000L;
  55. result |= buf[rIndex++] << 48 & 0x00FF000000000000L;
  56. result |= buf[rIndex++] << 56 & 0xFF00000000000000L;
  57. return Double.longBitsToDouble(result);
  58. }
  59. @Override
  60. public final long readQ()
  61. {
  62. final int value1 = (buf[rIndex++] & 0x000000FF) | (buf[rIndex++] << 8 & 0x0000FF00) | (buf[rIndex++] << 16 & 0x00FF0000) | (buf[rIndex++] << 24 & 0xFF000000);
  63. final int value2 = (buf[rIndex++] & 0x000000FF) | (buf[rIndex++] << 8 & 0x0000FF00) | (buf[rIndex++] << 16 & 0x00FF0000) | (buf[rIndex++] << 24 & 0xFF000000);
  64. return (value1 & 0xFFFFFFFFL) | (value2 & 0xFFFFFFFFL) << 32;
  65. }
  66. @Override
  67. public final byte[] readB(final int length)
  68. {
  69. final byte[] result = new byte[length];
  70. System.arraycopy(buf, rIndex, result, 0, length);
  71. rIndex += length;
  72. return result;
  73. }
  74. @Override
  75. public final String readS()
  76. {
  77. String result = null;
  78. try
  79. {
  80. result = new String(buf, rIndex, buf.length - rIndex, "UTF-16LE");
  81. result = result.substring(0, result.indexOf(0x00));
  82. rIndex += result.length() * 2 + 2;
  83. }
  84. catch (Exception e)
  85. {
  86. e.printStackTrace();
  87. }
  88. return result;
  89. }
  90. }