Java:CharBuffer“忽略”数组偏移量?
请帮助我理解以下内容:
我使用创建一个 CharBuffer
CharBuffer.wrapped(new char[12], 2, 10)
(数组、偏移量、长度)
所以我希望以 2
的偏移量和 10
的总(剩余)长度来访问该数组。但是 arrayOffset() 返回 0
。
我想了解的(并且无法从JavaDoc中弄清楚)是:
当
arrayOffset()
不是0
时,以及是否有可能让
CharBuffer
使用具有“真实”偏移量的数组(以便数组在该偏移量之前从未被访问过)?
这是一个小测试用例:
import java.nio.*;
import java.util.*;
import org.junit.*;
public class _CharBufferTests {
public _CharBufferTests() {
}
private static void printBufferInfo(CharBuffer b) {
System.out.println("- - - - - - - - - - - - - - -");
System.out.println("capacity: " + b.capacity());
System.out.println("length: " + b.length());
System.out.println("arrayOffset: " + b.arrayOffset());
System.out.println("limit: " + b.limit());
System.out.println("position: " + b.position());
System.out.println("remaining: " + b.remaining());
System.out.print("content from array: ");
char[] array = b.array();
for (int i = 0; i < array.length; ++i) {
if (array[i] == 0) {
array[i] = '_';
}
}
System.out.println(Arrays.toString(b.array()));
}
@Test
public void testCharBuffer3() {
CharBuffer b = CharBuffer.wrap(new char[12], 2, 10);
printBufferInfo(b);
b.put("abc");
printBufferInfo(b);
b.rewind();
b.put("abcd");
printBufferInfo(b);
}
}
输出:
- - - - - - - - - - - - - - -
capacity: 12
length: 10
arrayOffset: 0
limit: 12
position: 2
remaining: 10
content from array: [_, _, _, _, _, _, _, _, _, _, _, _]
- - - - - - - - - - - - - - -
capacity: 12
length: 7
arrayOffset: 0
limit: 12
position: 5
remaining: 7
content from array: [_, _, a, b, c, _, _, _, _, _, _, _]
- - - - - - - - - - - - - - -
capacity: 12
length: 8
arrayOffset: 0
limit: 12
position: 4
remaining: 8
content from array: [a, b, c, d, c, _, _, _, _, _, _, _]
谢谢!
Please help me understand the following:
I create a CharBuffer
usingCharBuffer.wrapped(new char[12], 2, 10)
(array, offset, length)
so I would expect that the array is accessed with an offset of 2
and total (rest) length of 10
. But arrayOffset()
returns 0
.
What I want to understand (and can't figure out from JavaDoc) is:
when
arrayOffset()
is not0
, andis there a possibility to let
CharBuffer
use an array with "real" offset (so that the array is never accessed before that offset)?
Here is a little test case:
import java.nio.*;
import java.util.*;
import org.junit.*;
public class _CharBufferTests {
public _CharBufferTests() {
}
private static void printBufferInfo(CharBuffer b) {
System.out.println("- - - - - - - - - - - - - - -");
System.out.println("capacity: " + b.capacity());
System.out.println("length: " + b.length());
System.out.println("arrayOffset: " + b.arrayOffset());
System.out.println("limit: " + b.limit());
System.out.println("position: " + b.position());
System.out.println("remaining: " + b.remaining());
System.out.print("content from array: ");
char[] array = b.array();
for (int i = 0; i < array.length; ++i) {
if (array[i] == 0) {
array[i] = '_';
}
}
System.out.println(Arrays.toString(b.array()));
}
@Test
public void testCharBuffer3() {
CharBuffer b = CharBuffer.wrap(new char[12], 2, 10);
printBufferInfo(b);
b.put("abc");
printBufferInfo(b);
b.rewind();
b.put("abcd");
printBufferInfo(b);
}
}
Output:
- - - - - - - - - - - - - - -
capacity: 12
length: 10
arrayOffset: 0
limit: 12
position: 2
remaining: 10
content from array: [_, _, _, _, _, _, _, _, _, _, _, _]
- - - - - - - - - - - - - - -
capacity: 12
length: 7
arrayOffset: 0
limit: 12
position: 5
remaining: 7
content from array: [_, _, a, b, c, _, _, _, _, _, _, _]
- - - - - - - - - - - - - - -
capacity: 12
length: 8
arrayOffset: 0
limit: 12
position: 4
remaining: 8
content from array: [a, b, c, d, c, _, _, _, _, _, _, _]
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
CharBuffer.wrap(char[], int, int)
:检查对
CharBuffer.offset
的写入,看起来 HeapCharBuffer 和 StringCharBuffer 都可以具有非零 arrayOffsets ()。CharBuffer.wrap(char[], int, int)
:Examining writes to
CharBuffer.offset
it looks like HeapCharBuffer and StringCharBuffer can both have non-zero arrayOffsets().我认为您可以使用
position
来实现类似的结果。不要使用rewind
,因为这会将position
设置为0;请改用reset
。You can use the
position
to achieve similar results, I think. Don't userewind
since this sets theposition
to 0; usereset
instead.