1
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
6
* A class for rendering DNS messages.
8
* @author Brian Wellington
12
public class DNSOutput {
14
private byte [] array;
16
private int saved_pos;
19
* Create a new DNSOutput with a specified size.
20
* @param size The initial size
24
array = new byte[size];
30
* Create a new DNSOutput
38
* Returns the current position.
46
check(long val, int bits) {
49
if (val < 0 || val > max) {
50
throw new IllegalArgumentException(val + " out of range for " +
57
if (array.length - pos >= n) {
60
int newsize = array.length * 2;
61
if (newsize < pos + n) {
64
byte [] newarray = new byte[newsize];
65
System.arraycopy(array, 0, newarray, 0, pos);
70
* Resets the current position of the output stream to the specified index.
71
* @param index The new current position.
72
* @throws IllegalArgumentException The index is not within the output.
77
throw new IllegalArgumentException("cannot jump past " +
84
* Saves the current state of the output stream.
85
* @throws IllegalArgumentException The index is not within the output.
93
* Restores the input stream to its state before the call to {@link #save}.
98
throw new IllegalStateException("no previous state");
105
* Writes an unsigned 8 bit value to the stream.
106
* @param val The value to be written
112
array[pos++] = (byte)(val & 0xFF);
116
* Writes an unsigned 16 bit value to the stream.
117
* @param val The value to be written
123
array[pos++] = (byte)((val >>> 8) & 0xFF);
124
array[pos++] = (byte)(val & 0xFF);
128
* Writes an unsigned 32 bit value to the stream.
129
* @param val The value to be written
135
array[pos++] = (byte)((val >>> 24) & 0xFF);
136
array[pos++] = (byte)((val >>> 16) & 0xFF);
137
array[pos++] = (byte)((val >>> 8) & 0xFF);
138
array[pos++] = (byte)(val & 0xFF);
142
* Writes a byte array to the stream.
143
* @param b The array to write.
144
* @param off The offset of the array to start copying data from.
145
* @param len The number of bytes to write.
148
writeByteArray(byte [] b, int off, int len) {
150
System.arraycopy(b, off, array, pos, len);
155
* Writes a byte array to the stream.
156
* @param b The array to write.
159
writeByteArray(byte [] b) {
160
writeByteArray(b, 0, b.length);
164
* Writes a counted string from the stream. A counted string is a one byte
165
* value indicating string length, followed by bytes of data.
166
* @param s The string to write.
169
writeCountedString(byte [] s) {
170
if (s.length > 0xFF) {
171
throw new IllegalArgumentException("Invalid counted string");
174
array[pos++] = (byte)(s.length & 0xFF);
175
writeByteArray(s, 0, s.length);
179
* Returns a byte array containing the current contents of the stream.
183
byte [] out = new byte[pos];
184
System.arraycopy(array, 0, out, 0, pos);