The buffer abstract class in Java

Source: Internet
Author: User
Tags compact

I. Overview

Buffer is an abstract class that is located under the Java.nio package and declares the following class: Publicabstract classBufferextends Object

Buffer is a container for data of a particular base type.

A buffer is a linear finite sequence of elements of a particular primitive type. In addition to content, the basic properties of a buffer include capacity, limits, and location:

The capacity of a buffer is the number of elements it contains. The capacity of the buffer cannot be negative and cannot be changed.

The limit for a buffer is the first index of an element that should not be read or written. The limit for a buffer cannot be negative and cannot be greater than its capacity.

The position of the buffer is the index of the next element to be read or written. The position of the buffer cannot be negative and cannot be greater than its limit.

For each non-Boolean base type, this class has a subclass corresponding to it.

Transfer data each subclass of this class defines two types of GetAnd placedOperation:

A relative operation reads or writes one or more elements, starting at the current position and then increasing the number of elements transferred. If the requested transport exceeds the limit, the relative fetch operation will be thrown, and the BufferUnderflowException relative drop will be thrown BufferOverflowException ; in both cases, no data is transmitted.

An absolute operation takes an explicit element index, which does not affect the location. If the index parameter exceeds the limit, the absolute get operation and the drop operation will be thrown IndexOutOfBoundsException .

Of course, I/O operations with appropriate channels (usually related to the current location) can also transfer data to or from the buffer.

To mark and reset buffers. Markis an index that calls the resetMethod resets the position of the buffer to the index. You do not always need to define a tag, but when you define a tag, you cannot define it as a negative number, and you cannot make it larger than the position. If a tag is defined, the tag is discarded when the position or limit is adjusted to a value that is less than the tag. If no tag is defined, then the call resetMethod causes the thrown InvalidMarkException

The invariant position, limit, and capacity values adhere to the following invariant:0 <= Mark <= location <= Limit<= capacity

The newly created buffer always has a 0 position and an undefined tag. The initial limit can be 0, or it can be a different value, depending on the type of buffer and how it is constructed. In general, the initial contents of the buffer are undefined.

Clear, invert, and re-wrap

In addition to accessing locations, limits, methods of capacity values, and methods for marking and resetting, this class defines the following operations that can be performed on buffers:

    • clear()Prepares the buffer for a series of new channel reads or relative placement operations: It sets the limit to the capacity size and sets the position to 0.

    • flip()Prepares the buffer for a series of new channel writes or relative fetches : It sets the limit to the current position, and then sets the position to 0.

    • rewind()Prepare the buffer for re-reading the contained data: it keeps the limit unchanged and sets the position to 0.

Read-only buffers each buffer is readable, but not every buffer is writable. The transformation method for each buffer class is specified as Optional Operationis thrown when a read-only buffer is called. ReadOnlyBufferException。 Read-only buffers do not allow changes to their contents, but their tags, positions, and limit values are variable. Can call its isReadOnlymethod to determine whether the buffer is read-only.

Thread-safe use of buffers by multiple current threads is not secure. If a buffer is used by more than one thread, access to the buffer should be controlled through appropriate synchronization.

The call chain specifies that methods in this class return buffers that call them (otherwise they will not return any values). This operation allows a chain of method calls to be made; for example, a sequence of statements

B.flip (); B.position (23); B.limit (42);
can be replaced by one of the following more compact statements
B.flip (). Position (at). Limit (42);


Second, the method

1. Public Final int capacity () Returns the capacity of this buffer.

2, publicfinal int position() returns the position of this buffer.

3. Public Final bufferposition(int newposition) Sets the position of this buffer. If the tag is defined and is greater than the new position, the token is discarded.

Parameters: newPosition -New position value, must be non-negative and not greater than current limit

return: this buffer

thrown: IllegalArgumentException -If newposition do not meet prerequisites

4. Public Final int Limit () returns the limit for this buffer.

5. Public Final Thelimit for this buffer is set by buffer limit (int newlimit). If the location is greater than the new limit, set it to the new limit. If the token is defined and is greater than the new limit, the token is discarded.

Parameters: newLimit -new limit value; must be non-negative and not greater than the capacity of this buffer

return: this buffer

thrown: IllegalArgumentException -If Newlimit do not meet prerequisites

6. Public Final BufferMark() sets the marker at the position of this buffer.

7. Public Final BufferReset() resets the position of this buffer to the location of the previously marked position. Call this method without changing or discarding the value of the tag.

return: this buffer
thrown: InvalidMarkException -If the tag has not been set
8. Public final Buffer Clear() clears this buffer. Set the location to 0, set the limit to capacity, and discard the tag.

This method is called before populating this buffer with a series of channel read or drop operations. For example:

Buf.clear ();     Prepare Buffer for reading In.read (BUF);    Read data

This method does not actually erase the data in the buffer, but it seems to be able to do so from the name because it is, in most cases, actually used when the data is purged.

9. Public final Buffer Flip()

Reverses this buffer. Set the limit to the current location first, and then set the location to 0. If a tag is already defined, the token is discarded. After a series of channel read or drop operations, call this method to prepare for a series of channel writes or relative fetch operations. For example:

Buf.put (Magic); Prepend Header

In.read (BUF); Read data into rest of buffer

Buf.flip (); Flip Buffer

Out.write (BUF); Write header + data to channel

This method is often used in conjunction with methods when transferring data from one place to another compact .

10, public final int remaining() returns the number of elements between the current position and the limit.

11. Public Final Boolean hasremaining() tells whether there are elements between the current position and the limit.

12. PublicAbstract Boolean isreadonly() tells if this buffer is a read-only buffer.

13. Public Abstract Boolean HasArray () tells if this buffer has an accessible array of underlying implementations. If this method returns True, it can be safely called array and the arrayOffset method.

14, Public abstract Object Array ()   (optional operation) This method is designed to have the underlying real The buffer of the current array can be passed to native code more efficiently. The specific subclass provides a stronger type of return value for this method. Call this method before calling hasarray

return: The underlying implementation array for this buffer

Thrown: ReadOnlyBufferException -If this buffer exists in the underlying implementation array, but it is read-only

          UnsupportedOperationException-If there is no accessible underlying implementation array for this buffer

15. public Abstract int Arrayoffset ()  

returns the offset of the first buffer element in the underlying implementation array of this buffer (optional operation).

If there is an underlying implementation array for this buffer, the buffer position p corresponds to the array index p + Arrayoffset ().

You should call this method before calling the hasArray method to ensure that the buffer has an accessible, underlying implementation array.

Return:
The offset of the first buffer element in this buffer array
Thrown:
ReadOnlyBufferException -If this buffer exists in the underlying implementation array, but it is read-only
UnsupportedOperationException -If this buffer does not exist an accessible underlying implementation array
16, public Abstract Boolean isdirect() tells if this buffer is a direct buffer .





The buffer abstract class in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.