mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

matthew_swift
04.49.2009 9fdb95ca9c3c8e3524845760b81a85c7a4c81a45
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2009 Sun Microsystems, Inc.
 */
package org.opends.sdk;
 
 
 
import java.io.IOException;
import java.io.OutputStream;
 
 
 
 
/**
 * A {@code ByteSequence} is a readable sequence of byte values. This
 * interface provides uniform, read-only access to many different kinds
 * of byte sequences.
 */
public interface ByteSequence extends Comparable<ByteSequence>
{
 
  /**
   * Returns a {@link ByteSequenceReader} which can be used to
   * incrementally read and decode data from this byte sequence.
   * <p>
   * <b>NOTE:</b> any concurrent changes to the underlying byte sequence
   * (if mutable) may cause subsequent reads to overrun and fail.
   * 
   * @return The {@link ByteSequenceReader} which can be used to
   *         incrementally read and decode data from this byte sequence.
   */
  ByteSequenceReader asReader();
 
 
 
  /**
   * Returns the byte value at the specified index.
   * <p>
   * An index ranges from zero to {@code length() - 1}. The first byte
   * value of the sequence is at index zero, the next at index one, and
   * so on, as for array indexing.
   * 
   * @param index
   *          The index of the byte to be returned.
   * @return The byte value at the specified index.
   * @throws IndexOutOfBoundsException
   *           If the index argument is negative or not less than
   *           length().
   */
  byte byteAt(int index) throws IndexOutOfBoundsException;
 
 
 
  /**
   * Compares this byte sequence with the specified byte array
   * sub-sequence for order. Returns a negative integer, zero, or a
   * positive integer depending on whether this byte sequence is less
   * than, equal to, or greater than the specified byte array
   * sub-sequence.
   * 
   * @param b
   *          The byte array to compare.
   * @param offset
   *          The offset of the sub-sequence in the byte array to be
   *          compared; must be non-negative and no larger than {@code
   *          b.length} .
   * @param length
   *          The length of the sub-sequence in the byte array to be
   *          compared; must be non-negative and no larger than {@code
   *          b.length - offset}.
   * @return A negative integer, zero, or a positive integer depending
   *         on whether this byte sequence is less than, equal to, or
   *         greater than the specified byte array sub-sequence.
   * @throws IndexOutOfBoundsException
   *           If {@code offset} is negative or if {@code length} is
   *           negative or if {@code offset + length} is greater than
   *           {@code b.length}.
   */
  int compareTo(byte[] b, int offset, int length)
      throws IndexOutOfBoundsException;
 
 
 
  /**
   * Compares this byte sequence with the specified byte sequence for
   * order. Returns a negative integer, zero, or a positive integer
   * depending on whether this byte sequence is less than, equal to, or
   * greater than the specified object.
   * 
   * @param o
   *          The byte sequence to be compared.
   * @return A negative integer, zero, or a positive integer depending
   *         on whether this byte sequence is less than, equal to, or
   *         greater than the specified object.
   */
  int compareTo(ByteSequence o);
 
 
 
  /**
   * Copies the contents of this byte sequence to the provided byte
   * array.
   * <p>
   * Copying will stop when either the entire content of this sequence
   * has been copied or if the end of the provided byte array has been
   * reached.
   * <p>
   * An invocation of the form:
   * 
   * <pre>
   * src.copyTo(b)
   * </pre>
   * 
   * Behaves in exactly the same way as the invocation:
   * 
   * <pre>
   * src.copyTo(b, 0);
   * </pre>
   * 
   * @param b
   *          The byte array to which bytes are to be copied.
   * @return The byte array.
   */
  byte[] copyTo(byte[] b);
 
 
 
  /**
   * Copies the contents of this byte sequence to the specified location
   * in the provided byte array.
   * <p>
   * Copying will stop when either the entire content of this sequence
   * has been copied or if the end of the provided byte array has been
   * reached.
   * <p>
   * An invocation of the form:
   * 
   * <pre>
   * src.copyTo(b, offset)
   * </pre>
   * 
   * Behaves in exactly the same way as the invocation:
   * 
   * <pre>
   * int len = Math.min(src.length(), b.length - offset);
   * for (int i = 0; i &lt; len; i++)
   *   b[offset + i] = src.get(i);
   * </pre>
   * 
   * Except that it is potentially much more efficient.
   * 
   * @param b
   *          The byte array to which bytes are to be copied.
   * @param offset
   *          The offset within the array of the first byte to be
   *          written; must be non-negative and no larger than b.length.
   * @return The byte array.
   * @throws IndexOutOfBoundsException
   *           If {@code offset} is negative.
   */
  byte[] copyTo(byte[] b, int offset) throws IndexOutOfBoundsException;
 
 
 
  /**
   * Appends the entire contents of this byte sequence to the provided
   * {@link ByteStringBuilder}.
   * 
   * @param builder
   *          The builder to copy to.
   * @return The builder.
   */
  ByteStringBuilder copyTo(ByteStringBuilder builder);
 
 
 
  /**
   * Copies the entire contents of this byte sequence to the provided
   * {@code OutputStream}.
   * 
   * @param stream
   *          The {@code OutputStream} to copy to.
   * @return The {@code OutputStream}.
   * @throws IOException
   *           If an error occurs while writing to the {@code
   *           OutputStream}.
   */
  OutputStream copyTo(OutputStream stream) throws IOException;
 
 
 
  /**
   * Indicates whether the provided byte array sub-sequence is equal to
   * this byte sequence. In order for it to be considered equal, the
   * provided byte array sub-sequence must contain the same bytes in the
   * same order.
   * 
   * @param b
   *          The byte array for which to make the determination.
   * @param offset
   *          The offset of the sub-sequence in the byte array to be
   *          compared; must be non-negative and no larger than {@code
   *          b.length} .
   * @param length
   *          The length of the sub-sequence in the byte array to be
   *          compared; must be non-negative and no larger than {@code
   *          b.length - offset}.
   * @return {@code true} if the content of the provided byte array
   *         sub-sequence is equal to that of this byte sequence, or
   *         {@code false} if not.
   * @throws IndexOutOfBoundsException
   *           If {@code offset} is negative or if {@code length} is
   *           negative or if {@code offset + length} is greater than
   *           {@code b.length}.
   */
  boolean equals(byte[] b, int offset, int length)
      throws IndexOutOfBoundsException;
 
 
 
  /**
   * Indicates whether the provided object is equal to this byte
   * sequence. In order for it to be considered equal, the provided
   * object must be a byte sequence containing the same bytes in the
   * same order.
   * 
   * @param o
   *          The object for which to make the determination.
   * @return {@code true} if the provided object is a byte sequence
   *         whose content is equal to that of this byte sequence, or
   *         {@code false} if not.
   */
  boolean equals(Object o);
 
 
 
  /**
   * Returns a hash code for this byte sequence. It will be the sum of
   * all of the bytes contained in the byte sequence.
   * 
   * @return A hash code for this byte sequence.
   */
  int hashCode();
 
 
 
  /**
   * Returns the length of this byte sequence.
   * 
   * @return The length of this byte sequence.
   */
  int length();
 
 
 
  /**
   * Returns a new byte sequence that is a subsequence of this byte
   * sequence.
   * <p>
   * The subsequence starts with the byte value at the specified {@code
   * start} index and ends with the byte value at index {@code end - 1}.
   * The length (in bytes) of the returned sequence is {@code end -
   * start}, so if {@code start == end} then an empty sequence is
   * returned.
   * <p>
   * <b>NOTE:</b> changes to the underlying byte sequence (if mutable)
   * may render the returned sub-sequence invalid.
   * 
   * @param start
   *          The start index, inclusive.
   * @param end
   *          The end index, exclusive.
   * @return The newly created byte subsequence.
   * @throws IndexOutOfBoundsException
   *           If {@code start} or {@code end} are negative, if {@code
   *           end} is greater than {@code length()}, or if {@code
   *           start} is greater than {@code end}.
   */
  ByteSequence subSequence(int start, int end)
      throws IndexOutOfBoundsException;
 
 
 
  /**
   * Returns a byte array containing the bytes in this sequence in the
   * same order as this sequence. The length of the byte array will be
   * the length of this sequence.
   * <p>
   * An invocation of the form:
   * 
   * <pre>
   * src.toByteArray()
   * </pre>
   * 
   * Behaves in exactly the same way as the invocation:
   * 
   * <pre>
   * src.copyTo(new byte[src.length()]);
   * </pre>
   * 
   * @return A byte array consisting of exactly this sequence of bytes.
   */
  byte[] toByteArray();
 
 
 
  /**
   * Returns the {@link ByteString} representation of this byte
   * sequence.
   * 
   * @return The {@link ByteString} representation of this byte
   *         sequence.
   */
  ByteString toByteString();
 
 
 
  /**
   * Returns the UTF-8 decoded string representation of this byte
   * sequence. If UTF-8 decoding fails, the platform's default encoding
   * will be used.
   * 
   * @return The string representation of this byte sequence.
   */
  String toString();
}