aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/StringArray.java
blob: 41e8ba5f8586932f6f659dc68300eedcb4d734a4 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


import java.nio.ByteBuffer;
import java.util.Arrays;


/**
 * String array. 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 #asStringArray
 * asStringArray} method will incur a string conversion overhead.
 **/
public class StringArray extends Value
{
    private byte[][] value;

    /**
     * Create from a Java-type value
     *
     * @param value the value
     **/
    public StringArray(String[] value) {
        this.value = new byte[value.length][];
        for (int i = 0; i < value.length; i++) {
            try {
                this.value[i] = value[i].getBytes("UTF-8");
            } catch(java.io.UnsupportedEncodingException e) {}
        }
    }

    /**
     * Create by decoding the value from the given buffer
     *
     * @param src buffer where the value is stored
     **/
    StringArray(ByteBuffer src) {
        int size = src.getInt();
        value = new byte[size][];
        for (int i = 0; i < size; i++) {
            value[i] = new byte[src.getInt()];
            src.get(value[i]);
        }
    }

    /**
     * @return STRING_ARRAY
     **/
    public byte type() { return STRING_ARRAY; }
    public int count() { return value.length; }

    int bytes() {
        int bytes = 4;
        for (int i = 0; i < value.length; i++) {
            bytes += 4 + value[i].length;
        }
        return bytes;
    }
    void encode(ByteBuffer dst) {
        dst.putInt(value.length);
        for (int i = 0; i < value.length; i++) {
            dst.putInt(value[i].length);
            dst.put(value[i]);
        }
    }

    public String[] asStringArray() {
        String[] ret = new String[value.length];
        for (int i = 0; i < value.length; i++) {
            try {
                ret[i] = new String(value[i], "UTF-8");
            } catch(java.io.UnsupportedEncodingException e) {}
        }
        return ret;
    }

    @Override
    public String toString() {
        return Arrays.toString(asStringArray());
    }

}