This repository has been archived by the owner on Apr 12, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ByteSequences.java
69 lines (61 loc) · 2.1 KB
/
ByteSequences.java
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
/*
* Copyright (c) 2018 Red Hat, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.etcd.utils;
import com.google.errorprone.annotations.Var;
import io.etcd.jetcd.data.ByteSequence;
import java.util.Arrays;
/**
* Utilities for {@link ByteSequence}.
*
* @author Michael Vorburger.ch
*/
public final class ByteSequences {
private ByteSequences() { }
public static Object toStringable(ByteSequence byteSequence) {
return new Object() {
@Override
public String toString() {
return ByteSequences.asString(byteSequence);
}
};
}
public static String asString(ByteSequence byteSequence) {
return asString(byteSequence.getBytes());
}
// from ch.vorburger.dom2kv.bite.ByteSeq
private static String asString(byte[] bytes) {
StringBuilder text = new StringBuilder();
text.append('«');
@Var boolean isEscaping = false;
for (byte b : bytes) {
if (b >= 0x20 && b <= 0x7e) {
if (isEscaping) {
text.append('·');
isEscaping = false;
}
text.append((char) b);
} else {
if (!isEscaping) {
text.append('·');
isEscaping = true;
}
text.append(String.format("%02X", b & 0xFF));
}
}
text.append('»');
return text.toString();
}
public static ByteSequence fromBytes(byte... bytes) {
return ByteSequence.from(bytes);
}
public static ByteSequence append(ByteSequence base, byte... bytes) {
byte[] newBytes = Arrays.copyOf(base.getBytes(), base.getBytes().length + bytes.length);
System.arraycopy(bytes, 0, newBytes, base.getBytes().length, bytes.length);
return ByteSequence.from(newBytes);
}
}