Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / nio / Buffer.java
blobdff7f60c7441c1fac9d1c35dd4a988a0731dbfa6
1 /* Buffer.java --
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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.nio;
41 import gnu.gcj.RawData;
43 /**
44 * @since 1.4
46 public abstract class Buffer
48 int cap = 0;
49 int limit = 0;
50 int pos = 0;
51 int mark = -1;
52 RawData address;
54 /**
55 * Creates a new Buffer.
57 * Should be package private.
59 Buffer (int capacity, int limit, int position, int mark)
61 if (capacity < 0)
62 throw new IllegalArgumentException ();
64 cap = capacity;
65 limit (limit);
66 position (position);
68 if (mark >= 0)
70 if (mark > pos)
71 throw new IllegalArgumentException ();
73 this.mark = mark;
77 /**
78 * Retrieves the capacity of the buffer.
80 * @return the capacity of the buffer
82 public final int capacity ()
84 return cap;
87 /**
88 * Clears the buffer.
90 * @return this buffer
92 public final Buffer clear ()
94 limit = cap;
95 pos = 0;
96 mark = -1;
97 return this;
101 * Flips the buffer.
103 * @return this buffer
105 public final Buffer flip ()
107 limit = pos;
108 pos = 0;
109 mark = -1;
110 return this;
114 * Tells whether the buffer has remaining data to read or not.
116 * @return true if the buffer contains remaining data to read,
117 * false otherwise
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 ()
138 return 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
150 * do not hold.
152 public final Buffer limit (int newLimit)
154 if ((newLimit < 0) || (newLimit > cap))
155 throw new IllegalArgumentException ();
157 if (newLimit < mark)
158 mark = -1;
160 if (pos > newLimit)
161 pos = newLimit;
163 limit = newLimit;
164 return this;
168 * Sets this buffer's mark at its position.
170 * @return this buffer
172 public final Buffer mark ()
174 mark = pos;
175 return this;
179 * Retrieves the current position of this buffer.
181 * @return the current position of this buffer
183 public final int position ()
185 return pos;
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
198 * do not hold
200 public final Buffer position (int newPosition)
202 if ((newPosition < 0) || (newPosition > limit))
203 throw new IllegalArgumentException ();
205 if (newPosition <= mark)
206 mark = -1;
208 pos = newPosition;
209 return this;
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()
219 return limit - pos;
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()
231 if (mark == -1)
232 throw new InvalidMarkException ();
234 pos = mark;
235 return this;
239 * Rewinds this buffer. The position is set to zero and the mark
240 * is discarded.
242 * @return this buffer
244 public final Buffer rewind()
246 pos = 0;
247 mark = -1;
248 return this;
252 * Checks for underflow. This method is used internally to check
253 * whether a buffer has enough elements left to satisfy a read
254 * request.
256 * @exception BufferUnderflowException If there are no remaining
257 * elements in this buffer.
259 final void checkForUnderflow()
261 if (!hasRemaining())
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
284 * request.
286 * @exception BufferOverflowException If there is no remaining
287 * space in this buffer.
289 final void checkForOverflow()
291 if (!hasRemaining())
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)
323 if (index < 0
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()
336 if (isReadOnly())
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)
356 if ((offset < 0) ||
357 (length < 0) ||
358 (arraylength < length + offset))
359 throw new IndexOutOfBoundsException ();