SynchronizedBufferedByteWriter.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. import java.util.Arrays;
  17. /**
  18. * @author Forsaiken
  19. */
  20. public final class SynchronizedBufferedByteWriter extends AbstractBufferedByteWriter
  21. {
  22. public SynchronizedBufferedByteWriter()
  23. {
  24. super(32);
  25. }
  26. public SynchronizedBufferedByteWriter(final int size)
  27. {
  28. super(size);
  29. }
  30. @Override
  31. public final void writeC(final int value)
  32. {
  33. synchronized(buf)
  34. {
  35. expand(1);
  36. buf[wIndex++] = (byte)(value & 0x000000FF);
  37. }
  38. }
  39. @Override
  40. public final void writeH(final int value)
  41. {
  42. synchronized(buf)
  43. {
  44. expand(2);
  45. buf[wIndex++] = (byte)(value & 0x000000FF);
  46. buf[wIndex++] = (byte)(value >> 8 & 0x000000FF);
  47. }
  48. }
  49. @Override
  50. public final void writeD(final int value)
  51. {
  52. synchronized(buf)
  53. {
  54. expand(4);
  55. buf[wIndex++] = (byte)(value & 0x000000FF);
  56. buf[wIndex++] = (byte)(value >> 8 & 0x000000FF);
  57. buf[wIndex++] = (byte)(value >> 16 & 0x000000FF);
  58. buf[wIndex++] = (byte)(value >> 24 & 0x000000FF);
  59. }
  60. }
  61. @Override
  62. public final void writeF(final double value)
  63. {
  64. writeQ(Double.doubleToRawLongBits(value));
  65. }
  66. @Override
  67. public final void writeQ(final long value)
  68. {
  69. synchronized(buf)
  70. {
  71. expand(8);
  72. buf[wIndex++] = (byte)(value & 0x000000FF);
  73. buf[wIndex++] = (byte)(value >> 8 & 0x000000FF);
  74. buf[wIndex++] = (byte)(value >> 16 & 0x000000FF);
  75. buf[wIndex++] = (byte)(value >> 24 & 0x000000FF);
  76. buf[wIndex++] = (byte)(value >> 32 & 0x000000FF);
  77. buf[wIndex++] = (byte)(value >> 40 & 0x000000FF);
  78. buf[wIndex++] = (byte)(value >> 48 & 0x000000FF);
  79. buf[wIndex++] = (byte)(value >> 56 & 0x000000FF);
  80. }
  81. }
  82. @Override
  83. public final void writeS(final String text)
  84. {
  85. if (text != null)
  86. {
  87. synchronized(buf)
  88. {
  89. expand(text.length() + 2);
  90. for (final char c : text.toCharArray())
  91. {
  92. buf[wIndex] = (byte)c;
  93. wIndex += 2;
  94. }
  95. wIndex += 2;
  96. }
  97. }
  98. else
  99. {
  100. synchronized(buf)
  101. {
  102. expand(2);
  103. wIndex += 2;
  104. }
  105. }
  106. }
  107. @Override
  108. public final void writeB(final byte[] data)
  109. {
  110. try
  111. {
  112. synchronized(buf)
  113. {
  114. expand(data.length);
  115. System.arraycopy(data, 0, buf, wIndex, data.length);
  116. wIndex += data.length;
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. e.printStackTrace();
  122. }
  123. }
  124. private synchronized void expand(final int expand)
  125. {
  126. final int newSize = wIndex + expand;
  127. if (newSize > buf.length)
  128. buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newSize));
  129. }
  130. }