aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/slime/Utf8Value.java
blob: fd7cea1e18090ebc2a8f4670a5dea8f13461ebb5 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

/**
 * A value type encapsulating a String in its UTF-8 representation.
 * Useful for lazy decoding; if the data is just passed through in
 * UTF-8 it will never be converted at all.
 *
 * @author havardpe
 */
final class Utf8Value extends Value {

    private final byte[] value;
    private String string;
    private Utf8Value(byte[] value) { this.value = value; }
    public static Value create(byte[] value) {
        if (value == null) {
            return NixValue.instance();
        } else {
            return new Utf8Value(value);
        }
    }
    public Type type() { return Type.STRING; }
    public String asString() {
        if (string == null) {
            string = Utf8Codec.decode(value, 0, value.length);
        }
        return string;
    }
    public byte[] asUtf8() { return value; }
    public void accept(Visitor v) { v.visitString(value); }

}