1 7 8 package java.nio; 9 10 11 156 157 public abstract class Buffer { 158 159 private int mark = -1; 161 private int position = 0; 162 private int limit; 163 private int capacity; 164 165 long address; 168 169 Buffer(int mark, int pos, int lim, int cap) { if (cap < 0) 174 throw new IllegalArgumentException (); 175 this.capacity = cap; 176 limit(lim); 177 position(pos); 178 if (mark >= 0) { 179 if (mark > pos) 180 throw new IllegalArgumentException (); 181 this.mark = mark; 182 } 183 } 184 185 190 public final int capacity() { 191 return capacity; 192 } 193 194 199 public final int position() { 200 return position; 201 } 202 203 216 public final Buffer position(int newPosition) { 217 if ((newPosition > limit) || (newPosition < 0)) 218 throw new IllegalArgumentException (); 219 position = newPosition; 220 if (mark > position) mark = -1; 221 return this; 222 } 223 224 229 public final int limit() { 230 return limit; 231 } 232 233 247 public final Buffer limit(int newLimit) { 248 if ((newLimit > capacity) || (newLimit < 0)) 249 throw new IllegalArgumentException (); 250 limit = newLimit; 251 if (position > limit) position = limit; 252 if (mark > limit) mark = -1; 253 return this; 254 } 255 256 261 public final Buffer mark() { 262 mark = position; 263 return this; 264 } 265 266 277 public final Buffer reset() { 278 int m = mark; 279 if (m < 0) 280 throw new InvalidMarkException (); 281 position = m; 282 return this; 283 } 284 285 302 public final Buffer clear() { 303 position = 0; 304 limit = capacity; 305 mark = -1; 306 return this; 307 } 308 309 330 public final Buffer flip() { 331 limit = position; 332 position = 0; 333 mark = -1; 334 return this; 335 } 336 337 352 public final Buffer rewind() { 353 position = 0; 354 mark = -1; 355 return this; 356 } 357 358 364 public final int remaining() { 365 return limit - position; 366 } 367 368 375 public final boolean hasRemaining() { 376 return position < limit; 377 } 378 379 384 public abstract boolean isReadOnly(); 385 386 387 389 396 final int nextGetIndex() { if (position >= limit) 398 throw new BufferUnderflowException (); 399 return position++; 400 } 401 402 final int nextGetIndex(int nb) { if (limit - position < nb) 404 throw new BufferUnderflowException (); 405 int p = position; 406 position += nb; 407 return p; 408 } 409 410 417 final int nextPutIndex() { if (position >= limit) 419 throw new BufferOverflowException (); 420 return position++; 421 } 422 423 final int nextPutIndex(int nb) { if (limit - position < nb) 425 throw new BufferOverflowException (); 426 int p = position; 427 position += nb; 428 return p; 429 } 430 431 436 final int checkIndex(int i) { if ((i < 0) || (i >= limit)) 438 throw new IndexOutOfBoundsException (); 439 return i; 440 } 441 442 final int checkIndex(int i, int nb) { if ((i < 0) || (nb > limit - i)) 444 throw new IndexOutOfBoundsException (); 445 return i; 446 } 447 448 final int markValue() { return mark; 450 } 451 452 static void checkBounds(int off, int len, int size) { if ((off | len | (off + len) | (size - (off + len))) < 0) 454 throw new IndexOutOfBoundsException (); 455 } 456 457 } 458 | Popular Tags |