2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 package org
.strongswan
.android
.utils
;
18 import java
.nio
.ByteBuffer
;
21 * Very similar to ByteBuffer (although with a stripped interface) but it
22 * automatically resizes the underlying buffer.
24 public class BufferedByteWriter
27 * The underlying byte buffer
29 private byte[] mBuffer
;
32 * ByteBuffer used as wrapper around the buffer to easily convert values
34 private ByteBuffer mWriter
;
37 * Create a writer with a default initial capacity
39 public BufferedByteWriter()
45 * Create a writer with the given initial capacity (helps avoid expensive
47 * @param capacity initial capacity
49 public BufferedByteWriter(int capacity
)
51 capacity
= capacity
> 4 ? capacity
: 32;
52 mBuffer
= new byte[capacity
];
53 mWriter
= ByteBuffer
.wrap(mBuffer
);
57 * Ensure that there is enough space available to write the requested
58 * number of bytes. If necessary the internal buffer is resized.
59 * @param required required number of bytes
61 private void ensureCapacity(int required
)
63 if (mWriter
.remaining() >= required
)
67 byte[] buffer
= new byte[(mBuffer
.length
+ required
) * 2];
68 System
.arraycopy(mBuffer
, 0, buffer
, 0, mWriter
.position());
70 ByteBuffer writer
= ByteBuffer
.wrap(buffer
);
71 writer
.position(mWriter
.position());
76 * Write the given byte array to the buffer
80 public BufferedByteWriter
put(byte[] value
)
82 ensureCapacity(value
.length
);
88 * Write the given byte to the buffer
92 public BufferedByteWriter
put(byte value
)
100 * Write the 8-bit length of the given data followed by the data itself
104 public BufferedByteWriter
putLen8(byte[] value
)
106 ensureCapacity(1 + value
.length
);
107 mWriter
.put((byte)value
.length
);
113 * Write the 16-bit length of the given data followed by the data itself
117 public BufferedByteWriter
putLen16(byte[] value
)
119 ensureCapacity(2 + value
.length
);
120 mWriter
.putShort((short)value
.length
);
126 * Write the given short value (16-bit) in big-endian order to the buffer
130 public BufferedByteWriter
put16(short value
)
133 mWriter
.putShort(value
);
138 * Write 24-bit of the given value in big-endian order to the buffer
142 public BufferedByteWriter
put24(int value
)
145 mWriter
.put((byte)(value
>> 16));
146 mWriter
.putShort((short)value
);
151 * Write the given int value (32-bit) in big-endian order to the buffer
155 public BufferedByteWriter
put32(int value
)
158 mWriter
.putInt(value
);
163 * Write the given long value (64-bit) in big-endian order to the buffer
167 public BufferedByteWriter
put64(long value
)
170 mWriter
.putLong(value
);
175 * Convert the internal buffer to a new byte array.
178 public byte[] toByteArray()
180 int length
= mWriter
.position();
181 byte[] bytes
= new byte[length
];
182 System
.arraycopy(mBuffer
, 0, bytes
, 0, length
);