aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/hitfield/RawData.java
blob: 41f0191ad1e671919baf3d024122806a4af48058 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.hitfield;

/**
 * A representation of some binary data with unknown semantics
 *
 * @author arnej27959
 */
public final class RawData {

    private final byte[] content;

    /**
     * Constructor, takes ownership of the given byte array.
     *
     * @param content some bytes, handover
     */
    public RawData(byte[] content) {
        this.content = content;
    }

    /** Returns the internal byte array containing the actual data received */
    public byte[] getInternalData() {
        return content;
    }

    /**
     * An ascii string; non-ascii data is escaped with hex notation.
     * NB: not always uniquely reversible.
     */
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        for (byte b : content) {
            int i = b;
            i &= 0xFF;
            char cv = (char)i;
            if ((i > 31 && i < 127) || cv == '\n' || cv == '\t') {
                buf.append(cv);
            } else if (i < 16) {
                buf.append("\\x0");
                buf.append(Integer.toHexString(i));
            } else if (i < 256) {
                buf.append("\\x");
                buf.append(Integer.toHexString(i));
            } else {
                // XXX maybe we should only do this? creates possibly-invalid XML though.
                buf.append("&");
                buf.append(i);
                buf.append(";");
            }
        }
        return buf.toString();
    }

}