| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore; |
| 7 |
|
| 8 |
import java.nio.ByteBuffer; |
| 9 |
|
| 10 |
/** |
| 11 |
* An auto-resize buffer to write data into a ByteBuffer. |
| 12 |
*/ |
| 13 |
public class WriteBuffer { |
| 14 |
|
| 15 |
/** |
| 16 |
* The maximum size of the buffer in order to be re-used after a clear |
| 17 |
* operation. |
| 18 |
*/ |
| 19 |
private static final int MAX_REUSE_CAPACITY = 4 * 1024 * 1024; |
| 20 |
|
| 21 |
/** |
| 22 |
* The minimum number of bytes to grow a buffer at a time. |
| 23 |
*/ |
| 24 |
private static final int MIN_GROW = 1024 * 1024; |
| 25 |
|
| 26 |
/** |
| 27 |
* The buffer that is used after a clear operation. |
| 28 |
*/ |
| 29 |
private ByteBuffer reuse; |
| 30 |
|
| 31 |
/** |
| 32 |
* The current buffer (may be replaced if it is too small). |
| 33 |
*/ |
| 34 |
private ByteBuffer buff; |
| 35 |
|
| 36 |
public WriteBuffer(int initialSize) { |
| 37 |
reuse = ByteBuffer.allocate(initialSize); |
| 38 |
buff = reuse; |
| 39 |
} |
| 40 |
|
| 41 |
public WriteBuffer() { |
| 42 |
this(MIN_GROW); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Write a variable size integer. |
| 47 |
* |
| 48 |
* @param x the value |
| 49 |
* @return this |
| 50 |
*/ |
| 51 |
public WriteBuffer putVarInt(int x) { |
| 52 |
DataUtils.writeVarInt(ensureCapacity(5), x); |
| 53 |
return this; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Write a variable size long. |
| 58 |
* |
| 59 |
* @param x the value |
| 60 |
* @return this |
| 61 |
*/ |
| 62 |
public WriteBuffer putVarLong(long x) { |
| 63 |
DataUtils.writeVarLong(ensureCapacity(10), x); |
| 64 |
return this; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Write the characters of a string in a format similar to UTF-8. |
| 69 |
* |
| 70 |
* @param s the string |
| 71 |
* @param len the number of characters to write |
| 72 |
* @return this |
| 73 |
*/ |
| 74 |
public WriteBuffer putStringData(String s, int len) { |
| 75 |
ByteBuffer b = ensureCapacity(3 * len); |
| 76 |
DataUtils.writeStringData(b, s, len); |
| 77 |
return this; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Put a byte. |
| 82 |
* |
| 83 |
* @param x the value |
| 84 |
* @return this |
| 85 |
*/ |
| 86 |
public WriteBuffer put(byte x) { |
| 87 |
ensureCapacity(1).put(x); |
| 88 |
return this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Put a character. |
| 93 |
* |
| 94 |
* @param x the value |
| 95 |
* @return this |
| 96 |
*/ |
| 97 |
public WriteBuffer putChar(char x) { |
| 98 |
ensureCapacity(2).putChar(x); |
| 99 |
return this; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Put a short. |
| 104 |
* |
| 105 |
* @param x the value |
| 106 |
* @return this |
| 107 |
*/ |
| 108 |
public WriteBuffer putShort(short x) { |
| 109 |
ensureCapacity(2).putShort(x); |
| 110 |
return this; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Put an integer. |
| 115 |
* |
| 116 |
* @param x the value |
| 117 |
* @return this |
| 118 |
*/ |
| 119 |
public WriteBuffer putInt(int x) { |
| 120 |
ensureCapacity(4).putInt(x); |
| 121 |
return this; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Put a long. |
| 126 |
* |
| 127 |
* @param x the value |
| 128 |
* @return this |
| 129 |
*/ |
| 130 |
public WriteBuffer putLong(long x) { |
| 131 |
ensureCapacity(8).putLong(x); |
| 132 |
return this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Put a float. |
| 137 |
* |
| 138 |
* @param x the value |
| 139 |
* @return this |
| 140 |
*/ |
| 141 |
public WriteBuffer putFloat(float x) { |
| 142 |
ensureCapacity(4).putFloat(x); |
| 143 |
return this; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Put a double. |
| 148 |
* |
| 149 |
* @param x the value |
| 150 |
* @return this |
| 151 |
*/ |
| 152 |
public WriteBuffer putDouble(double x) { |
| 153 |
ensureCapacity(8).putDouble(x); |
| 154 |
return this; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Put a byte array. |
| 159 |
* |
| 160 |
* @param bytes the value |
| 161 |
* @return this |
| 162 |
*/ |
| 163 |
public WriteBuffer put(byte[] bytes) { |
| 164 |
ensureCapacity(bytes.length).put(bytes); |
| 165 |
return this; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Put a byte array. |
| 170 |
* |
| 171 |
* @param bytes the value |
| 172 |
* @param offset the source offset |
| 173 |
* @param length the number of bytes |
| 174 |
* @return this |
| 175 |
*/ |
| 176 |
public WriteBuffer put(byte[] bytes, int offset, int length) { |
| 177 |
ensureCapacity(length).put(bytes, offset, length); |
| 178 |
return this; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Put the contents of a byte buffer. |
| 183 |
* |
| 184 |
* @param src the source buffer |
| 185 |
* @return this |
| 186 |
*/ |
| 187 |
public WriteBuffer put(ByteBuffer src) { |
| 188 |
ensureCapacity(src.remaining()).put(src); |
| 189 |
return this; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Set the limit, possibly growing the buffer. |
| 194 |
* |
| 195 |
* @param newLimit the new limit |
| 196 |
* @return this |
| 197 |
*/ |
| 198 |
public WriteBuffer limit(int newLimit) { |
| 199 |
ensureCapacity(newLimit - buff.position()).limit(newLimit); |
| 200 |
return this; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Get the capacity. |
| 205 |
* |
| 206 |
* @return the capacity |
| 207 |
*/ |
| 208 |
public int capacity() { |
| 209 |
return buff.capacity(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Set the position. |
| 214 |
* |
| 215 |
* @param newPosition the new position |
| 216 |
* @return the new position |
| 217 |
*/ |
| 218 |
public WriteBuffer position(int newPosition) { |
| 219 |
buff.position(newPosition); |
| 220 |
return this; |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Get the limit. |
| 225 |
* |
| 226 |
* @return the limit |
| 227 |
*/ |
| 228 |
public int limit() { |
| 229 |
return buff.limit(); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Get the current position. |
| 234 |
* |
| 235 |
* @return the position |
| 236 |
*/ |
| 237 |
public int position() { |
| 238 |
return buff.position(); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Copy the data into the destination array. |
| 243 |
* |
| 244 |
* @param dst the destination array |
| 245 |
* @return this |
| 246 |
*/ |
| 247 |
public WriteBuffer get(byte[] dst) { |
| 248 |
buff.get(dst); |
| 249 |
return this; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Update an integer at the given index. |
| 254 |
* |
| 255 |
* @param index the index |
| 256 |
* @param value the value |
| 257 |
* @return this |
| 258 |
*/ |
| 259 |
public WriteBuffer putInt(int index, int value) { |
| 260 |
buff.putInt(index, value); |
| 261 |
return this; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Update a short at the given index. |
| 266 |
* |
| 267 |
* @param index the index |
| 268 |
* @param value the value |
| 269 |
* @return this |
| 270 |
*/ |
| 271 |
public WriteBuffer putShort(int index, short value) { |
| 272 |
buff.putShort(index, value); |
| 273 |
return this; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Clear the buffer after use. |
| 278 |
* |
| 279 |
* @return this |
| 280 |
*/ |
| 281 |
public WriteBuffer clear() { |
| 282 |
if (buff.limit() > MAX_REUSE_CAPACITY) { |
| 283 |
buff = reuse; |
| 284 |
} else if (buff != reuse) { |
| 285 |
reuse = buff; |
| 286 |
} |
| 287 |
buff.clear(); |
| 288 |
return this; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Get the byte buffer. |
| 293 |
* |
| 294 |
* @return the byte buffer |
| 295 |
*/ |
| 296 |
public ByteBuffer getBuffer() { |
| 297 |
return buff; |
| 298 |
} |
| 299 |
|
| 300 |
private ByteBuffer ensureCapacity(int len) { |
| 301 |
if (buff.remaining() < len) { |
| 302 |
grow(len); |
| 303 |
} |
| 304 |
return buff; |
| 305 |
} |
| 306 |
|
| 307 |
private void grow(int additional) { |
| 308 |
ByteBuffer temp = buff; |
| 309 |
int needed = additional - temp.remaining(); |
| 310 |
// grow at least MIN_GROW |
| 311 |
long grow = Math.max(needed, MIN_GROW); |
| 312 |
// grow at least 50% of the current size |
| 313 |
grow = Math.max(temp.capacity() / 2, grow); |
| 314 |
// the new capacity is at most Integer.MAX_VALUE |
| 315 |
int newCapacity = (int) Math.min(Integer.MAX_VALUE, temp.capacity() + grow); |
| 316 |
if (newCapacity < needed) { |
| 317 |
throw new OutOfMemoryError("Capacity: " + newCapacity + " needed: " + needed); |
| 318 |
} |
| 319 |
try { |
| 320 |
buff = ByteBuffer.allocate(newCapacity); |
| 321 |
} catch (OutOfMemoryError e) { |
| 322 |
throw new OutOfMemoryError("Capacity: " + newCapacity); |
| 323 |
} |
| 324 |
temp.flip(); |
| 325 |
buff.put(temp); |
| 326 |
if (newCapacity <= MAX_REUSE_CAPACITY) { |
| 327 |
reuse = buff; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
} |