android: Migrate to the Gradle build system
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / utils / BufferedByteWriter.java
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 package org.strongswan.android.utils;
17
18 import java.nio.ByteBuffer;
19
20 /**
21 * Very similar to ByteBuffer (although with a stripped interface) but it
22 * automatically resizes the underlying buffer.
23 */
24 public class BufferedByteWriter
25 {
26 /**
27 * The underlying byte buffer
28 */
29 private byte[] mBuffer;
30
31 /**
32 * ByteBuffer used as wrapper around the buffer to easily convert values
33 */
34 private ByteBuffer mWriter;
35
36 /**
37 * Create a writer with a default initial capacity
38 */
39 public BufferedByteWriter()
40 {
41 this(0);
42 }
43
44 /**
45 * Create a writer with the given initial capacity (helps avoid expensive
46 * resizing if known).
47 * @param capacity initial capacity
48 */
49 public BufferedByteWriter(int capacity)
50 {
51 capacity = capacity > 4 ? capacity : 32;
52 mBuffer = new byte[capacity];
53 mWriter = ByteBuffer.wrap(mBuffer);
54 }
55
56 /**
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
60 */
61 private void ensureCapacity(int required)
62 {
63 if (mWriter.remaining() >= required)
64 {
65 return;
66 }
67 byte[] buffer = new byte[(mBuffer.length + required) * 2];
68 System.arraycopy(mBuffer, 0, buffer, 0, mWriter.position());
69 mBuffer = buffer;
70 ByteBuffer writer = ByteBuffer.wrap(buffer);
71 writer.position(mWriter.position());
72 mWriter = writer;
73 }
74
75 /**
76 * Write the given byte array to the buffer
77 * @param value
78 * @return the writer
79 */
80 public BufferedByteWriter put(byte[] value)
81 {
82 ensureCapacity(value.length);
83 mWriter.put(value);
84 return this;
85 }
86
87 /**
88 * Write the given byte to the buffer
89 * @param value
90 * @return the writer
91 */
92 public BufferedByteWriter put(byte value)
93 {
94 ensureCapacity(1);
95 mWriter.put(value);
96 return this;
97 }
98
99 /**
100 * Write the 8-bit length of the given data followed by the data itself
101 * @param value
102 * @return the writer
103 */
104 public BufferedByteWriter putLen8(byte[] value)
105 {
106 ensureCapacity(1 + value.length);
107 mWriter.put((byte)value.length);
108 mWriter.put(value);
109 return this;
110 }
111
112 /**
113 * Write the 16-bit length of the given data followed by the data itself
114 * @param value
115 * @return the writer
116 */
117 public BufferedByteWriter putLen16(byte[] value)
118 {
119 ensureCapacity(2 + value.length);
120 mWriter.putShort((short)value.length);
121 mWriter.put(value);
122 return this;
123 }
124
125 /**
126 * Write the given short value (16-bit) in big-endian order to the buffer
127 * @param value
128 * @return the writer
129 */
130 public BufferedByteWriter put16(short value)
131 {
132 ensureCapacity(2);
133 mWriter.putShort(value);
134 return this;
135 }
136
137 /**
138 * Write 24-bit of the given value in big-endian order to the buffer
139 * @param value
140 * @return the writer
141 */
142 public BufferedByteWriter put24(int value)
143 {
144 ensureCapacity(3);
145 mWriter.put((byte)(value >> 16));
146 mWriter.putShort((short)value);
147 return this;
148 }
149
150 /**
151 * Write the given int value (32-bit) in big-endian order to the buffer
152 * @param value
153 * @return the writer
154 */
155 public BufferedByteWriter put32(int value)
156 {
157 ensureCapacity(4);
158 mWriter.putInt(value);
159 return this;
160 }
161
162 /**
163 * Write the given long value (64-bit) in big-endian order to the buffer
164 * @param value
165 * @return the writer
166 */
167 public BufferedByteWriter put64(long value)
168 {
169 ensureCapacity(8);
170 mWriter.putLong(value);
171 return this;
172 }
173
174 /**
175 * Convert the internal buffer to a new byte array.
176 * @return byte array
177 */
178 public byte[] toByteArray()
179 {
180 int length = mWriter.position();
181 byte[] bytes = new byte[length];
182 System.arraycopy(mBuffer, 0, bytes, 0, length);
183 return bytes;
184 }
185 }