本文整理匯總了Java中java.nio.CharBuffer.compact方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.compact方法的具體用法?Java CharBuffer.compact怎麽用?Java CharBuffer.compact使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.compact方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fill
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* <p>Refill the buffer read from the socket.</p>
*
* Preconditions: <br/>
* buffer should have position @ beginning of unprocessed data. <br/>
* buffer should have limit @ end of unprocessed data. <br/>
*
* Postconditions: <br/>
* buffer should have position @ beginning of buffer (pos=0). <br/>
* buffer should have limit @ end of unprocessed data. <br/>
*
* Note: this method blocks on new data arriving.
*
* @param buffer The buffer to fill
* @param reader The Reader to read the data from
* @return number of characters read
* @throws IOException
*/
private int fill(CharBuffer buffer, Reader reader)
throws IOException {
// move existing data to the front of the buffer
buffer.compact();
// pull in as much data as we can from the socket
int charsRead = reader.read(buffer);
counterGroup.addAndGet("characters.received", Long.valueOf(charsRead));
// flip so the data can be consumed
buffer.flip();
return charsRead;
}