SynchronizedBufferedByteReader.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 SynchronizedBufferedByteReader extends AbstractBufferedByteReader
  20. {
  21. public SynchronizedBufferedByteReader(final byte[] data)
  22. {
  23. super(data);
  24. }
  25. @Override
  26. public final int readC()
  27. {
  28. int result;
  29. synchronized(buf)
  30. {
  31. result = buf[rIndex++] & 0x000000FF;
  32. }
  33. return result;
  34. }
  35. @Override
  36. public final int readH()
  37. {
  38. int result;
  39. synchronized(buf)
  40. {
  41. result = buf[rIndex++] & 0x000000FF;
  42. result |= buf[rIndex++] << 8 & 0x0000FF00;
  43. }
  44. return result;
  45. }
  46. @Override
  47. public final int readD()
  48. {
  49. int result;
  50. synchronized(buf)
  51. {
  52. result = buf[rIndex++] & 0x000000FF;
  53. result |= buf[rIndex++] << 8 & 0x0000FF00;
  54. result |= buf[rIndex++] << 16 & 0x00FF0000;
  55. result |= buf[rIndex++] << 24 & 0xFF000000;
  56. }
  57. return result;
  58. }
  59. @Override
  60. public final double readF()
  61. {
  62. long result;
  63. synchronized(buf)
  64. {
  65. result = buf[rIndex++] & 0x00000000000000FF;
  66. result |= buf[rIndex++] << 8 & 0x000000000000FF00;
  67. result |= buf[rIndex++] << 16 & 0x0000000000FF0000;
  68. result |= buf[rIndex++] << 24 & 0x00000000FF000000;
  69. result |= buf[rIndex++] << 32 & 0x000000FF00000000L;
  70. result |= buf[rIndex++] << 40 & 0x0000FF0000000000L;
  71. result |= buf[rIndex++] << 48 & 0x00FF000000000000L;
  72. result |= buf[rIndex++] << 56 & 0xFF00000000000000L;
  73. }
  74. return Double.longBitsToDouble(result);
  75. }
  76. @Override
  77. public final long readQ()
  78. {
  79. int value1, value2;
  80. synchronized(buf)
  81. {
  82. value1 = (buf[rIndex++] & 0x000000FF) | (buf[rIndex++] << 8 & 0x0000FF00) | (buf[rIndex++] << 16 & 0x00FF0000) | (buf[rIndex++] << 24 & 0xFF000000);
  83. value2 = (buf[rIndex++] & 0x000000FF) | (buf[rIndex++] << 8 & 0x0000FF00) | (buf[rIndex++] << 16 & 0x00FF0000) | (buf[rIndex++] << 24 & 0xFF000000);
  84. }
  85. return (value1 & 0xFFFFFFFFL) | (value2 & 0xFFFFFFFFL) << 32;
  86. }
  87. @Override
  88. public final byte[] readB(final int length)
  89. {
  90. final byte[] result = new byte[length];
  91. synchronized(buf)
  92. {
  93. for (int i = 0; i < length; i++)
  94. {
  95. result[i] = buf[rIndex++];
  96. }
  97. }
  98. return result;
  99. }
  100. @Override
  101. public final String readS()
  102. {
  103. String result = null;
  104. try
  105. {
  106. synchronized(buf)
  107. {
  108. result = new String(buf, rIndex, buf.length - rIndex, "UTF-16LE");
  109. result = result.substring(0, result.indexOf(0x00));
  110. rIndex += result.length() * 2 + 2;
  111. }
  112. }
  113. catch (Exception e)
  114. {
  115. e.printStackTrace();
  116. }
  117. return result;
  118. }
  119. }