Buffer in Java

Source: Internet
Author: User

First, the attribute
There are four basic properties of buffer:
1. Capacity capacity, the maximum number of elements that can be accommodated in buffer, set when buffer is created and cannot be changed
2. The number of valid positions in limit buffer cannot read or write to areas exceeding the limit.
3, Position next read or write the position
4, Mark used for memory mark, with Reset (), the initial value is not set, call mark after the current position set to a value
Quad Relationship: 0 <= Mark <= position <= limit <= capacity

2. Direct vs. non-direct buffers

The byte buffers are either direct or non-direct. If it is a direct byte buffer, the Java Virtual Machine tries its best to perform native I/O operations directly on this buffer. That is, the virtual machine tries to avoid copying the contents of the buffer into the intermediate buffer (or copying the contents from the buffer in between) before each call to a native I/O operation of the underlying operating system.

A direct byte buffer can be created by calling the Allocatedireact () factory method of this class. The buffer returned by this method is typically higher than the non-direct buffer for allocation and deallocation. The contents of the direct buffers can reside outside the regular garbage collection heap, so their impact on the memory requirements of the application may not be obvious. Therefore, it is recommended that the direct buffers be allocated primarily to large, persistent buffers that are susceptible to native I/O operations of the underlying system. In general, it is best to assign them only if the direct buffers can bring significant benefits in terms of program performance.

Direct byte buffers can also be created by mapping the file area directly into memory through mapping. The implementation of the Java platform facilitates the creation of direct byte buffers from native code through JNI. If a buffer instance in these buffers refers to an inaccessible area of memory, attempting to access the zone does not change the contents of the buffer and will cause an indeterminate exception to be thrown during or at a later time during the visit.

The byte buffer is either a direct buffer or a non-direct buffer that can be determined by calling its Isdirect () method. This method is provided to enable explicit buffer management in performance-critical code. The binary data class defines a method that reads and writes all other base type values except Boolean . These base values can be converted to and from the byte sequence according to the current byte order of the buffer, and can be obtained and modified by the order method. The specific byte order is ByteOrder represented by an instance of the class. The initial order of the byte buffers is always BIG_ENDIAN .

To access heterogeneous binary data (that is, sequences of other types of values), this class also defines a series of absolute and relative get and put methods for each type. For example, for 32-bit floating-point values, this class defines the following methods:

float  getFloat() float  getFloat(int index)  void  putFloat(float f)  void  putFloat(int index, float f)

The corresponding methods aredefined for types such as char, short,int,long , and double . The index parameter of the absolute get and put method is defined by the byte, not by the type that is read and written.

To access homogeneous binary data (that is, a sequence of values of the same type), this class also defines a method that can create a view for a buffer of the specified type. A view buffer is just another buffer whose contents are supported by that byte buffer. Changes in the contents of the byte buffer are visible in the view buffer, and vice versa; the position, limit, and tag values of the two buffers are independent. For example, the asFloatBuffer method creates an instance of the class that is supported by the FloatBuffer byte buffer on which the method is called. The class defines the corresponding viewcreation method for types such as char, short,int,long , and double .

The view buffer has three main advantages over the type-specific get and put method families:

    • View buffers are indexed not by bytes, but by the size of their type-specific values;

    • The view buffers provide a relative bulk get and put method, which can transmit sequential sequences of values between buffers and arrays or other buffers of the same type;

    • The view buffer can be more efficient because it is a direct buffer when and only if its supported byte buffers are direct buffers.

The byte order of the view buffer is fixed to the byte order of its byte buffers when the view is created.

Buffer 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.