diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-10 15:03:02 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-10 15:03:02 +0100 |
commit | 14a0470694ea7f24b8ef007783432a6f532e42ba (patch) | |
tree | ccdc94ca5a32fc9f7cf960a772dbf6f9b539d14c /vespajlib/src/main/java/com/yahoo/io | |
parent | db983b09c9f0ce597f5cf63d9ef6b0ebf20627e0 (diff) |
Move string encoding decoding to GrowableByteBuffer
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/io')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/io/GrowableByteBuffer.java | 32 |
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. * |