FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / nio / Buffer.java
blob7d291bedbc135b6379ce76c73d619e3eb8152839
1 /* Buffer.java --
2 Copyright (C) 2002 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. */
38 package java.nio;
40 public abstract class Buffer
42 private int cap = 0;
43 private int limit = 0;
44 private int pos = 0;
45 private int mark = -1;
47 // Creates a new Buffer.
49 // Should be package private.
51 Buffer (int capacity, int limit, int position, int mark)
53 if (capacity < 0)
54 throw new IllegalArgumentException ();
56 cap = capacity;
57 limit (limit);
58 position (position);
60 if (mark > 0)
62 if (mark > pos)
63 throw new IllegalArgumentException ();
65 this.mark = mark;
69 /**
70 * Retrieves the capacity of the buffer.
72 public final int capacity ()
74 return cap;
77 /**
78 * Clears the buffer.
80 public final Buffer clear ()
82 limit = cap;
83 pos = 0;
84 mark = -1;
85 return this;
88 /**
89 * Flips the buffer.
91 public final Buffer flip ()
93 limit = pos;
94 pos = 0;
95 mark = -1;
96 return this;
99 /**
100 * Tells whether the buffer has remaining data to read or not.
102 public final boolean hasRemaining ()
104 return limit > pos;
108 * Tells whether this buffer is read only or not.
110 public abstract boolean isReadOnly ();
113 * Retrieves the current limit of the buffer.
115 public final int limit ()
117 return limit;
121 * Sets this buffer's limit.
123 * @param newLimit The new limit value; must be non-negative and no larger
124 * than this buffer's capacity.
126 * @exception IllegalArgumentException If the preconditions on newLimit
127 * do not hold.
129 public final Buffer limit (int newLimit)
131 if ((newLimit < 0) || (newLimit > cap))
132 throw new IllegalArgumentException ();
134 if (newLimit <= mark)
135 mark = -1;
137 if (pos > newLimit)
138 pos = newLimit - 1;
140 limit = newLimit;
141 return this;
145 * Sets this buffer's mark at its position.
147 public final Buffer mark ()
149 mark = pos;
150 return this;
154 * Retrieves the current position of this buffer.
156 public final int position ()
158 return pos;
162 * Sets this buffer's position. If the mark is defined and larger than the
163 * new position then it is discarded.
165 * @param newPosition The new position value; must be non-negative and no
166 * larger than the current limit.
168 * @exception IllegalArgumentException If the preconditions on newPosition
169 * do not hold
171 public final Buffer position (int newPosition)
173 if ((newPosition < 0) || (newPosition > limit))
174 throw new IllegalArgumentException ();
176 if (newPosition <= mark)
177 mark = -1;
179 pos = newPosition;
180 return this;
184 * Returns the number of elements between the current position and the limit.
186 public final int remaining()
188 return limit - pos;
192 * Resets this buffer's position to the previously-marked position.
194 * @exception InvalidMarkException If the mark has not been set.
196 public final Buffer reset()
198 if (mark == -1)
199 throw new InvalidMarkException ();
201 pos = mark;
202 return this;
206 * Rewinds this buffer. The position is set to zero and the mark
207 * is discarded.
209 public final Buffer rewind()
211 pos = 0;
212 mark = -1;
213 return this;