You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ByteArrayBuilder.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Copyright (c) 2013 James Ahlborn
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.healthmarketscience.jackcess.impl;
  14. import java.nio.ByteBuffer;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * Utility class for constructing {@code byte[]s} where the final size of the
  19. * data is not known beforehand. The API is similar to {@code ByteBuffer} but
  20. * the data is not actually written to a {@code byte[]} until {@link
  21. * #toBuffer} or {@link #toArray} is called.
  22. *
  23. * @author James Ahlborn
  24. */
  25. public class ByteArrayBuilder
  26. {
  27. private int _pos;
  28. private final List<Data> _data = new ArrayList<Data>();
  29. public ByteArrayBuilder() {
  30. }
  31. public int position() {
  32. return _pos;
  33. }
  34. public ByteArrayBuilder reserveInt() {
  35. return reserve(4);
  36. }
  37. public ByteArrayBuilder reserveShort() {
  38. return reserve(2);
  39. }
  40. public ByteArrayBuilder reserve(int bytes) {
  41. _pos += bytes;
  42. return this;
  43. }
  44. public ByteArrayBuilder put(byte val) {
  45. return put(new ByteData(_pos, val));
  46. }
  47. public ByteArrayBuilder putInt(int val) {
  48. return putInt(_pos, val);
  49. }
  50. public ByteArrayBuilder putInt(int pos, int val) {
  51. return put(new IntData(pos, val));
  52. }
  53. public ByteArrayBuilder putShort(short val) {
  54. return putShort(_pos, val);
  55. }
  56. public ByteArrayBuilder putShort(int pos, short val) {
  57. return put(new ShortData(pos, val));
  58. }
  59. public ByteArrayBuilder put(byte[] val) {
  60. return put(new BytesData(_pos, val));
  61. }
  62. public ByteArrayBuilder put(ByteBuffer val) {
  63. return put(new BufData(_pos, val));
  64. }
  65. private ByteArrayBuilder put(Data data) {
  66. _data.add(data);
  67. int endPos = data.getEndPos();
  68. if(endPos > _pos) {
  69. _pos = endPos;
  70. }
  71. return this;
  72. }
  73. public ByteBuffer toBuffer() {
  74. return toBuffer(PageChannel.wrap(new byte[_pos]));
  75. }
  76. public ByteBuffer toBuffer(ByteBuffer buf) {
  77. for(Data data : _data) {
  78. data.write(buf);
  79. }
  80. buf.rewind();
  81. return buf;
  82. }
  83. public byte[] toArray() {
  84. return toBuffer().array();
  85. }
  86. private static abstract class Data
  87. {
  88. private int _pos;
  89. protected Data(int pos) {
  90. _pos = pos;
  91. }
  92. public int getPos() {
  93. return _pos;
  94. }
  95. public int getEndPos() {
  96. return getPos() + size();
  97. }
  98. public abstract int size();
  99. public abstract void write(ByteBuffer buf);
  100. }
  101. private static final class IntData extends Data
  102. {
  103. private final int _val;
  104. private IntData(int pos, int val) {
  105. super(pos);
  106. _val = val;
  107. }
  108. @Override
  109. public int size() {
  110. return 4;
  111. }
  112. @Override
  113. public void write(ByteBuffer buf) {
  114. buf.putInt(getPos(), _val);
  115. }
  116. }
  117. private static final class ShortData extends Data
  118. {
  119. private final short _val;
  120. private ShortData(int pos, short val) {
  121. super(pos);
  122. _val = val;
  123. }
  124. @Override
  125. public int size() {
  126. return 2;
  127. }
  128. @Override
  129. public void write(ByteBuffer buf) {
  130. buf.putShort(getPos(), _val);
  131. }
  132. }
  133. private static final class ByteData extends Data
  134. {
  135. private final byte _val;
  136. private ByteData(int pos, byte val) {
  137. super(pos);
  138. _val = val;
  139. }
  140. @Override
  141. public int size() {
  142. return 1;
  143. }
  144. @Override
  145. public void write(ByteBuffer buf) {
  146. buf.put(getPos(), _val);
  147. }
  148. }
  149. private static final class BytesData extends Data
  150. {
  151. private final byte[] _val;
  152. private BytesData(int pos, byte[] val) {
  153. super(pos);
  154. _val = val;
  155. }
  156. @Override
  157. public int size() {
  158. return _val.length;
  159. }
  160. @Override
  161. public void write(ByteBuffer buf) {
  162. buf.position(getPos());
  163. buf.put(_val);
  164. }
  165. }
  166. private static final class BufData extends Data
  167. {
  168. private final ByteBuffer _val;
  169. private BufData(int pos, ByteBuffer val) {
  170. super(pos);
  171. _val = val;
  172. }
  173. @Override
  174. public int size() {
  175. return _val.remaining();
  176. }
  177. @Override
  178. public void write(ByteBuffer buf) {
  179. buf.position(getPos());
  180. buf.put(_val);
  181. }
  182. }
  183. }