aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/text/Utf8Array.java
blob: 2d881ea2f627595a3f31284e5aea49238b8d0901 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Yahoo. 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 baldersheim
 */
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;
    }

}