本文整理匯總了Java中java.nio.CharBuffer.slice方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.slice方法的具體用法?Java CharBuffer.slice怎麽用?Java CharBuffer.slice使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.slice方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: slice
import java.nio.CharBuffer; //導入方法依賴的package包/類
public static CharBuffer slice(CharBuffer buf) {
CharBuffer buffer = buf.slice();
Buffer viewedBuffer = bufferViews.get(buf);
if (viewedBuffer != null) {
bufferViews.put(buffer, viewedBuffer);
}
return buffer;
}
示例2: assertRoundTrips
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static void assertRoundTrips(String str, int index, int size, byte[] bytes, Memory mem,
WritableMemory writeMem) throws IOException {
StringBuilder sb = new StringBuilder();
mem.getCharsFromUtf8(index, size, sb);
checkStrings(sb.toString(), new String(bytes, index, size, UTF_8));
CharBuffer cb = CharBuffer.allocate(bytes.length + 1);
cb.position(1);
// Make CharBuffer 1-based, to check correct offset handling
cb = cb.slice();
mem.getCharsFromUtf8(index, size, cb);
cb.flip();
checkStrings(cb.toString(), new String(bytes, index, size, UTF_8));
assertEquals(writeMem.putCharsToUtf8(0, str), bytes.length);
assertEquals(0, writeMem.compareTo(0, bytes.length, mem, 0, bytes.length));
// Test write overflow
WritableMemory writeMem2 = WritableMemory.allocate(bytes.length - 1);
try {
writeMem2.putCharsToUtf8(0, str);
fail();
} catch (Utf8CodingException e) {
// Expected.
}
}
示例3: main
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
*
* @param args
*/
public static void main(String args[]) {
CharBuffer charBuffer = CharBuffer.allocate(BUF_SIZE);
File file = new File("temp.txt");
file.deleteOnExit();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
char arr[] = new char[BUF_SIZE];
charBuffer = CharBuffer.wrap(arr);
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
char secArr[] = new char[BUF_SIZE];
charBuffer = CharBuffer.wrap(secArr);
charBuffer.put(str);
charBuffer.position(5);
charBuffer.limit(str.length() - 7);
charBuffer = charBuffer.slice();
checkFileContent(charBuffer, file, str.substring(5, (str.length() - 7)));
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(15, (str.length() - 7)));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
charBuffer = ByteBuffer.allocate(BUF_SIZE).asCharBuffer();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
charBuffer = ByteBuffer.allocateDirect(1024).asCharBuffer();
charBuffer.put(str);
charBuffer.flip();
checkFileContent(charBuffer, file, str);
charBuffer.position(10);
checkFileContent(charBuffer, file, str.substring(10));
charBuffer.position(charBuffer.limit());
checkFileContent(charBuffer, file, str.substring(charBuffer.limit()));
}