aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/StringValue.java
blob: ed0e8d67baa9584ea739ad957995db526a08eb48 (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
66
67
68
69
70
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


import com.yahoo.text.Utf8Array;
import com.yahoo.text.Utf8String;

import java.nio.ByteBuffer;


/**
 * String value. The internal string representation is UTF-8 encoded
 * bytes. This means that creating an object of this class as well as
 * extracting the value contained with the {@link #asString asString}
 * method will incur a string conversion overhead.
 **/
public class StringValue extends Value
{
    private Utf8Array value;

    /**
     * Create from a Java-type value
     *
     * @param value the value
     **/
    public StringValue(String value) {
        this.value = new Utf8String(value);
    }
    public StringValue(Utf8String value) {
        this.value = value;
    }
    public StringValue(Utf8Array value) {
        this.value = value;
    }

    /**
     * Create by decoding the value from the given buffer
     *
     * @param src buffer where the value is stored
     **/
    StringValue(ByteBuffer src) {
        int size = src.getInt();
        value = new Utf8String(new Utf8Array(src, size));
    }

    /**
     * @return STRING
     **/
    public byte type() { return STRING; }
    public int count() { return 1; }

    int bytes() { return 4 + value.getByteLength(); }
    void encode(ByteBuffer dst) {
        dst.putInt(value.getByteLength());
        value.writeTo(dst);
    }

    public String asString() {
        return value.toString();
    }

    @Override
    public Utf8Array asUtf8Array() { return value; }

    @Override
    public String toString() {
        return asString();
    }

}