Mon 23 Oct 2006
CharBuffer charBuffer = CharBuffer.wrap (”Hello World”); The three-argument
Posted by sales under techniquesOn 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