| 1 | /* Buffer.java --
|
|---|
| 2 | Copyright (C) 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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.
|
|---|
| 20 |
|
|---|
| 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.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 | package java.nio;
|
|---|
| 39 |
|
|---|
| 40 | public abstract class Buffer
|
|---|
| 41 | {
|
|---|
| 42 | private int cap = 0;
|
|---|
| 43 | private int limit = 0;
|
|---|
| 44 | private int pos = 0;
|
|---|
| 45 | private int mark = -1;
|
|---|
| 46 |
|
|---|
| 47 | // Creates a new Buffer.
|
|---|
| 48 | //
|
|---|
| 49 | // Should be package private.
|
|---|
| 50 | //
|
|---|
| 51 | Buffer (int capacity, int limit, int position, int mark)
|
|---|
| 52 | {
|
|---|
| 53 | if (capacity < 0)
|
|---|
| 54 | throw new IllegalArgumentException ();
|
|---|
| 55 |
|
|---|
| 56 | cap = capacity;
|
|---|
| 57 | limit (limit);
|
|---|
| 58 | position (position);
|
|---|
| 59 |
|
|---|
| 60 | if (mark > 0)
|
|---|
| 61 | {
|
|---|
| 62 | if (mark > pos)
|
|---|
| 63 | throw new IllegalArgumentException ();
|
|---|
| 64 |
|
|---|
| 65 | this.mark = mark;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Retrieves the capacity of the buffer.
|
|---|
| 71 | */
|
|---|
| 72 | public final int capacity ()
|
|---|
| 73 | {
|
|---|
| 74 | return cap;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * Clears the buffer.
|
|---|
| 79 | */
|
|---|
| 80 | public final Buffer clear ()
|
|---|
| 81 | {
|
|---|
| 82 | limit = cap;
|
|---|
| 83 | pos = 0;
|
|---|
| 84 | mark = -1;
|
|---|
| 85 | return this;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Flips the buffer.
|
|---|
| 90 | */
|
|---|
| 91 | public final Buffer flip ()
|
|---|
| 92 | {
|
|---|
| 93 | limit = pos;
|
|---|
| 94 | pos = 0;
|
|---|
| 95 | mark = -1;
|
|---|
| 96 | return this;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Tells whether the buffer has remaining data to read or not.
|
|---|
| 101 | */
|
|---|
| 102 | public final boolean hasRemaining ()
|
|---|
| 103 | {
|
|---|
| 104 | return limit > pos;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | * Tells whether this buffer is read only or not.
|
|---|
| 109 | */
|
|---|
| 110 | public abstract boolean isReadOnly ();
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Retrieves the current limit of the buffer.
|
|---|
| 114 | */
|
|---|
| 115 | public final int limit ()
|
|---|
| 116 | {
|
|---|
| 117 | return limit;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Sets this buffer's limit.
|
|---|
| 122 | *
|
|---|
| 123 | * @param newLimit The new limit value; must be non-negative and no larger
|
|---|
| 124 | * than this buffer's capacity.
|
|---|
| 125 | *
|
|---|
| 126 | * @exception IllegalArgumentException If the preconditions on newLimit
|
|---|
| 127 | * do not hold.
|
|---|
| 128 | */
|
|---|
| 129 | public final Buffer limit (int newLimit)
|
|---|
| 130 | {
|
|---|
| 131 | if ((newLimit < 0) || (newLimit > cap))
|
|---|
| 132 | throw new IllegalArgumentException ();
|
|---|
| 133 |
|
|---|
| 134 | if (newLimit <= mark)
|
|---|
| 135 | mark = -1;
|
|---|
| 136 |
|
|---|
| 137 | if (pos > newLimit)
|
|---|
| 138 | pos = newLimit - 1;
|
|---|
| 139 |
|
|---|
| 140 | limit = newLimit;
|
|---|
| 141 | return this;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Sets this buffer's mark at its position.
|
|---|
| 146 | */
|
|---|
| 147 | public final Buffer mark ()
|
|---|
| 148 | {
|
|---|
| 149 | mark = pos;
|
|---|
| 150 | return this;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | /**
|
|---|
| 154 | * Retrieves the current position of this buffer.
|
|---|
| 155 | */
|
|---|
| 156 | public final int position ()
|
|---|
| 157 | {
|
|---|
| 158 | return pos;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * Sets this buffer's position. If the mark is defined and larger than the
|
|---|
| 163 | * new position then it is discarded.
|
|---|
| 164 | *
|
|---|
| 165 | * @param newPosition The new position value; must be non-negative and no
|
|---|
| 166 | * larger than the current limit.
|
|---|
| 167 | *
|
|---|
| 168 | * @exception IllegalArgumentException If the preconditions on newPosition
|
|---|
| 169 | * do not hold
|
|---|
| 170 | */
|
|---|
| 171 | public final Buffer position (int newPosition)
|
|---|
| 172 | {
|
|---|
| 173 | if ((newPosition < 0) || (newPosition > limit))
|
|---|
| 174 | throw new IllegalArgumentException ();
|
|---|
| 175 |
|
|---|
| 176 | if (newPosition <= mark)
|
|---|
| 177 | mark = -1;
|
|---|
| 178 |
|
|---|
| 179 | pos = newPosition;
|
|---|
| 180 | return this;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Returns the number of elements between the current position and the limit.
|
|---|
| 185 | */
|
|---|
| 186 | public final int remaining()
|
|---|
| 187 | {
|
|---|
| 188 | return limit - pos;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /**
|
|---|
| 192 | * Resets this buffer's position to the previously-marked position.
|
|---|
| 193 | *
|
|---|
| 194 | * @exception InvalidMarkException If the mark has not been set.
|
|---|
| 195 | */
|
|---|
| 196 | public final Buffer reset()
|
|---|
| 197 | {
|
|---|
| 198 | if (mark == -1)
|
|---|
| 199 | throw new InvalidMarkException ();
|
|---|
| 200 |
|
|---|
| 201 | pos = mark;
|
|---|
| 202 | return this;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /**
|
|---|
| 206 | * Rewinds this buffer. The position is set to zero and the mark
|
|---|
| 207 | * is discarded.
|
|---|
| 208 | */
|
|---|
| 209 | public final Buffer rewind()
|
|---|
| 210 | {
|
|---|
| 211 | pos = 0;
|
|---|
| 212 | mark = -1;
|
|---|
| 213 | return this;
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|