aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/healthmarketscience/jackcess/impl/ByteArrayBuilder.java
blob: aee6f0b67e9386524867473c3c68d08513d88a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Copyright (c) 2013 James Ahlborn

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.healthmarketscience.jackcess.impl;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;


/**
 * Utility class for constructing {@code byte[]s} where the final size of the
 * data is not known beforehand.  The API is similar to {@code ByteBuffer} but
 * the data is not actually written to a {@code byte[]} until {@link
 * #toBuffer} or {@link #toArray} is called.
 *
 * @author James Ahlborn
 */
public class ByteArrayBuilder 
{
  private int _pos;
  private final List<Data> _data = new ArrayList<Data>();

  public ByteArrayBuilder() {
  }

  public int position() {
    return _pos;
  }

  public ByteArrayBuilder reserveInt() {
    return reserve(4);
  }

  public ByteArrayBuilder reserveShort() {
    return reserve(2);
  }

  public ByteArrayBuilder reserve(int bytes) {
    _pos += bytes;
    return this;
  }

  public ByteArrayBuilder put(byte val) {
    return put(new ByteData(_pos, val));
  }

  public ByteArrayBuilder putInt(int val) {
    return putInt(_pos, val);
  }

  public ByteArrayBuilder putInt(int pos, int val) {
    return put(new IntData(pos, val));
  }

  public ByteArrayBuilder putShort(short val) {
    return putShort(_pos, val);
  }

  public ByteArrayBuilder putShort(int pos, short val) {
    return put(new ShortData(pos, val));
  }

  public ByteArrayBuilder put(byte[] val) {
    return put(new BytesData(_pos, val));
  }

  public ByteArrayBuilder put(ByteBuffer val) {
    return put(new BufData(_pos, val));
  }

  private ByteArrayBuilder put(Data data) {
    _data.add(data);
    int endPos = data.getEndPos();
    if(endPos > _pos) {
      _pos = endPos;
    }
    return this;
  }

  public ByteBuffer toBuffer() {
    return toBuffer(PageChannel.wrap(new byte[_pos]));
  }

  public ByteBuffer toBuffer(ByteBuffer buf) {
    for(Data data : _data) {
      data.write(buf);
    }
    buf.rewind();
    return buf;
  }

  public byte[] toArray() {
    return toBuffer().array();
  }

  private static abstract class Data
  {
    private int _pos;
    
    protected Data(int pos) {
      _pos = pos;
    }

    public int getPos() {
      return _pos;
    }

    public int getEndPos() {
      return getPos() + size();
    }

    public abstract int size();

    public abstract void write(ByteBuffer buf);
  }

  private static final class IntData extends Data
  {
    private final int _val;

    private IntData(int pos, int val) {
      super(pos);
      _val = val;
    }

    @Override
    public int size() {
      return 4;
    }

    @Override
    public void write(ByteBuffer buf) {
      buf.putInt(getPos(), _val);
    }
  }

  private static final class ShortData extends Data
  {
    private final short _val;

    private ShortData(int pos, short val) {
      super(pos);
      _val = val;
    }

    @Override
    public int size() {
      return 2;
    }

    @Override
    public void write(ByteBuffer buf) {
      buf.putShort(getPos(), _val);
    }
  }

  private static final class ByteData extends Data
  {
    private final byte _val;

    private ByteData(int pos, byte val) {
      super(pos);
      _val = val;
    }

    @Override
    public int size() {
      return 1;
    }

    @Override
    public void write(ByteBuffer buf) {
      buf.put(getPos(), _val);
    }
  }

  private static final class BytesData extends Data
  {
    private final byte[] _val;

    private BytesData(int pos, byte[] val) {
      super(pos);
      _val = val;
    }

    @Override
    public int size() {
      return _val.length;
    }

    @Override
    public void write(ByteBuffer buf) {
      buf.position(getPos());
      buf.put(_val);
    }
  }

  private static final class BufData extends Data
  {
    private final ByteBuffer _val;

    private BufData(int pos, ByteBuffer val) {
      super(pos);
      _val = val;
    }

    @Override
    public int size() {
      return _val.remaining();
    }

    @Override
    public void write(ByteBuffer buf) {
      buf.position(getPos());
      buf.put(_val);
    }
  }
}