~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to org/xbill/DNS/DNSOutput.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
 
2
 
 
3
package org.xbill.DNS;
 
4
 
 
5
/**
 
6
 * A class for rendering DNS messages.
 
7
 *
 
8
 * @author Brian Wellington
 
9
 */
 
10
 
 
11
 
 
12
public class DNSOutput {
 
13
 
 
14
private byte [] array;
 
15
private int pos;
 
16
private int saved_pos;
 
17
 
 
18
/**
 
19
 * Create a new DNSOutput with a specified size.
 
20
 * @param size The initial size
 
21
 */
 
22
public
 
23
DNSOutput(int size) {
 
24
        array = new byte[size];
 
25
        pos = 0;
 
26
        saved_pos = -1;
 
27
}
 
28
 
 
29
/**
 
30
 * Create a new DNSOutput
 
31
 */
 
32
public
 
33
DNSOutput() {
 
34
        this(32);
 
35
}
 
36
 
 
37
/**
 
38
 * Returns the current position.
 
39
 */
 
40
public int
 
41
current() {
 
42
        return pos;
 
43
}
 
44
 
 
45
private void
 
46
check(long val, int bits) {
 
47
        long max = 1;
 
48
        max <<= bits;
 
49
        if (val < 0 || val > max) {
 
50
                throw new IllegalArgumentException(val + " out of range for " +
 
51
                                                   bits + " bit value");
 
52
        }
 
53
}
 
54
 
 
55
private void
 
56
need(int n) {
 
57
        if (array.length - pos >= n) {
 
58
                return;
 
59
        }
 
60
        int newsize = array.length * 2;
 
61
        if (newsize < pos + n) {
 
62
                newsize = pos + n;
 
63
        }
 
64
        byte [] newarray = new byte[newsize];
 
65
        System.arraycopy(array, 0, newarray, 0, pos);
 
66
        array = newarray;
 
67
}
 
68
 
 
69
/**
 
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.
 
73
 */
 
74
public void
 
75
jump(int index) {
 
76
        if (index > pos) {
 
77
                throw new IllegalArgumentException("cannot jump past " +
 
78
                                                   "end of data");
 
79
        }
 
80
        pos = index;
 
81
}
 
82
 
 
83
/**
 
84
 * Saves the current state of the output stream.
 
85
 * @throws IllegalArgumentException The index is not within the output.
 
86
 */
 
87
public void
 
88
save() {
 
89
        saved_pos = pos;
 
90
}
 
91
 
 
92
/**
 
93
 * Restores the input stream to its state before the call to {@link #save}.
 
94
 */
 
95
public void
 
96
restore() {
 
97
        if (saved_pos < 0) {
 
98
                throw new IllegalStateException("no previous state");
 
99
        }
 
100
        pos = saved_pos;
 
101
        saved_pos = -1;
 
102
}
 
103
 
 
104
/**
 
105
 * Writes an unsigned 8 bit value to the stream.
 
106
 * @param val The value to be written
 
107
 */
 
108
public void
 
109
writeU8(int val) {
 
110
        check(val, 8);
 
111
        need(1);
 
112
        array[pos++] = (byte)(val & 0xFF);
 
113
}
 
114
 
 
115
/**
 
116
 * Writes an unsigned 16 bit value to the stream.
 
117
 * @param val The value to be written
 
118
 */
 
119
public void
 
120
writeU16(int val) {
 
121
        check(val, 16);
 
122
        need(2);
 
123
        array[pos++] = (byte)((val >>> 8) & 0xFF);
 
124
        array[pos++] = (byte)(val & 0xFF);
 
125
}
 
126
 
 
127
/**
 
128
 * Writes an unsigned 32 bit value to the stream.
 
129
 * @param val The value to be written
 
130
 */
 
131
public void
 
132
writeU32(long val) {
 
133
        check(val, 32);
 
134
        need(4);
 
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);
 
139
}
 
140
 
 
141
/**
 
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.
 
146
 */
 
147
public void
 
148
writeByteArray(byte [] b, int off, int len) {
 
149
        need(len);
 
150
        System.arraycopy(b, off, array, pos, len);
 
151
        pos += len;
 
152
}
 
153
 
 
154
/**
 
155
 * Writes a byte array to the stream.
 
156
 * @param b The array to write.
 
157
 */
 
158
public void
 
159
writeByteArray(byte [] b) {
 
160
        writeByteArray(b, 0, b.length);
 
161
}
 
162
 
 
163
/**
 
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.
 
167
 */
 
168
public void
 
169
writeCountedString(byte [] s) {
 
170
        if (s.length > 0xFF) {
 
171
                throw new IllegalArgumentException("Invalid counted string");
 
172
        }
 
173
        need(1 + s.length);
 
174
        array[pos++] = (byte)(s.length & 0xFF);
 
175
        writeByteArray(s, 0, s.length);
 
176
}
 
177
 
 
178
/**
 
179
 * Returns a byte array containing the current contents of the stream.
 
180
 */
 
181
public byte []
 
182
toByteArray() {
 
183
        byte [] out = new byte[pos];
 
184
        System.arraycopy(array, 0, out, 0, pos);
 
185
        return out;
 
186
}
 
187
 
 
188
}