diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /vespajlib/src/main/java/com/yahoo/text/Utf8Array.java |
Publish
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/text/Utf8Array.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/text/Utf8Array.java | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/text/Utf8Array.java b/vespajlib/src/main/java/com/yahoo/text/Utf8Array.java new file mode 100644 index 00000000000..30b2e665392 --- /dev/null +++ b/vespajlib/src/main/java/com/yahoo/text/Utf8Array.java @@ -0,0 +1,67 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.text; + + +import java.nio.ByteBuffer; + +/** + * This is a primitive class that owns an array of utf8 encoded string. + * This is a class that has speed as its primary purpose. + * If you have a string, consider Utf8String + * If you have a large backing array consider Utf8PartialArray + * @author <a href="mailto:balder@yahoo-inc.com">Henning Baldersheim</a> + * @since 5.2 + */ + +public class Utf8Array extends AbstractUtf8Array { + + protected final byte[] utf8; + + /** + * This will simply wrap the given array assuming it is valid utf8. + * Note that the immutability of this primitive class depends on that the buffer + * is not modified after ownership has been transferred. + * @param utf8data The utf8 byte sequence. + */ + public Utf8Array(final byte[] utf8data) { + utf8 = utf8data; + } + + /** + * This will create a new array from the window given. No validation done. + * Note that this will copy data. You might also want to consider Utf8PartialArray + * @param utf8data The base array. + * @param offset The offset from where to copy from + * @param length The number of bytes that should be copied. + */ + public Utf8Array(byte[] utf8data, int offset, int length) { + this.utf8 = new byte[length]; + System.arraycopy(utf8data, offset, this.utf8, 0, length); + } + + /** + * This will fetch length bytes from the given buffer. + * @param buf The ByteBuffer to read from + * @param length number of bytes to read + */ + public Utf8Array(ByteBuffer buf, int length) { + this.utf8 = new byte[length]; + buf.get(this.utf8, 0, length); + } + + @Override + public byte[] getBytes() { + return utf8; + } + + @Override + public int getByteLength() { + return utf8.length; + } + + @Override + protected int getByteOffset() { + return 0; + } + +} |