BufferedByteWriter.java 3.1 KB

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