BaseWritePacket.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.network.netcon;
  16. import java.io.IOException;
  17. import com.l2jserver.communityserver.util.buffer.AbstractBufferedByteWriter;
  18. import com.l2jserver.communityserver.util.buffer.BufferedByteWriter;
  19. public abstract class BaseWritePacket
  20. {
  21. private final AbstractBufferedByteWriter _buf;
  22. protected BaseWritePacket()
  23. {
  24. _buf = new BufferedByteWriter();
  25. }
  26. protected final void writeC(final int value)
  27. {
  28. _buf.writeC(value);
  29. }
  30. protected final void writeH(final int value)
  31. {
  32. _buf.writeH(value);
  33. }
  34. protected final void writeD(final int value)
  35. {
  36. _buf.writeD(value);
  37. }
  38. protected final void writeF(final double value)
  39. {
  40. _buf.writeF(value);
  41. }
  42. protected final void writeQ(final long value)
  43. {
  44. _buf.writeQ(value);
  45. }
  46. protected final void writeS(final String text)
  47. {
  48. _buf.writeS(text);
  49. }
  50. protected final void writeB(final byte[] array)
  51. {
  52. _buf.writeB(array);
  53. }
  54. public final byte[] getContent() throws IOException
  55. {
  56. writeD(0x00);
  57. int padding = _buf.length() % 8;
  58. if (padding != 0)
  59. {
  60. for (int i = padding; i < 8; i++)
  61. {
  62. writeC(0x00);
  63. }
  64. }
  65. return _buf.toByteArray();
  66. }
  67. }