-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathGPOSRecord.java
More file actions
156 lines (139 loc) · 4.77 KB
/
Copy pathGPOSRecord.java
File metadata and controls
156 lines (139 loc) · 4.77 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.IOException;
/**
* Geographical Location - describes the physical location of a host. (withdrawn -- this was a
* limited early version of {@link LOCRecord})
*
* @author Brian Wellington
* @see <a href="https://datatracker.ietf.org/doc/html/rfc1712">RFC 1712: DNS Encoding of
* Geographical Location</a>
*/
public class GPOSRecord extends Record {
private byte[] latitude;
private byte[] longitude;
private byte[] altitude;
GPOSRecord() {}
private void validate(double longitude, double latitude) throws IllegalArgumentException {
if (longitude < -90.0 || longitude > 90.0) {
throw new IllegalArgumentException("illegal longitude " + longitude);
}
if (latitude < -180.0 || latitude > 180.0) {
throw new IllegalArgumentException("illegal latitude " + latitude);
}
}
/**
* Creates an GPOS Record from the given data
*
* @param longitude The longitude component of the location.
* @param latitude The latitude component of the location.
* @param altitude The altitude component of the location (in meters above sea level).
*/
public GPOSRecord(
Name name, int dclass, long ttl, double longitude, double latitude, double altitude) {
super(name, Type.GPOS, dclass, ttl);
validate(longitude, latitude);
this.longitude = Double.toString(longitude).getBytes();
this.latitude = Double.toString(latitude).getBytes();
this.altitude = Double.toString(altitude).getBytes();
}
/**
* Creates an GPOS Record from the given data
*
* @param longitude The longitude component of the location.
* @param latitude The latitude component of the location.
* @param altitude The altitude component of the location (in meters above sea level).
*/
public GPOSRecord(
Name name, int dclass, long ttl, String longitude, String latitude, String altitude) {
super(name, Type.GPOS, dclass, ttl);
try {
this.longitude = byteArrayFromString(longitude);
this.latitude = byteArrayFromString(latitude);
validate(getLongitude(), getLatitude());
this.altitude = byteArrayFromString(altitude);
} catch (TextParseException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
@Override
protected void rrFromWire(DNSInput in) throws IOException {
longitude = in.readCountedString();
latitude = in.readCountedString();
altitude = in.readCountedString();
try {
validate(getLongitude(), getLatitude());
} catch (IllegalArgumentException e) {
throw new WireParseException(e.getMessage());
}
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
try {
longitude = byteArrayFromString(st.getString());
latitude = byteArrayFromString(st.getString());
altitude = byteArrayFromString(st.getString());
} catch (TextParseException e) {
throw st.exception(e.getMessage());
}
try {
validate(getLongitude(), getLatitude());
} catch (IllegalArgumentException e) {
throw new WireParseException(e.getMessage());
}
}
/** Convert to a String */
@Override
protected String rrToString() {
StringBuilder sb = new StringBuilder();
sb.append(byteArrayToString(longitude, true));
sb.append(" ");
sb.append(byteArrayToString(latitude, true));
sb.append(" ");
sb.append(byteArrayToString(altitude, true));
return sb.toString();
}
/** Returns the longitude as a string */
public String getLongitudeString() {
return byteArrayToString(longitude, false);
}
/**
* Returns the longitude as a double
*
* @throws NumberFormatException The string does not contain a valid numeric value.
*/
public double getLongitude() {
return Double.parseDouble(getLongitudeString());
}
/** Returns the latitude as a string */
public String getLatitudeString() {
return byteArrayToString(latitude, false);
}
/**
* Returns the latitude as a double
*
* @throws NumberFormatException The string does not contain a valid numeric value.
*/
public double getLatitude() {
return Double.parseDouble(getLatitudeString());
}
/** Returns the altitude as a string */
public String getAltitudeString() {
return byteArrayToString(altitude, false);
}
/**
* Returns the altitude as a double
*
* @throws NumberFormatException The string does not contain a valid numeric value.
*/
public double getAltitude() {
return Double.parseDouble(getAltitudeString());
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
out.writeCountedString(longitude);
out.writeCountedString(latitude);
out.writeCountedString(altitude);
}
}