techniques


On the other hand, if the buffer holds more data than will fit in your array, you can iterate and pull it out in chunks with code like this: char [] smallArray = new char [10]; while (buffer.hasRemaining()) { int length = Math.min (buffer.remaining(), smallArray.length); buffer.get (smallArray, 0, length); processData (smallArray, length); } The bulk versions of put() behave similarly but move data in the opposite direction, from arrays into buffers. They have similar semantics regarding the size of transfers: buffer.put (myArray); is equivalent to: buffer.put (myArray, 0, myArray.length); If the buffer has room to accept the data in the array (buffer.remaining() >= myArray.length), the data will be copied into the buffer starting at the current position, and the buffer position will be advanced by the number of data elements added. If there is not sufficient room in the buffer, no data will be transferred, and a BufferOverflowException will be thrown. It’s also possible to do bulk moves of data from one buffer to another by calling put() with a buffer reference as argument: dstBuffer.put (srcBuffer); This is equivalent to (assuming dstBuffer has sufficient space): while (srcBuffer.hasRemaining()) { dstBuffer.put (srcBuffer.get()); } The positions of both buffers will be advanced by the number of data elements transferred. Range checks are done as they are for arrays. Specifically, if srcBuffer.remaining() is greater than dstBuffer.remaining(), then no data will be transferred, and BufferOverflowException will be thrown. In case you’re wondering, if you pass a buffer to itself, you’ll receive a big, fat java.lang.IllegalArgumentException for your hubris. I’ve been using CharBuffer for examples in this section, and so far, the discussion has also applied to other typed buffers, such as FloatBuffer, LongBuffer, etc. But the last two methods in the following API listing contain two bulk move methods unique to CharBuffer: 40
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services

CharBuffer charBuffer = CharBuffer.wrap (”Hello World”); The three-argument form takes start and end index positions describing a subsequence of the given CharSequence. This is a convenience pass-through to CharSequence.subsequence(). The start argument is the first character in the sequence to use; end is the last position of the character plus one. 2.3 Duplicating Buffers As we just discussed, buffer objects can be created that describe data elements stored externally in an array. But buffers are not limited to managing external data in arrays. They can also manage data externally in other buffers. When a buffer that manages data elements contained in another buffer is created, it’s known as a view buffer. Most view buffers are views of ByteBuffers (see Section 2.4.3). Before moving on to the specifics of byte buffers, we’ll concentrate on the views that are common to all buffer types. View buffers are always created by calling methods on an existing buffer instance. Using a factory method on an existing buffer instance means that the view object will be privy to internal implementation details of the original buffer. It will be able to access the data elements directly, whether they are stored in an array or by some other means, rather than going through the get()/put() API of the original buffer object. If the original buffer is direct, views of that buffer will have the same efficiency advantages. Likewise for mapped buffers (discussed in Chapter 3). In this section, we’ll again use CharBuffer as an example, but the same operations can be done on any of the primary buffer types (see Figure 2-1). public abstract class CharBuffer extends Buffer implements CharSequence, Comparable { // This is a partial API listing public abstract CharBuffer duplicate(); public abstract CharBuffer asReadOnlyBuffer(); public abstract CharBuffer slice(); } The duplicate() method creates a new buffer that is just like the original. Both buffers share the data elements and have the same capacity, but each buffer will have its own position, limit, and mark. Changes made to data elements in one buffer will be reflected in the other. The duplicate buffer has the same view of the data as the original buffer. If the original buffer is read-only, or direct, the new buffer will inherit those attributes. Direct buffers are discussed in Section 2.4.2. Duplicating a buffer creates a new Buffer object but does not make a copy of the data. Both the original buffer and the copy will act upon the same data elements. 44
Note: If you are looking for inexpensive but high quality provider to host and run your jsp application check Astra jsp hosting services