本文介绍了CharBuffer与char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是否有理由更喜欢 CharBuffer
到 char []
:
Is there any reason to prefer a CharBuffer
to a char[]
in the following:
CharBuffer buf = CharBuffer.allocate(DEFAULT_BUFFER_SIZE);
while( in.read(buf) >= 0 ) {
out.append( buf.flip() );
buf.clear();
}
vs。
char[] buf = new char[DEFAULT_BUFFER_SIZE];
int n;
while( (n = in.read(buf)) >= 0 ) {
out.write( buf, 0, n );
}
(其中 in
在 Writer
中是 Reader
和 out
?
推荐答案
不,在这种情况下,没有理由更喜欢 CharBuffer
。
No, there's really no reason to prefer a CharBuffer
in this case.
一般来说, CharBuffer
(以及 ByteBuffer
)可以真正简化API并鼓励正确处理。如果您正在设计公共API,那么绝对值得考虑面向缓冲区的API。
In general, though, CharBuffer
(and ByteBuffer
) can really simplify APIs and encourage correct processing. If you were designing a public API, it's definitely worth considering a buffer-oriented API.
这篇关于CharBuffer与char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!