src/java.base/share/classes/java/nio/Buffer.java

Print this page




 222      *
 223      * @return  The position of this buffer
 224      */
 225     public final int position() {
 226         return position;
 227     }
 228 
 229     /**
 230      * Sets this buffer's position.  If the mark is defined and larger than the
 231      * new position then it is discarded.
 232      *
 233      * @param  newPosition
 234      *         The new position value; must be non-negative
 235      *         and no larger than the current limit
 236      *
 237      * @return  This buffer
 238      *
 239      * @throws  IllegalArgumentException
 240      *          If the preconditions on <tt>newPosition</tt> do not hold
 241      */
 242     public final Buffer position(int newPosition) {
 243         if ((newPosition > limit) || (newPosition < 0))
 244             throw new IllegalArgumentException();
 245         position = newPosition;
 246         if (mark > position) mark = -1;
 247         return this;
 248     }
 249 
 250     /**
 251      * Returns this buffer's limit.
 252      *
 253      * @return  The limit of this buffer
 254      */
 255     public final int limit() {
 256         return limit;
 257     }
 258 
 259     /**
 260      * Sets this buffer's limit.  If the position is larger than the new limit
 261      * then it is set to the new limit.  If the mark is defined and larger than
 262      * the new limit then it is discarded.
 263      *
 264      * @param  newLimit
 265      *         The new limit value; must be non-negative
 266      *         and no larger than this buffer's capacity
 267      *
 268      * @return  This buffer
 269      *
 270      * @throws  IllegalArgumentException
 271      *          If the preconditions on <tt>newLimit</tt> do not hold
 272      */
 273     public final Buffer limit(int newLimit) {
 274         if ((newLimit > capacity) || (newLimit < 0))
 275             throw new IllegalArgumentException();
 276         limit = newLimit;
 277         if (position > limit) position = limit;
 278         if (mark > limit) mark = -1;
 279         return this;
 280     }
 281 
 282     /**
 283      * Sets this buffer's mark at its position.
 284      *
 285      * @return  This buffer
 286      */
 287     public final Buffer mark() {
 288         mark = position;
 289         return this;
 290     }
 291 
 292     /**
 293      * Resets this buffer's position to the previously-marked position.
 294      *
 295      * <p> Invoking this method neither changes nor discards the mark's
 296      * value. </p>
 297      *
 298      * @return  This buffer
 299      *
 300      * @throws  InvalidMarkException
 301      *          If the mark has not been set
 302      */
 303     public final Buffer reset() {
 304         int m = mark;
 305         if (m < 0)
 306             throw new InvalidMarkException();
 307         position = m;
 308         return this;
 309     }
 310 
 311     /**
 312      * Clears this buffer.  The position is set to zero, the limit is set to
 313      * the capacity, and the mark is discarded.
 314      *
 315      * <p> Invoke this method before using a sequence of channel-read or
 316      * <i>put</i> operations to fill this buffer.  For example:
 317      *
 318      * <blockquote><pre>
 319      * buf.clear();     // Prepare buffer for reading
 320      * in.read(buf);    // Read data</pre></blockquote>
 321      *
 322      * <p> This method does not actually erase the data in the buffer, but it
 323      * is named as if it did because it will most often be used in situations
 324      * in which that might as well be the case. </p>
 325      *
 326      * @return  This buffer
 327      */
 328     public final Buffer clear() {
 329         position = 0;
 330         limit = capacity;
 331         mark = -1;
 332         return this;
 333     }
 334 
 335     /**
 336      * Flips this buffer.  The limit is set to the current position and then
 337      * the position is set to zero.  If the mark is defined then it is
 338      * discarded.
 339      *
 340      * <p> After a sequence of channel-read or <i>put</i> operations, invoke
 341      * this method to prepare for a sequence of channel-write or relative
 342      * <i>get</i> operations.  For example:
 343      *
 344      * <blockquote><pre>
 345      * buf.put(magic);    // Prepend header
 346      * in.read(buf);      // Read data into rest of buffer
 347      * buf.flip();        // Flip buffer
 348      * out.write(buf);    // Write header + data to channel</pre></blockquote>
 349      *
 350      * <p> This method is often used in conjunction with the {@link
 351      * java.nio.ByteBuffer#compact compact} method when transferring data from
 352      * one place to another.  </p>
 353      *
 354      * @return  This buffer
 355      */
 356     public final Buffer flip() {
 357         limit = position;
 358         position = 0;
 359         mark = -1;
 360         return this;
 361     }
 362 
 363     /**
 364      * Rewinds this buffer.  The position is set to zero and the mark is
 365      * discarded.
 366      *
 367      * <p> Invoke this method before a sequence of channel-write or <i>get</i>
 368      * operations, assuming that the limit has already been set
 369      * appropriately.  For example:
 370      *
 371      * <blockquote><pre>
 372      * out.write(buf);    // Write remaining data
 373      * buf.rewind();      // Rewind buffer
 374      * buf.get(array);    // Copy data into array</pre></blockquote>
 375      *
 376      * @return  This buffer
 377      */
 378     public final Buffer rewind() {
 379         position = 0;
 380         mark = -1;
 381         return this;
 382     }
 383 
 384     /**
 385      * Returns the number of elements between the current position and the
 386      * limit.
 387      *
 388      * @return  The number of elements remaining in this buffer
 389      */
 390     public final int remaining() {
 391         return limit - position;
 392     }
 393 
 394     /**
 395      * Tells whether there are any elements between the current position and
 396      * the limit.
 397      *
 398      * @return  <tt>true</tt> if, and only if, there is at least one element




 222      *
 223      * @return  The position of this buffer
 224      */
 225     public final int position() {
 226         return position;
 227     }
 228 
 229     /**
 230      * Sets this buffer's position.  If the mark is defined and larger than the
 231      * new position then it is discarded.
 232      *
 233      * @param  newPosition
 234      *         The new position value; must be non-negative
 235      *         and no larger than the current limit
 236      *
 237      * @return  This buffer
 238      *
 239      * @throws  IllegalArgumentException
 240      *          If the preconditions on <tt>newPosition</tt> do not hold
 241      */
 242     public Buffer position(int newPosition) {
 243         if ((newPosition > limit) || (newPosition < 0))
 244             throw new IllegalArgumentException();
 245         position = newPosition;
 246         if (mark > position) mark = -1;
 247         return this;
 248     }
 249 
 250     /**
 251      * Returns this buffer's limit.
 252      *
 253      * @return  The limit of this buffer
 254      */
 255     public final int limit() {
 256         return limit;
 257     }
 258 
 259     /**
 260      * Sets this buffer's limit.  If the position is larger than the new limit
 261      * then it is set to the new limit.  If the mark is defined and larger than
 262      * the new limit then it is discarded.
 263      *
 264      * @param  newLimit
 265      *         The new limit value; must be non-negative
 266      *         and no larger than this buffer's capacity
 267      *
 268      * @return  This buffer
 269      *
 270      * @throws  IllegalArgumentException
 271      *          If the preconditions on <tt>newLimit</tt> do not hold
 272      */
 273     public Buffer limit(int newLimit) {
 274         if ((newLimit > capacity) || (newLimit < 0))
 275             throw new IllegalArgumentException();
 276         limit = newLimit;
 277         if (position > limit) position = limit;
 278         if (mark > limit) mark = -1;
 279         return this;
 280     }
 281 
 282     /**
 283      * Sets this buffer's mark at its position.
 284      *
 285      * @return  This buffer
 286      */
 287     public Buffer mark() {
 288         mark = position;
 289         return this;
 290     }
 291 
 292     /**
 293      * Resets this buffer's position to the previously-marked position.
 294      *
 295      * <p> Invoking this method neither changes nor discards the mark's
 296      * value. </p>
 297      *
 298      * @return  This buffer
 299      *
 300      * @throws  InvalidMarkException
 301      *          If the mark has not been set
 302      */
 303     public Buffer reset() {
 304         int m = mark;
 305         if (m < 0)
 306             throw new InvalidMarkException();
 307         position = m;
 308         return this;
 309     }
 310 
 311     /**
 312      * Clears this buffer.  The position is set to zero, the limit is set to
 313      * the capacity, and the mark is discarded.
 314      *
 315      * <p> Invoke this method before using a sequence of channel-read or
 316      * <i>put</i> operations to fill this buffer.  For example:
 317      *
 318      * <blockquote><pre>
 319      * buf.clear();     // Prepare buffer for reading
 320      * in.read(buf);    // Read data</pre></blockquote>
 321      *
 322      * <p> This method does not actually erase the data in the buffer, but it
 323      * is named as if it did because it will most often be used in situations
 324      * in which that might as well be the case. </p>
 325      *
 326      * @return  This buffer
 327      */
 328     public Buffer clear() {
 329         position = 0;
 330         limit = capacity;
 331         mark = -1;
 332         return this;
 333     }
 334 
 335     /**
 336      * Flips this buffer.  The limit is set to the current position and then
 337      * the position is set to zero.  If the mark is defined then it is
 338      * discarded.
 339      *
 340      * <p> After a sequence of channel-read or <i>put</i> operations, invoke
 341      * this method to prepare for a sequence of channel-write or relative
 342      * <i>get</i> operations.  For example:
 343      *
 344      * <blockquote><pre>
 345      * buf.put(magic);    // Prepend header
 346      * in.read(buf);      // Read data into rest of buffer
 347      * buf.flip();        // Flip buffer
 348      * out.write(buf);    // Write header + data to channel</pre></blockquote>
 349      *
 350      * <p> This method is often used in conjunction with the {@link
 351      * java.nio.ByteBuffer#compact compact} method when transferring data from
 352      * one place to another.  </p>
 353      *
 354      * @return  This buffer
 355      */
 356     public Buffer flip() {
 357         limit = position;
 358         position = 0;
 359         mark = -1;
 360         return this;
 361     }
 362 
 363     /**
 364      * Rewinds this buffer.  The position is set to zero and the mark is
 365      * discarded.
 366      *
 367      * <p> Invoke this method before a sequence of channel-write or <i>get</i>
 368      * operations, assuming that the limit has already been set
 369      * appropriately.  For example:
 370      *
 371      * <blockquote><pre>
 372      * out.write(buf);    // Write remaining data
 373      * buf.rewind();      // Rewind buffer
 374      * buf.get(array);    // Copy data into array</pre></blockquote>
 375      *
 376      * @return  This buffer
 377      */
 378     public Buffer rewind() {
 379         position = 0;
 380         mark = -1;
 381         return this;
 382     }
 383 
 384     /**
 385      * Returns the number of elements between the current position and the
 386      * limit.
 387      *
 388      * @return  The number of elements remaining in this buffer
 389      */
 390     public final int remaining() {
 391         return limit - position;
 392     }
 393 
 394     /**
 395      * Tells whether there are any elements between the current position and
 396      * the limit.
 397      *
 398      * @return  <tt>true</tt> if, and only if, there is at least one element