-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathISDNRecord.java
More file actions
142 lines (127 loc) · 3.9 KB
/
Copy pathISDNRecord.java
File metadata and controls
142 lines (127 loc) · 3.9 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* ISDN - identifies the ISDN number and subaddress associated with a name.
*
* @author Brian Wellington
* @see <a href="https://datatracker.ietf.org/doc/html/rfc1183">RFC 1183: New DNS RR Definitions</a>
*/
public class ISDNRecord extends Record {
private byte[] address;
private byte[] subAddress;
ISDNRecord() {}
/**
* Creates an ISDN Record from the given data
*
* @param address The ISDN number associated with the domain.
* @param subAddress The subaddress, if any.
* @throws IllegalArgumentException One of the strings is invalid.
*/
public ISDNRecord(Name name, int dclass, long ttl, String address, String subAddress) {
super(name, Type.ISDN, dclass, ttl);
try {
this.address = byteArrayFromString(address);
if (subAddress != null) {
this.subAddress = byteArrayFromString(subAddress);
}
} catch (TextParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
@Override
protected void rrFromWire(DNSInput in) throws IOException {
address = in.readCountedString();
if (in.remaining() > 0) {
subAddress = in.readCountedString();
}
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
try {
address = byteArrayFromString(st.getString());
Tokenizer.Token t = st.get();
if (t.isString()) {
subAddress = byteArrayFromString(t.value());
} else {
st.unget();
}
} catch (TextParseException e) {
throw st.exception(e.getMessage());
}
}
/**
* Returns the ISDN number associated with the domain as a string.
*
* @param escape if true, returns the RR textual representation of the underlying bytes. If false,
* returns just the simple string using the UTF-8 charset with no additional escaping.
* @since 3.6.5
*/
public String getAddress(boolean escape) {
return escape ? byteArrayToString(address, false) : new String(address, StandardCharsets.UTF_8);
}
/**
* Returns the ISDN number associated with the domain as a string, escaped for RR textual
* representation
*/
public String getAddress() {
return getAddress(true);
}
/**
* Returns the ISDN number associated with the domain as a raw byte-array
*
* @since 3.6.5
*/
public byte[] getAddressAsByteArray() {
return address;
}
/**
* Returns the ISDN subaddress as a string, or null if there is none.
*
* @param escape if true, returns the RR textual representation of the underlying bytes. If false,
* returns just the simple string using the UTF-8 charset with no additional escaping.
* @since 3.6.5
*/
public String getSubAddress(boolean escape) {
if (subAddress == null) {
return null;
}
return escape
? byteArrayToString(subAddress, false)
: new String(subAddress, StandardCharsets.UTF_8);
}
/**
* Returns the ISDN subaddress as a string, escaped for RR textual representation, or null if
* there is none.
*/
public String getSubAddress() {
return getSubAddress(true);
}
/**
* Returns the ISDN subaddress as a raw byte-array, or null if there is none.
*
* @since 3.6.5
*/
public byte[] getSubAddressAsByteArray() {
return subAddress;
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
out.writeCountedString(address);
if (subAddress != null) {
out.writeCountedString(subAddress);
}
}
@Override
protected String rrToString() {
StringBuilder sb = new StringBuilder();
sb.append(byteArrayToString(address, true));
if (subAddress != null) {
sb.append(" ");
sb.append(byteArrayToString(subAddress, true));
}
return sb.toString();
}
}