In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what the Java NIO Buffer process is like". In the daily operation, I believe many people have doubts about the Java NIO Buffer process. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what the Java NIO Buffer process is like!" Next, please follow the editor to study!
Preface
Use Java NIO Buffer when interacting with NIO channels. As you know, data is read from the channel into the buffer and written to the channel from the buffer.
A buffer is essentially a block of memory that can be written to data and then read again. This memory block is contained in the NIO Buffer object, which provides a set of methods that make it easier to use memory blocks.
Basic buffer usage
Using buffers to read and write data usually follows these four small steps:
1. Write data to buffer
two。 Call buffer.flip ()
3. Read data from buffer
4. Call buffer.clear () or buffer.compact ()
When you write data to Buffer, Buffer keeps track of how much data you have written. Once you need to read the data, you need to call the flip () method to change the Buffer from write mode to read mode. In read mode, Buffer allows you to read out all previously written data.
Once you have read all the data, you need to clear the Buffer to prepare for the next write. This can be done in two ways: clear () and compact (). The clear () method clears the entire Buffer, while the compact () method only clears the Buffer that you have read, the unread data is moved to the start of the Buffer, and the rewritten data is appended to the unread data.
This is a simple example of Buffer using write, flip, read and clear operations:
RandomAccessFile aFile = new RandomAccessFile ("data/nio-data.txt", "rw"); FileChannel inChannel = aFile.getChannel (); / / create buffer with capacity of 48 bytesByteBuffer buf = ByteBuffer.allocate (48); int bytesRead = inChannel.read (buf); / / read into buffer.while (bytesRead! =-1) {buf.flip (); / / make buffer ready for readwhile (buf.hasRemaining ()) {System.out.print ((char) buf.get ()); / / read 1 byte ata time} buf.clear () / / make buffer ready for writingbytesRead = inChannel.read (buf);} aFile.close ()
Three attributes of Buffer: capacity, location, and qualifier
Buffer is essentially an area of memory where you can write data, and of course you can also read data after writing. This memory area is encapsulated into a NIO Buffer object, which provides a series of methods to facilitate operations on the memory area.
In order to learn how Buffer works, you must be familiar with three properties of Buffer. They are:
Capacity (capacity) position (Vernier position) limit (end qualifier)
The meaning of position and limit depends on whether the current Buffer is in read mode or write mode. The meaning of capacity is the same regardless of read-write mode.
The following is an example of the above three properties in read mode and write mode, which will be explained in detail later:
Buffer capacity, position and limit in write and read mode.
Capacity (capacity)
As a memory block, Buffer has a fixed size, which we call "capacity". You can only write capacity-sized bytes, long integers, characters, etc., to Buffer. Once the Buffer is full, you must clear it (read the data, or clear the data) before continuing to write the data.
Position (cursor position)
When you start writing data to Buffer, you must know where the data will be written. The initial value of position is 0. When a similar data type, such as a byte or long integer, is written to Buffer, the position points to the next location where the data will be written (based on the size of the data type). The maximum value of position is capacity-1.
When you need to read data from Buffer, you also need to know where you are going to start reading data. When you call the flip method to change Buffer from write mode to read mode, position is reset to 0. Then you start reading from the location where position points, and then position points to the next location you want to read.
Limit (Limit)
The limit on a Buffer in write mode is how much data you can write to the Buffer. In write mode, the limit is equivalent to the capacity of Buffer (capacity).
When switching Buffer to read mode, the limit indicates how much data you can read at most. Therefore, when you switch Buffer to read mode, the limit is set to the position value in write mode. In other words, you can read all the data previously written (the limit is set to the number of bytes written, which is position in write mode).
Buffer Typ
Java NIO proposes the following types of Buffer:
ByteBuffer MappedByteBuffer CharBuffer DoubleBuffer FloatBuffer IntBuffer LongBuffer ShortBuffer
As you can see, these Buffer types represent different data types. In other words, they allow you to use char, short, int, long, float or double types instead of directly using bytes in buffer.
MappedByteBuffer is a bit special and will be described in its own part.
Assign a Buffer
To get a Buffer object, you must first allocate it. Each Buffer class has an allocate () function for allocation. The following example shows assigning a
ByteBuffer, with a capacity of 48 bytes.
ByteBuffer buf = ByteBuffer.allocate (48)
The following example is a CharBuffer that allocates a space of 1024 characters:
CharBuffer buf = CharBuffer.allocate (1024)
Write data to Buffer
There are two ways to write data to Buffer:
1. Write data from Channel to Buffer
two。 Call the put () function of buffer and write the data to Buffer yourself.
The following example shows how Channel writes data to Buffer:
Int bytesRead = inChannel.read (buf); / / read into buffer.
The following example writes data to Buffer through the put () function:
Buf.put (127C)
There are many other versions of the put () function that allow you to write data to Buffer in ways you don't use. For example, write at a specific location, or write a byte array to buffer. Check out JavaDoc for more details of the buffer implementation.
Flip ()
Use the flip () method to switch Buffer f from write mode to read mode. Call flip () to set the position back to 0 and set the limit to where it was just now.
In other words, position now marks the read location, and limit marks the number of bytes written to the buffer, the number of characters, and so on-- the limit on the number of bytes that can be read, the number of characters, and so on.
Read data from buffer
There are two ways to read data from Buffer.
1. Reads data from the buffer to the channel.
two。 Use the get () method in the buffer native method to read data from the buffer.
Here is an example of how to read data from a buffer to a channel:
/ / read from buffer into channel.int bytesWritten = inChannel.write (buf)
Here is an example of reading data from Buffer using the get () method:
Byte aByte = buf.get ()
There are many other versions of the get () method that allow you to read data from Buffer in many different ways. For example, read at a specific location, or read an array of bytes from a buffer. For more information about the specific buffer implementation, see JavaDoc.
Rewind ()
Buffer.rewind () sets position back to 0, so you can reread all the data in the buffer. This limit remains the same, so it still marks how many elements (bytes, characters, etc.) can be read from the Buffer.
Clear () and compact ()
After reading the data from the Buffer, you must prepare the Buffer for writing again. You can do this by calling clear () or compact ().
If you call clear (), position will be set to 0 and limit will be set to capacity. In other words, Buffer is cleared. The data in the Buffer was not purged. Only the tag tells you where the data can be written to Buffer.
When you call clear (), if there is any unread data in the buffer, the data is "forgotten", which means that you no longer have any flags indicating which data has been read and which is not.
If there is still unread data in the Buffer and you want to read it later, but you need to write something first, call compact () instead of clear ().
Compact () copies all unread data to the beginning of the Buffer. Then set the position to the position after the last unread element. Like clear (), the limit property is still set to capacity. Buffer is now ready to write, but will not overwrite unread data.
Mark () and reset ()
You can call the Buffer.mark () method to mark a given location in the Buffer. After that, you can call the Buffer.reset () method to reset the location of the tag. Here is an example:
Buffer.mark (); / / call buffer.get () a couple of times, e.g. During parsing.buffer.reset (); / / set position back to mark.
Equals () and compareTo ()
You can compare the two buffers with the equals () and compareTo () methods.
Equals ()
The two buffers are the same if:
1. They are of the same type (byte,char,int, etc.).
two。 In buffers, they leave the same amount of bytes, characters, and so on.
3. All remaining bytes and characters are the same.
As you can see, equals only compares parts of the Buffer, not each element. In fact, it only compares the elements left over from Buffer.
CompareTo ()
Use the compareTo () method to compare the legacy elements (bytes, characters, etc.) of the two buffers for example sorting routines. In the following cases, one buffer is considered "less than" the other if:
The first element equal to the corresponding element of another buffer is smaller than the element of the other buffer.
All elements are equal, but the first buffer runs out of elements before the second buffer (it has fewer elements).
At this point, the study of "what the Java NIO Buffer process is like" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.