2 Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu
.gcj
.RawData
;
46 public abstract class Buffer
55 * Creates a new Buffer.
57 * Should be package private.
59 Buffer (int capacity
, int limit
, int position
, int mark
)
62 throw new IllegalArgumentException ();
71 throw new IllegalArgumentException ();
78 * Retrieves the capacity of the buffer.
80 * @return the capacity of the buffer
82 public final int capacity ()
92 public final Buffer
clear ()
103 * @return this buffer
105 public final Buffer
flip ()
114 * Tells whether the buffer has remaining data to read or not.
116 * @return true if the buffer contains remaining data to read,
119 public final boolean hasRemaining ()
121 return remaining() > 0;
125 * Tells whether this buffer is read only or not.
127 * @return true if the buffer is read only, false otherwise
129 public abstract boolean isReadOnly ();
132 * Retrieves the current limit of the buffer.
134 * @return the limit of the buffer
136 public final int limit ()
142 * Sets this buffer's limit.
144 * @param newLimit The new limit value; must be non-negative and no larger
145 * than this buffer's capacity.
147 * @return this buffer
149 * @exception IllegalArgumentException If the preconditions on newLimit
152 public final Buffer
limit (int newLimit
)
154 if ((newLimit
< 0) || (newLimit
> cap
))
155 throw new IllegalArgumentException ();
168 * Sets this buffer's mark at its position.
170 * @return this buffer
172 public final Buffer
mark ()
179 * Retrieves the current position of this buffer.
181 * @return the current position of this buffer
183 public final int position ()
189 * Sets this buffer's position. If the mark is defined and larger than the
190 * new position then it is discarded.
192 * @param newPosition The new position value; must be non-negative and no
193 * larger than the current limit.
195 * @return this buffer
197 * @exception IllegalArgumentException If the preconditions on newPosition
200 public final Buffer
position (int newPosition
)
202 if ((newPosition
< 0) || (newPosition
> limit
))
203 throw new IllegalArgumentException ();
205 if (newPosition
<= mark
)
213 * Returns the number of elements between the current position and the limit.
215 * @return the number of remaining elements
217 public final int remaining()
223 * Resets this buffer's position to the previously-marked position.
225 * @return this buffer
227 * @exception InvalidMarkException If the mark has not been set.
229 public final Buffer
reset()
232 throw new InvalidMarkException ();
239 * Rewinds this buffer. The position is set to zero and the mark
242 * @return this buffer
244 public final Buffer
rewind()
252 * Checks for underflow. This method is used internally to check
253 * whether a buffer has enough elements left to satisfy a read
256 * @exception BufferUnderflowException If there are no remaining
257 * elements in this buffer.
259 final void checkForUnderflow()
262 throw new BufferUnderflowException();
266 * Checks for underflow. This method is used internally to check
267 * whether a buffer has enough elements left to satisfy a read
268 * request for a given number of elements.
270 * @param length The length of a sequence of elements.
272 * @exception BufferUnderflowException If there are not enough
273 * remaining elements in this buffer.
275 final void checkForUnderflow(int length
)
277 if (remaining() < length
)
278 throw new BufferUnderflowException();
282 * Checks for overflow. This method is used internally to check
283 * whether a buffer has enough space left to satisfy a write
286 * @exception BufferOverflowException If there is no remaining
287 * space in this buffer.
289 final void checkForOverflow()
292 throw new BufferOverflowException();
296 * Checks for overflow. This method is used internally to check
297 * whether a buffer has enough space left to satisfy a write
298 * request for a given number of elements.
300 * @param length The length of a sequence of elements.
302 * @exception BufferUnderflowException If there is not enough
303 * remaining space in this buffer.
305 final void checkForOverflow(int length
)
307 if (remaining() < length
)
308 throw new BufferOverflowException();
312 * Checks if index is negative or not smaller than the buffer's
313 * limit. This method is used internally to check whether
314 * an indexed request can be fulfilled.
316 * @param index The requested position in the buffer.
318 * @exception IndexOutOfBoundsException If index is negative or not smaller
319 * than the buffer's limit.
321 final void checkIndex(int index
)
324 || index
>= limit ())
325 throw new IndexOutOfBoundsException ();
329 * Checks if buffer is read-only. This method is used internally to
330 * check if elements can be put into a buffer.
332 * @exception ReadOnlyBufferException If this buffer is read-only.
334 final void checkIfReadOnly()
337 throw new ReadOnlyBufferException ();
341 * Checks whether an array is large enough to hold the given number of
342 * elements at the given offset. This method is used internally to
343 * check if an array is big enough.
345 * @param arraylength The length of the array.
346 * @param offset The offset within the array of the first byte to be read;
347 * must be non-negative and no larger than arraylength.
348 * @param length The number of bytes to be read from the given array;
349 * must be non-negative and no larger than arraylength - offset.
351 * @exception IndexOutOfBoundsException If the preconditions on the offset
352 * and length parameters do not hold
354 static final void checkArraySize(int arraylength
, int offset
, int length
)
358 (arraylength
< length
+ offset
))
359 throw new IndexOutOfBoundsException ();