Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ByteArrayBuilder.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  14. USA
  15. */
  16. package com.healthmarketscience.jackcess.impl;
  17. import java.nio.ByteBuffer;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * Utility class for constructing {@code byte[]s} where the final size of the
  22. * data is not known beforehand. The API is similar to {@code ByteBuffer} but
  23. * the data is not actually written to a {@code byte[]} until {@link
  24. * #toBuffer} or {@link #toArray} is called.
  25. *
  26. * @author James Ahlborn
  27. */
  28. public class ByteArrayBuilder
  29. {
  30. private int _pos;
  31. private final List<Data> _data = new ArrayList<Data>();
  32. public ByteArrayBuilder() {
  33. }
  34. public int position() {
  35. return _pos;
  36. }
  37. public ByteArrayBuilder reserveInt() {
  38. return reserve(4);
  39. }
  40. public ByteArrayBuilder reserveShort() {
  41. return reserve(2);
  42. }
  43. public ByteArrayBuilder reserve(int bytes) {
  44. _pos += bytes;
  45. return this;
  46. }
  47. public ByteArrayBuilder put(byte val) {
  48. return put(new ByteData(_pos, val));
  49. }
  50. public ByteArrayBuilder putInt(int val) {
  51. return putInt(_pos, val);
  52. }
  53. public ByteArrayBuilder putInt(int pos, int val) {
  54. return put(new IntData(pos, val));
  55. }
  56. public ByteArrayBuilder putShort(short val) {
  57. return putShort(_pos, val);
  58. }
  59. public ByteArrayBuilder putShort(int pos, short val) {
  60. return put(new ShortData(pos, val));
  61. }
  62. public ByteArrayBuilder put(byte[] val) {
  63. return put(new BytesData(_pos, val));
  64. }
  65. public ByteArrayBuilder put(ByteBuffer val) {
  66. return put(new BufData(_pos, val));
  67. }
  68. private ByteArrayBuilder put(Data data) {
  69. _data.add(data);
  70. int endPos = data.getEndPos();
  71. if(endPos > _pos) {
  72. _pos = endPos;
  73. }
  74. return this;
  75. }
  76. public ByteBuffer toBuffer() {
  77. return toBuffer(PageChannel.wrap(new byte[_pos]));
  78. }
  79. public ByteBuffer toBuffer(ByteBuffer buf) {
  80. for(Data data : _data) {
  81. data.write(buf);
  82. }
  83. buf.rewind();
  84. return buf;
  85. }
  86. public byte[] toArray() {
  87. return toBuffer().array();
  88. }
  89. private static abstract class Data
  90. {
  91. private int _pos;
  92. protected Data(int pos) {
  93. _pos = pos;
  94. }
  95. public int getPos() {
  96. return _pos;
  97. }
  98. public int getEndPos() {
  99. return getPos() + size();
  100. }
  101. public abstract int size();
  102. public abstract void write(ByteBuffer buf);
  103. }
  104. private static final class IntData extends Data
  105. {
  106. private final int _val;
  107. private IntData(int pos, int val) {
  108. super(pos);
  109. _val = val;
  110. }
  111. @Override
  112. public int size() {
  113. return 4;
  114. }
  115. @Override
  116. public void write(ByteBuffer buf) {
  117. buf.putInt(getPos(), _val);
  118. }
  119. }
  120. private static final class ShortData extends Data
  121. {
  122. private final short _val;
  123. private ShortData(int pos, short val) {
  124. super(pos);
  125. _val = val;
  126. }
  127. @Override
  128. public int size() {
  129. return 2;
  130. }
  131. @Override
  132. public void write(ByteBuffer buf) {
  133. buf.putShort(getPos(), _val);
  134. }
  135. }
  136. private static final class ByteData extends Data
  137. {
  138. private final byte _val;
  139. private ByteData(int pos, byte val) {
  140. super(pos);
  141. _val = val;
  142. }
  143. @Override
  144. public int size() {
  145. return 1;
  146. }
  147. @Override
  148. public void write(ByteBuffer buf) {
  149. buf.put(getPos(), _val);
  150. }
  151. }
  152. private static final class BytesData extends Data
  153. {
  154. private final byte[] _val;
  155. private BytesData(int pos, byte[] val) {
  156. super(pos);
  157. _val = val;
  158. }
  159. @Override
  160. public int size() {
  161. return _val.length;
  162. }
  163. @Override
  164. public void write(ByteBuffer buf) {
  165. buf.position(getPos());
  166. buf.put(_val);
  167. }
  168. }
  169. private static final class BufData extends Data
  170. {
  171. private final ByteBuffer _val;
  172. private BufData(int pos, ByteBuffer val) {
  173. super(pos);
  174. _val = val;
  175. }
  176. @Override
  177. public int size() {
  178. return _val.remaining();
  179. }
  180. @Override
  181. public void write(ByteBuffer buf) {
  182. buf.position(getPos());
  183. buf.put(_val);
  184. }
  185. }
  186. }