1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.nio; 28 29 import java.util.Spliterator; 30 31 /** 32 * A container for data of a specific primitive type. 33 * 34 * <p> A buffer is a linear, finite sequence of elements of a specific 35 * primitive type. Aside from its content, the essential properties of a 36 * buffer are its capacity, limit, and position: </p> 37 * 38 * <blockquote> 39 * 40 * <p> A buffer's <i>capacity</i> is the number of elements it contains. The 41 * capacity of a buffer is never negative and never changes. </p> 42 * 43 * <p> A buffer's <i>limit</i> is the index of the first element that should 44 * not be read or written. A buffer's limit is never negative and is never 45 * greater than its capacity. </p> 46 * 47 * <p> A buffer's <i>position</i> is the index of the next element to be 48 * read or written. A buffer's position is never negative and is never 49 * greater than its limit. </p> 50 * 51 * </blockquote> 52 * 53 * <p> There is one subclass of this class for each non-boolean primitive type. 54 * 55 * 56 * <h2> Transferring data </h2> 57 * 58 * <p> Each subclass of this class defines two categories of <i>get</i> and 59 * <i>put</i> operations: </p> 60 * 61 * <blockquote> 62 * 63 * <p> <i>Relative</i> operations read or write one or more elements starting 64 * at the current position and then increment the position by the number of 65 * elements transferred. If the requested transfer exceeds the limit then a 66 * relative <i>get</i> operation throws a {@link BufferUnderflowException} 67 * and a relative <i>put</i> operation throws a {@link 68 * BufferOverflowException}; in either case, no data is transferred. </p> 69 * 70 * <p> <i>Absolute</i> operations take an explicit element index and do not 71 * affect the position. Absolute <i>get</i> and <i>put</i> operations throw 72 * an {@link IndexOutOfBoundsException} if the index argument exceeds the 73 * limit. </p> 74 * 75 * </blockquote> 76 * 77 * <p> Data may also, of course, be transferred in to or out of a buffer by the 78 * I/O operations of an appropriate channel, which are always relative to the 79 * current position. 80 * 81 * 82 * <h2> Marking and resetting </h2> 83 * 84 * <p> A buffer's <i>mark</i> is the index to which its position will be reset 85 * when the {@link #reset reset} method is invoked. The mark is not always 86 * defined, but when it is defined it is never negative and is never greater 87 * than the position. If the mark is defined then it is discarded when the 88 * position or the limit is adjusted to a value smaller than the mark. If the 89 * mark is not defined then invoking the {@link #reset reset} method causes an 90 * {@link InvalidMarkException} to be thrown. 91 * 92 * 93 * <h2> Invariants </h2> 94 * 95 * <p> The following invariant holds for the mark, position, limit, and 96 * capacity values: 97 * 98 * <blockquote> 99 * <tt>0</tt> <tt><=</tt> 100 * <i>mark</i> <tt><=</tt> 101 * <i>position</i> <tt><=</tt> 102 * <i>limit</i> <tt><=</tt> 103 * <i>capacity</i> 104 * </blockquote> 105 * 106 * <p> A newly-created buffer always has a position of zero and a mark that is 107 * undefined. The initial limit may be zero, or it may be some other value 108 * that depends upon the type of the buffer and the manner in which it is 109 * constructed. Each element of a newly-allocated buffer is initialized 110 * to zero. 111 * 112 * 113 * <h2> Clearing, flipping, and rewinding </h2> 114 * 115 * <p> In addition to methods for accessing the position, limit, and capacity 116 * values and for marking and resetting, this class also defines the following 117 * operations upon buffers: 118 * 119 * <ul> 120 * 121 * <li><p> {@link #clear} makes a buffer ready for a new sequence of 122 * channel-read or relative <i>put</i> operations: It sets the limit to the 123 * capacity and the position to zero. </p></li> 124 * 125 * <li><p> {@link #flip} makes a buffer ready for a new sequence of 126 * channel-write or relative <i>get</i> operations: It sets the limit to the 127 * current position and then sets the position to zero. </p></li> 128 * 129 * <li><p> {@link #rewind} makes a buffer ready for re-reading the data that 130 * it already contains: It leaves the limit unchanged and sets the position 131 * to zero. </p></li> 132 * 133 * </ul> 134 * 135 * 136 * <h2> Read-only buffers </h2> 137 * 138 * <p> Every buffer is readable, but not every buffer is writable. The 139 * mutation methods of each buffer class are specified as <i>optional 140 * operations</i> that will throw a {@link ReadOnlyBufferException} when 141 * invoked upon a read-only buffer. A read-only buffer does not allow its 142 * content to be changed, but its mark, position, and limit values are mutable. 143 * Whether or not a buffer is read-only may be determined by invoking its 144 * {@link #isReadOnly isReadOnly} method. 145 * 146 * 147 * <h2> Thread safety </h2> 148 * 149 * <p> Buffers are not safe for use by multiple concurrent threads. If a 150 * buffer is to be used by more than one thread then access to the buffer 151 * should be controlled by appropriate synchronization. 152 * 153 * 154 * <h2> Invocation chaining </h2> 155 * 156 * <p> Methods in this class that do not otherwise have a value to return are 157 * specified to return the buffer upon which they are invoked. This allows 158 * method invocations to be chained; for example, the sequence of statements 159 * 160 * <blockquote><pre> 161 * b.flip(); 162 * b.position(23); 163 * b.limit(42);</pre></blockquote> 164 * 165 * can be replaced by the single, more compact statement 166 * 167 * <blockquote><pre> 168 * b.flip().position(23).limit(42);</pre></blockquote> 169 * 170 * 171 * @author Mark Reinhold 172 * @author JSR-51 Expert Group 173 * @since 1.4 174 */ 175 176 public abstract class Buffer { 177 178 /** 179 * The characteristics of Spliterators that traverse and split elements 180 * maintained in Buffers. 181 */ 182 static final int SPLITERATOR_CHARACTERISTICS = 183 Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED; 184 185 // Invariants: mark <= position <= limit <= capacity 186 private int mark = -1; 187 // Android-changed: position field non-private for use by Android's nio implementation classes. 188 int position = 0; 189 private int limit; 190 private int capacity; 191 192 // Used only by direct buffers 193 // NOTE: hoisted here for speed in JNI GetDirectBufferAddress 194 long address; 195 196 // Android-added: _elementSizeShift field for NIOAccess class and framework native code. 197 /** 198 * The log base 2 of the element size of this buffer. Each typed subclass 199 * (ByteBuffer, CharBuffer, etc.) is responsible for initializing this 200 * value. The value is used by JNI code in frameworks/base/ to avoid the 201 * need for costly 'instanceof' tests. 202 */ 203 final int _elementSizeShift; 204 205 // Creates a new buffer with the given mark, position, limit, and capacity, 206 // after checking invariants. 207 // 208 // Android-added: _elementSizeShift field for NIOAccess class and framework native code. Buffer(int mark, int pos, int lim, int cap, int elementSizeShift)209 Buffer(int mark, int pos, int lim, int cap, int elementSizeShift) { // package-private 210 if (cap < 0) 211 throw new IllegalArgumentException("Negative capacity: " + cap); 212 this.capacity = cap; 213 limit(lim); 214 position(pos); 215 if (mark >= 0) { 216 if (mark > pos) 217 throw new IllegalArgumentException("mark > position: (" 218 + mark + " > " + pos + ")"); 219 this.mark = mark; 220 } 221 // Android-added: _elementSizeShift field for NIOAccess class and framework native code. 222 _elementSizeShift = elementSizeShift; 223 } 224 225 /** 226 * Returns this buffer's capacity. 227 * 228 * @return The capacity of this buffer 229 */ capacity()230 public final int capacity() { 231 return capacity; 232 } 233 234 /** 235 * Returns this buffer's position. 236 * 237 * @return The position of this buffer 238 */ position()239 public final int position() { 240 return position; 241 } 242 243 /** 244 * Sets this buffer's position. If the mark is defined and larger than the 245 * new position then it is discarded. 246 * 247 * @param newPosition 248 * The new position value; must be non-negative 249 * and no larger than the current limit 250 * 251 * @return This buffer 252 * 253 * @throws IllegalArgumentException 254 * If the preconditions on <tt>newPosition</tt> do not hold 255 */ position(int newPosition)256 public Buffer position(int newPosition) { 257 if ((newPosition > limit) || (newPosition < 0)) 258 // Android-changed: Improved error message. 259 throw new IllegalArgumentException("Bad position " + newPosition + "/" + limit); 260 position = newPosition; 261 if (mark > position) mark = -1; 262 return this; 263 } 264 265 /** 266 * Returns this buffer's limit. 267 * 268 * @return The limit of this buffer 269 */ limit()270 public final int limit() { 271 return limit; 272 } 273 274 /** 275 * Sets this buffer's limit. If the position is larger than the new limit 276 * then it is set to the new limit. If the mark is defined and larger than 277 * the new limit then it is discarded. 278 * 279 * @param newLimit 280 * The new limit value; must be non-negative 281 * and no larger than this buffer's capacity 282 * 283 * @return This buffer 284 * 285 * @throws IllegalArgumentException 286 * If the preconditions on <tt>newLimit</tt> do not hold 287 */ limit(int newLimit)288 public Buffer limit(int newLimit) { 289 if ((newLimit > capacity) || (newLimit < 0)) 290 throw new IllegalArgumentException(); 291 limit = newLimit; 292 if (position > limit) position = limit; 293 if (mark > limit) mark = -1; 294 return this; 295 } 296 297 /** 298 * Sets this buffer's mark at its position. 299 * 300 * @return This buffer 301 */ mark()302 public Buffer mark() { 303 mark = position; 304 return this; 305 } 306 307 /** 308 * Resets this buffer's position to the previously-marked position. 309 * 310 * <p> Invoking this method neither changes nor discards the mark's 311 * value. </p> 312 * 313 * @return This buffer 314 * 315 * @throws InvalidMarkException 316 * If the mark has not been set 317 */ reset()318 public Buffer reset() { 319 int m = mark; 320 if (m < 0) 321 throw new InvalidMarkException(); 322 position = m; 323 return this; 324 } 325 326 /** 327 * Clears this buffer. The position is set to zero, the limit is set to 328 * the capacity, and the mark is discarded. 329 * 330 * <p> Invoke this method before using a sequence of channel-read or 331 * <i>put</i> operations to fill this buffer. For example: 332 * 333 * <blockquote><pre> 334 * buf.clear(); // Prepare buffer for reading 335 * in.read(buf); // Read data</pre></blockquote> 336 * 337 * <p> This method does not actually erase the data in the buffer, but it 338 * is named as if it did because it will most often be used in situations 339 * in which that might as well be the case. </p> 340 * 341 * @return This buffer 342 */ clear()343 public Buffer clear() { 344 position = 0; 345 limit = capacity; 346 mark = -1; 347 return this; 348 } 349 350 /** 351 * Flips this buffer. The limit is set to the current position and then 352 * the position is set to zero. If the mark is defined then it is 353 * discarded. 354 * 355 * <p> After a sequence of channel-read or <i>put</i> operations, invoke 356 * this method to prepare for a sequence of channel-write or relative 357 * <i>get</i> operations. For example: 358 * 359 * <blockquote><pre> 360 * buf.put(magic); // Prepend header 361 * in.read(buf); // Read data into rest of buffer 362 * buf.flip(); // Flip buffer 363 * out.write(buf); // Write header + data to channel</pre></blockquote> 364 * 365 * <p> This method is often used in conjunction with the {@link 366 * java.nio.ByteBuffer#compact compact} method when transferring data from 367 * one place to another. </p> 368 * 369 * @return This buffer 370 */ flip()371 public Buffer flip() { 372 limit = position; 373 position = 0; 374 mark = -1; 375 return this; 376 } 377 378 /** 379 * Rewinds this buffer. The position is set to zero and the mark is 380 * discarded. 381 * 382 * <p> Invoke this method before a sequence of channel-write or <i>get</i> 383 * operations, assuming that the limit has already been set 384 * appropriately. For example: 385 * 386 * <blockquote><pre> 387 * out.write(buf); // Write remaining data 388 * buf.rewind(); // Rewind buffer 389 * buf.get(array); // Copy data into array</pre></blockquote> 390 * 391 * @return This buffer 392 */ rewind()393 public Buffer rewind() { 394 position = 0; 395 mark = -1; 396 return this; 397 } 398 399 /** 400 * Returns the number of elements between the current position and the 401 * limit. 402 * 403 * @return The number of elements remaining in this buffer 404 */ remaining()405 public final int remaining() { 406 return limit - position; 407 } 408 409 /** 410 * Tells whether there are any elements between the current position and 411 * the limit. 412 * 413 * @return <tt>true</tt> if, and only if, there is at least one element 414 * remaining in this buffer 415 */ hasRemaining()416 public final boolean hasRemaining() { 417 return position < limit; 418 } 419 420 /** 421 * Tells whether or not this buffer is read-only. 422 * 423 * @return <tt>true</tt> if, and only if, this buffer is read-only 424 */ isReadOnly()425 public abstract boolean isReadOnly(); 426 427 /** 428 * Tells whether or not this buffer is backed by an accessible 429 * array. 430 * 431 * <p> If this method returns <tt>true</tt> then the {@link #array() array} 432 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked. 433 * </p> 434 * 435 * @return <tt>true</tt> if, and only if, this buffer 436 * is backed by an array and is not read-only 437 * 438 * @since 1.6 439 */ hasArray()440 public abstract boolean hasArray(); 441 442 /** 443 * Returns the array that backs this 444 * buffer <i>(optional operation)</i>. 445 * 446 * <p> This method is intended to allow array-backed buffers to be 447 * passed to native code more efficiently. Concrete subclasses 448 * provide more strongly-typed return values for this method. 449 * 450 * <p> Modifications to this buffer's content will cause the returned 451 * array's content to be modified, and vice versa. 452 * 453 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 454 * method in order to ensure that this buffer has an accessible backing 455 * array. </p> 456 * 457 * @return The array that backs this buffer 458 * 459 * @throws ReadOnlyBufferException 460 * If this buffer is backed by an array but is read-only 461 * 462 * @throws UnsupportedOperationException 463 * If this buffer is not backed by an accessible array 464 * 465 * @since 1.6 466 */ array()467 public abstract Object array(); 468 469 /** 470 * Returns the offset within this buffer's backing array of the first 471 * element of the buffer <i>(optional operation)</i>. 472 * 473 * <p> If this buffer is backed by an array then buffer position <i>p</i> 474 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>. 475 * 476 * <p> Invoke the {@link #hasArray hasArray} method before invoking this 477 * method in order to ensure that this buffer has an accessible backing 478 * array. </p> 479 * 480 * @return The offset within this buffer's array 481 * of the first element of the buffer 482 * 483 * @throws ReadOnlyBufferException 484 * If this buffer is backed by an array but is read-only 485 * 486 * @throws UnsupportedOperationException 487 * If this buffer is not backed by an accessible array 488 * 489 * @since 1.6 490 */ arrayOffset()491 public abstract int arrayOffset(); 492 493 /** 494 * Tells whether or not this buffer is 495 * <a href="ByteBuffer.html#direct"><i>direct</i></a>. 496 * 497 * @return <tt>true</tt> if, and only if, this buffer is direct 498 * 499 * @since 1.6 500 */ isDirect()501 public abstract boolean isDirect(); 502 503 504 // -- Package-private methods for bounds checking, etc. -- 505 506 /** 507 * Checks the current position against the limit, throwing a {@link 508 * BufferUnderflowException} if it is not smaller than the limit, and then 509 * increments the position. 510 * 511 * @return The current position value, before it is incremented 512 */ nextGetIndex()513 final int nextGetIndex() { // package-private 514 if (position >= limit) 515 throw new BufferUnderflowException(); 516 return position++; 517 } 518 nextGetIndex(int nb)519 final int nextGetIndex(int nb) { // package-private 520 if (limit - position < nb) 521 throw new BufferUnderflowException(); 522 int p = position; 523 position += nb; 524 return p; 525 } 526 527 /** 528 * Checks the current position against the limit, throwing a {@link 529 * BufferOverflowException} if it is not smaller than the limit, and then 530 * increments the position. 531 * 532 * @return The current position value, before it is incremented 533 */ nextPutIndex()534 final int nextPutIndex() { // package-private 535 if (position >= limit) 536 throw new BufferOverflowException(); 537 return position++; 538 } 539 nextPutIndex(int nb)540 final int nextPutIndex(int nb) { // package-private 541 if (limit - position < nb) 542 throw new BufferOverflowException(); 543 int p = position; 544 position += nb; 545 return p; 546 } 547 548 /** 549 * Checks the given index against the limit, throwing an {@link 550 * IndexOutOfBoundsException} if it is not smaller than the limit 551 * or is smaller than zero. 552 */ checkIndex(int i)553 final int checkIndex(int i) { // package-private 554 if ((i < 0) || (i >= limit)) 555 // Android-changed: Add bounds details to exception. 556 throw new IndexOutOfBoundsException( 557 "index=" + i + " out of bounds (limit=" + limit + ")"); 558 return i; 559 } 560 checkIndex(int i, int nb)561 final int checkIndex(int i, int nb) { // package-private 562 if ((i < 0) || (nb > limit - i)) 563 // Android-changed: Add bounds details to exception. 564 throw new IndexOutOfBoundsException( 565 "index=" + i + " out of bounds (limit=" + limit + ", nb=" + nb + ")"); 566 return i; 567 } 568 markValue()569 final int markValue() { // package-private 570 return mark; 571 } 572 truncate()573 final void truncate() { // package-private 574 mark = -1; 575 position = 0; 576 limit = 0; 577 capacity = 0; 578 } 579 discardMark()580 final void discardMark() { // package-private 581 mark = -1; 582 } 583 checkBounds(int off, int len, int size)584 static void checkBounds(int off, int len, int size) { // package-private 585 if ((off | len | (off + len) | (size - (off + len))) < 0) 586 // Android-changed: Add bounds details to exception. 587 throw new IndexOutOfBoundsException( 588 "off=" + off + ", len=" + len + " out of bounds (size=" + size + ")"); 589 } 590 591 // Android-added: getElementSizeShift() method for testing. 592 /** 593 * For testing only. This field is accessed directly via JNI from frameworks code. 594 * 595 * @hide 596 */ getElementSizeShift()597 public int getElementSizeShift() { 598 return _elementSizeShift; 599 } 600 601 } 602