aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/DNSOutput.java
blob: 29a8f68d507459b6e5da891621306297b00b9541 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

/**
 * A class for rendering DNS messages.
 *
 * @author Brian Wellington
 */


public class DNSOutput {

private byte [] array;
private int pos;
private int saved_pos;

/**
 * Create a new DNSOutput with a specified size.
 * @param size The initial size
 */
public
DNSOutput(int size) {
	array = new byte[size];
	pos = 0;
	saved_pos = -1;
}

/**
 * Create a new DNSOutput
 */
public
DNSOutput() {
	this(32);
}

/**
 * Returns the current position.
 */
public int
current() {
	return pos;
}

private void
check(long val, int bits) {
	long max = 1;
	max <<= bits;
	if (val < 0 || val > max) {
		throw new IllegalArgumentException(val + " out of range for " +
						   bits + " bit value");
	}
}

private void
need(int n) {
	if (array.length - pos >= n) {
		return;
	}
	int newsize = array.length * 2;
	if (newsize < pos + n) {
		newsize = pos + n;
	}
	byte [] newarray = new byte[newsize];
	System.arraycopy(array, 0, newarray, 0, pos);
	array = newarray;
}

/**
 * Resets the current position of the output stream to the specified index.
 * @param index The new current position.
 * @throws IllegalArgumentException The index is not within the output.
 */
public void
jump(int index) {
	if (index > pos) {
		throw new IllegalArgumentException("cannot jump past " +
						   "end of data");
	}
	pos = index;
}

/**
 * Saves the current state of the output stream.
 * @throws IllegalArgumentException The index is not within the output.
 */
public void
save() {
	saved_pos = pos;
}

/**
 * Restores the input stream to its state before the call to {@link #save}.
 */
public void
restore() {
	if (saved_pos < 0) {
		throw new IllegalStateException("no previous state");
	}
	pos = saved_pos;
	saved_pos = -1;
}

/**
 * Writes an unsigned 8 bit value to the stream.
 * @param val The value to be written
 */
public void
writeU8(int val) {
	check(val, 8);
	need(1);
	array[pos++] = (byte)(val & 0xFF);
}

/**
 * Writes an unsigned 16 bit value to the stream.
 * @param val The value to be written
 */
public void
writeU16(int val) {
	check(val, 16);
	need(2);
	array[pos++] = (byte)((val >>> 8) & 0xFF);
	array[pos++] = (byte)(val & 0xFF);
}

/**
 * Writes an unsigned 16 bit value to the specified position in the stream.
 * @param val The value to be written
 * @param where The position to write the value.
 */
public void
writeU16At(int val, int where) {
	check(val, 16);
	if (where > pos - 2)
		throw new IllegalArgumentException("cannot write past " +
						   "end of data");
	array[where++] = (byte)((val >>> 8) & 0xFF);
	array[where++] = (byte)(val & 0xFF);
}

/**
 * Writes an unsigned 32 bit value to the stream.
 * @param val The value to be written
 */
public void
writeU32(long val) {
	check(val, 32);
	need(4);
	array[pos++] = (byte)((val >>> 24) & 0xFF);
	array[pos++] = (byte)((val >>> 16) & 0xFF);
	array[pos++] = (byte)((val >>> 8) & 0xFF);
	array[pos++] = (byte)(val & 0xFF);
}

/**
 * Writes a byte array to the stream.
 * @param b The array to write.
 * @param off The offset of the array to start copying data from.
 * @param len The number of bytes to write.
 */
public void
writeByteArray(byte [] b, int off, int len) {
	need(len);
	System.arraycopy(b, off, array, pos, len);
	pos += len;
}

/**
 * Writes a byte array to the stream.
 * @param b The array to write.
 */
public void
writeByteArray(byte [] b) {
	writeByteArray(b, 0, b.length);
}

/**
 * Writes a counted string from the stream.  A counted string is a one byte
 * value indicating string length, followed by bytes of data.
 * @param s The string to write.
 */
public void
writeCountedString(byte [] s) {
	if (s.length > 0xFF) {
		throw new IllegalArgumentException("Invalid counted string");
	}
	need(1 + s.length);
	array[pos++] = (byte)(s.length & 0xFF);
	writeByteArray(s, 0, s.length);
}

/**
 * Returns a byte array containing the current contents of the stream.
 */
public byte []
toByteArray() {
	byte [] out = new byte[pos];
	System.arraycopy(array, 0, out, 0, pos);
	return out;
}

}