summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java
diff options
context:
space:
mode:
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java32
1 files changed, 25 insertions, 7 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java b/vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java
index c33882052b4..eba749bd14e 100644
--- a/vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java
+++ b/vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java
@@ -1,6 +1,8 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;
+import com.yahoo.text.Utf8;
+
import java.nio.*;
/**
@@ -20,21 +22,22 @@ import java.nio.*;
* No methods except getByteBuffer() expose the encapsulated
* ByteBuffer, which is intentional.
*
- * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
+ * @author Einar M R Rosenvinge
*/
public class GrowableByteBuffer implements Comparable<GrowableByteBuffer> {
+
public static final int DEFAULT_BASE_SIZE = 64*1024;
public static final float DEFAULT_GROW_FACTOR = 2.0f;
private ByteBuffer buffer;
private float growFactor;
private int mark = -1;
- //NOTE: It might have been better to subclass HeapByteBuffer,
- //but that class is package-private. Subclassing ByteBuffer would involve
- //implementing a lot of abstract methods, which would mean reinventing
- //some (too many) wheels.
+ // NOTE: It might have been better to subclass HeapByteBuffer,
+ // but that class is package-private. Subclassing ByteBuffer would involve
+ // implementing a lot of abstract methods, which would mean reinventing
+ // some (too many) wheels.
- //CONSTRUCTORS:
+ // CONSTRUCTORS:
public GrowableByteBuffer() {
this(DEFAULT_BASE_SIZE, DEFAULT_GROW_FACTOR);
@@ -61,7 +64,7 @@ public class GrowableByteBuffer implements Comparable<GrowableByteBuffer> {
}
- //ACCESSORS:
+ // ACCESSORS:
public float getGrowFactor() {
return growFactor;
@@ -364,6 +367,21 @@ public class GrowableByteBuffer implements Comparable<GrowableByteBuffer> {
}
}
+ /** Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes */
+ public void putUtf8String(String value) {
+ byte[] stringBytes = Utf8.toBytes(value);
+ putInt1_4Bytes(stringBytes.length);
+ put(stringBytes);
+ }
+
+ /** Reads a string from the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes */
+ public String getUtf8String() {
+ int stringLength = getInt1_4Bytes();
+ byte[] stringBytes = new byte[stringLength];
+ get(stringBytes);
+ return Utf8.toString(stringBytes);
+ }
+
/**
* Computes the size used for storing the given integer using 1 or 4 bytes.
*