blob: a0c9b10c5191e812ae5dbe1da56e83cd449aa44d (
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 2017 Yahoo Holdings. 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 byte[] content;
/**
* Constructor, takes ownership
* @param content some bytes, handover
*/
public RawData(byte[] content) {
this.content = content;
}
/**
* @return 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
**/
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(Integer.toString(i));
buf.append(";");
}
}
return buf.toString();
}
}
|