5.3 Byte-level Operations
In addition to providing basic data read and write operations, BYTEBUF provides a number of other methods, in the next section, we will discuss the more important of these methods to analyze and explain
5.3.1 Random Access Indexing
Like a normal Java byte array, BYTEBUF index subscript is also starting from 0, the first index table is 0, the last byte index is always its capacity-1, the following code listing shows you the BYTEBUF encapsulates its storage mechanism, Can make it easy to iterate over the contents of Bytebuf
Note that if you want to get the data in Bytebuf, the index values of Readerindex and Writerindex are not modified when you pass index subscript as a parameter. Unless there is a need to manually invoke the Readerindex and Writerindex methods to modify the corresponding values
5.3.2 Sequential Access indexing
Bytebuf has both a read index and a write index, but the bytebuffer in the JDK has only one index, which is why you need to call Flip's method to switch between read and write mode, and Figure 5.3 shows you an example of the bytebuf being cut into three parts of the two indexes.
5.3.3 discardable bytes
The segment structure labeled "discardable bytes" in 5.3 contains bytes that have been read, which can be reclaimed by calling the Discardreadbyte method, and then the effect of memory reuse can be obtained. The initialization size of this space is stored in Readerindex, which is 0, and its true size will gradually increment with the read operation (the method starting with Get does not move the Readerindex subscript value)
Figure 5.4 shows you in Figure 5.3 by calling the Discardreadbyte () method after the buffer space modified, you can see the previous space can be recycled in the paragraph can be used to write, note that after calling the Discardreadbyte method, The contents of the writable paragraphs are not guaranteed
If you often call the Discardreadbyte method to guarantee the maximum capacity of a writable segment, you realize that this may cause memory duplication, because the readable content of the "content" in the callout needs to be moved to Bytebuf's head. So we advise you to call Discardreadbyte to increase the amount of readable space, for example, memory is really tight, unless you really need it.
5.3.4 Readable bytes
The readable segment guarantees true data, a new assignment or wait to be overwritten, the default initial subscript for the copied initialized bytebuf is 0, any method that begins with the "read" and "skip" names to operate the BYTEBUF will first get the byte in the current array and then skip that byte. And the value of Readerindex +1, according to this rule, always read the next
When there is a method that takes bytebuf as a target for a write, and does not have a target index parameter, the write index of the target buffer also changes, for example:
The Indexoutofboundsexception exception occurs when the readable segment has been read and the attempt is made to continue reading.
The following code listing teaches you how to read readable bytes correctly
5.3.5 Writable bytes
There is no definition of anything in the memory area of the writable segment, these areas are waiting to be written, the index value of the default Writerindex of a new allocated buffer is 0, if the name of an operation is executed at the beginning of "write", This means that you start writing data from the current write index subscript, and the value of the index will increase as you increase the number of bytes, as well, if a write operation object is still bytebuf and does not specify the source index value, then the readerindex of the buffer will also increase, For example:
Indexoutofboundexception exception occurs if the writable space is still trying to write when it is exhausted
The following code listing shows an example of how to randomly write a value of an integer type when sufficient capacity is guaranteed, and the method writablebytes is used here to determine if there is enough space to read
5.3.6 Index Management
The JDK's InputStream defines the mark and reset two methods, which are used to mark the exact location of the current stream's index, and then to reset the stream to where it was just labeled.
Same as you can by calling Markreaderindex,markwriterindex,resetreaderindex, Resetwriteindex these methods to set and reposition the values of the Bytebuf Readerindex and Writeindex, which is very similar to the InputStream call, when no specific readerlimit parameter is specified, This label is illegal.
You can move two pointers by calling Readerindex (int) and writeindex (int) Two methods, of course, if you try to move the index of these two types of indexes to a nonexistent location will be reported indexoutofboundsexception The exception
You can also call the clear method to set the Readerindex and Writeindex two index subscript all to 0, note that this will not say in-memory content to clear out, figure 5.5 shows you how clear works
Figure 5.5 shows you what the clear method does before it executes, BYTEBUF contains 3 parts, and figure 5.6 shows you what bytebuf looks like after the clear method call
Calling clear is more economical than calling the Discardreadbytes method because it simply resets the two-index position to 0 and does not replicate any memory
5.3.7 Search Operations
There are several ways to determine the specific values of bytebuf, the simplest method is the IndexOf method, there are some retrieval methods by using a bytebufprocessor type of data as a parameter can be more complex retrieval, Bytebufprocessor is an interface in which there is an easy way
The meaning of this method is whether the input byte can be sought to
Bytebufprocessor also defines a number of target constants, and if your app interacts with a flash socket, the null is the end of the content and can be called
Getting the data in Flash is still very simple and efficient, because only a few checks are bound in the process
The following code listing shows you how to find (\ r) such uncommon characters
5.3.8 Derived Buffers
The derived buffer provides BYTEBUF with a special way of showing what it contains, and some special views can be created in a number of ways:
Each of these methods will return a new instance of Bytebuf and this instance has its own read-write tag index, the internal storage is consistent with the bytebuffer of the JDK, and the cost of creating a derived buffer is not very high, because there is no replication, This also means that if you modify the value of the derived buffer, you are modifying its native value as well.
bytebuf COPYING If you want to completely replicate an existing buffer, use the copy and copy (Int,int) method, Unlike the derived buffer, copy returns a completely new, independent bytebuf with no connection to the original BUF.
The following code listing shows you how a BYTEBUF segment works after using slice
Let's look at the Bytebuf section copy method is not the same as the slice method
The two use cases are very similar, except that the effect on the source data is not the same, and if possible, use slice to reduce the memory copy
5.3.9 read/write Operations
As we mentioned before, there are two big patterns for reading and writing.
1) The Get () and set () methods will start the operation from the given index, but the corresponding subscript does not change
2) the read () and write () methods start the operation from the given index, but also the value of the adjustment data corresponding to the operation
Table 5.1 shows you some of the commonly used get methods, the full list of methods to view the Java documentation
The corresponding common set method is shown in table 5.2:
The following code listing shows you how to use the Get and set methods, both of which do not change the index values for read and write
Let's take a look at the effect of the Read method on Readerindex or Writeindex, which can read bytebuf as a stream from here, and table 5.3 shows some of the most commonly used methods in the Read method family.
Almost all of the read methods have a write method that corresponds to writing bytes in Bytebuf, noting that all methods ' incoming arguments refer to the value written, not the index value
Code Listing 5.13 shows you how these methods work
5.3.10 More operations
Table 5.5 shows some of the other additional useful methods that BYTEBUF provides
Netty in Action (13) fifth chapter Part II Bytebuf byte-level operation