Skip to content

Commit 80097e5

Browse files
committed
Add getXxxAsByteArray() getters and overload with escape boolean
As discussed in the issue below, the library currently exposes character strings only as escaped strings for textual representation. This is a limitation as users are forced to deal with unnecessary escapes for application logic, and are also limited to interpret bytes as UTF-16 (as `byteArrayToString` casts bytes to `char`), whilst RFCs do not state which encoding for bytes should be used. This commit adds: - `getXxxAsByteArray()` getters that expose the "raw" byte array, allowing full control to users. This follows the already established pattern in 'TXTBase` with the `getStringsAsByteArrays()` method - an overload of the existing getters, which allows to pass an `escape` boolean: `true` (the default) is the current behavior, while `false` simply converts the bytes to a String using the UTF-8 encoding Closes #404
1 parent 6278133 commit 80097e5

15 files changed

+184
-29
lines changed

src/main/java/org/xbill/DNS/CAARecord.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78

89
/**
910
* Certification Authority Authorization
@@ -78,13 +79,29 @@ public int getFlags() {
7879
}
7980

8081
/** Returns the tag. */
82+
public String getTag(boolean escape) {
83+
return escape ? byteArrayToString(tag, false) : new String(tag, StandardCharsets.UTF_8);
84+
}
85+
8186
public String getTag() {
82-
return byteArrayToString(tag, false);
87+
return getTag(true);
88+
}
89+
90+
public byte[] getTagAsByteArray() {
91+
return tag;
8392
}
8493

8594
/** Returns the value */
95+
public String getValue(boolean escape) {
96+
return escape ? byteArrayToString(value, false) : new String(value, StandardCharsets.UTF_8);
97+
}
98+
8699
public String getValue() {
87-
return byteArrayToString(value, false);
100+
return getValue(true);
101+
}
102+
103+
public byte[] getValueAsByteArray() {
104+
return value;
88105
}
89106

90107
@Override

src/main/java/org/xbill/DNS/HINFORecord.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78

89
/**
910
* Host Information - describes the CPU and OS of a host
@@ -52,13 +53,30 @@ protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
5253
}
5354

5455
/** Returns the host's CPU */
56+
public String getCPU(boolean escape) {
57+
return escape ? byteArrayToString(cpu, false) : new String(cpu, StandardCharsets.UTF_8);
58+
}
59+
5560
public String getCPU() {
56-
return byteArrayToString(cpu, false);
61+
return getCPU(true);
5762
}
5863

64+
public byte[] getCPUAsByteArray() {
65+
return cpu;
66+
}
67+
68+
5969
/** Returns the host's OS */
70+
public String getOS(boolean escape) {
71+
return escape ? byteArrayToString(os, false) : new String(os, StandardCharsets.UTF_8);
72+
}
73+
6074
public String getOS() {
61-
return byteArrayToString(os, false);
75+
return getOS(true);
76+
}
77+
78+
public byte[] getOSAsByteArray() {
79+
return os;
6280
}
6381

6482
@Override

src/main/java/org/xbill/DNS/ISDNRecord.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78

89
/**
910
* ISDN - identifies the ISDN number and subaddress associated with a name.
@@ -60,19 +61,35 @@ protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
6061
}
6162

6263
/** Returns the ISDN number associated with the domain. */
64+
public String getAddress(boolean escape) {
65+
return escape ? byteArrayToString(address, false) : new String(address, StandardCharsets.UTF_8);
66+
}
67+
6368
public String getAddress() {
64-
return byteArrayToString(address, false);
69+
return getAddress(true);
70+
}
71+
72+
public byte[] getAddressAsByteArray() {
73+
return address;
6574
}
6675

6776
/** Returns the ISDN subaddress, or null if there is none. */
68-
public String getSubAddress() {
77+
public String getSubAddress(boolean escape) {
6978
if (subAddress == null) {
7079
return null;
7180
}
72-
return byteArrayToString(subAddress, false);
81+
return escape ? byteArrayToString(subAddress, false) : new String(subAddress, StandardCharsets.UTF_8);
7382
}
7483

75-
@Override
84+
public String getSubAddress() {
85+
return getSubAddress(true);
86+
}
87+
88+
public byte[] getSubAddressAsByteArray() {
89+
return subAddress;
90+
}
91+
92+
@Override
7693
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
7794
out.writeCountedString(address);
7895
if (subAddress != null) {

src/main/java/org/xbill/DNS/NAPTRRecord.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78

89
/**
910
* Name Authority Pointer Record - specifies rewrite rule, that when applied to an existing string
@@ -111,18 +112,42 @@ public int getPreference() {
111112
}
112113

113114
/** Returns flags */
115+
public String getFlags(boolean escape) {
116+
return escape ? byteArrayToString(flags, false) : new String(flags, StandardCharsets.UTF_8);
117+
}
118+
114119
public String getFlags() {
115-
return byteArrayToString(flags, false);
120+
return getFlags(true);
121+
}
122+
123+
public byte[] getFlagsAsByteArray() {
124+
return flags;
116125
}
117126

118127
/** Returns service */
128+
public String getService(boolean escape) {
129+
return escape ? byteArrayToString(service, false) : new String(service, StandardCharsets.UTF_8);
130+
}
131+
119132
public String getService() {
120-
return byteArrayToString(service, false);
133+
return getService(true);
134+
}
135+
136+
public byte[] getServiceAsByteArray() {
137+
return service;
121138
}
122139

123140
/** Returns regexp */
141+
public String getRegexp(boolean escape) {
142+
return escape ? byteArrayToString(regexp, false) : new String(regexp, StandardCharsets.UTF_8);
143+
}
144+
124145
public String getRegexp() {
125-
return byteArrayToString(regexp, false);
146+
return getRegexp(true);
147+
}
148+
149+
public byte[] getRegexpAsByteArray() {
150+
return regexp;
126151
}
127152

128153
/** Returns the replacement domain-name */

src/main/java/org/xbill/DNS/NSAPRecord.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.ByteArrayOutputStream;
77
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
9+
810
import org.xbill.DNS.utils.base16;
911

1012
/**
@@ -79,8 +81,16 @@ protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
7981
}
8082

8183
/** Returns the NSAP address. */
84+
public String getAddress(boolean escape) {
85+
return escape ? byteArrayToString(address, false) : new String(address, StandardCharsets.UTF_8);
86+
}
87+
8288
public String getAddress() {
83-
return byteArrayToString(address, false);
89+
return getAddress(true);
90+
}
91+
92+
public byte[] getAddressAsByteArray() {
93+
return address;
8494
}
8595

8696
@Override

src/main/java/org/xbill/DNS/TXTBase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78
import java.util.ArrayList;
89
import java.util.Collections;
910
import java.util.Iterator;
@@ -94,14 +95,18 @@ protected String rrToString() {
9495
*
9596
* @return A list of Strings corresponding to the text strings.
9697
*/
97-
public List<String> getStrings() {
98+
public List<String> getStrings(boolean escape) {
9899
List<String> list = new ArrayList<>(strings.size());
99100
for (byte[] string : strings) {
100-
list.add(byteArrayToString(string, false));
101+
list.add(escape ? byteArrayToString(string, false) : new String(string, StandardCharsets.UTF_8));
101102
}
102103
return list;
103104
}
104105

106+
public List<String> getStrings() {
107+
return getStrings(true);
108+
}
109+
105110
/**
106111
* Returns the text strings
107112
*

src/main/java/org/xbill/DNS/URIRecord.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package org.xbill.DNS;
66

77
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
89

910
/**
1011
* Uniform Resource Identifier (URI) DNS Resource Record
@@ -79,8 +80,16 @@ public int getWeight() {
7980
}
8081

8182
/** Returns the target URI */
83+
public String getTarget(boolean escape) {
84+
return escape ? byteArrayToString(target, false) : new String(target, StandardCharsets.UTF_8);
85+
}
86+
8287
public String getTarget() {
83-
return byteArrayToString(target, false);
88+
return getTarget(true);
89+
}
90+
91+
public byte[] getTargetAsByteArray() {
92+
return target;
8493
}
8594

8695
@Override

src/main/java/org/xbill/DNS/X25Record.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package org.xbill.DNS;
55

66
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
78

89
/**
910
* X25 - identifies the PSDN (Public Switched Data Network) address in the X.121 numbering plan
@@ -59,8 +60,16 @@ protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
5960
}
6061

6162
/** Returns the X.25 PSDN address. */
63+
public String getAddress(boolean escape) {
64+
return escape ? byteArrayToString(address, false) : new String(address, StandardCharsets.UTF_8);
65+
}
66+
6267
public String getAddress() {
63-
return byteArrayToString(address, false);
68+
return getAddress(true);
69+
}
70+
71+
public byte[] getAddressAsByteArray() {
72+
return address;
6473
}
6574

6675
@Override

src/test/java/org/xbill/DNS/CAARecordTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
package org.xbill.DNS;
33

4-
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
import static org.junit.jupiter.api.Assertions.assertThrows;
6-
74
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
87
import org.junit.jupiter.api.Test;
98

9+
import static org.junit.jupiter.api.Assertions.*;
10+
1011
class CAARecordTest {
1112

1213
Name n = Name.fromConstantString("my.name.");
@@ -16,7 +17,9 @@ void ctor_6arg() {
1617
CAARecord caa = new CAARecord(n, DClass.IN, 0, CAARecord.Flags.IssuerCritical, "", "");
1718
assertEquals(CAARecord.Flags.IssuerCritical, caa.getFlags());
1819
assertEquals("", caa.getTag());
20+
assertEquals(0, caa.getTagAsByteArray().length);
1921
assertEquals("", caa.getValue());
22+
assertEquals(0, caa.getValueAsByteArray().length);
2023

2124
String data = new String(new char[256]);
2225
IllegalArgumentException thrown =
@@ -32,6 +35,10 @@ void rdataFromString() throws IOException {
3235
CAARecord caa = new CAARecord();
3336
caa.rdataFromString(t, null);
3437
assertEquals("issue", caa.getTag());
38+
assertEquals("issue", caa.getTag(false));
39+
assertArrayEquals("issue".getBytes(StandardCharsets.UTF_8), caa.getTagAsByteArray());
3540
assertEquals("entrust.net", caa.getValue());
41+
assertEquals("entrust.net", caa.getValue(false));
42+
assertArrayEquals("entrust.net".getBytes(StandardCharsets.UTF_8), caa.getValueAsByteArray());
3643
}
3744
}

src/test/java/org/xbill/DNS/HINFORecordTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import static org.junit.jupiter.api.Assertions.assertThrows;
4242

4343
import java.io.IOException;
44+
import java.nio.charset.StandardCharsets;
45+
4446
import org.junit.jupiter.api.Test;
4547

4648
class HINFORecordTest {
@@ -120,7 +122,9 @@ void rdataFromString() throws IOException {
120122
HINFORecord dr = new HINFORecord();
121123
dr.rdataFromString(t, null);
122124
assertEquals(cpu, dr.getCPU());
125+
assertArrayEquals(cpu.getBytes(StandardCharsets.UTF_8), dr.getCPUAsByteArray());
123126
assertEquals(os, dr.getOS());
127+
assertArrayEquals(os.getBytes(StandardCharsets.UTF_8), dr.getOSAsByteArray());
124128
}
125129

126130
@Test

0 commit comments

Comments
 (0)