Java:CharBuffer“忽略”数组偏移量?

发布于 2024-09-06 14:57:07 字数 2034 浏览 5 评论 0原文

请帮助我理解以下内容:

我使用创建一个 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 using
CharBuffer.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 not 0, and

  • is 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-09-13 14:57:07

CharBuffer.wrap(char[], int, int):

它的支持数组将是给定的数组,并且它的数组偏移量将为零。

检查对 CharBuffer.offset 的写入,看起来 HeapCharBuffer 和 StringCharBuffer 都可以具有非零 arrayOffsets ()。

CharBuffer.wrap(char[], int, int):

Its backing array will be the given array, and its array offset will be zero.

Examining writes to CharBuffer.offset it looks like HeapCharBuffer and StringCharBuffer can both have non-zero arrayOffsets().

眼眸印温柔 2024-09-13 14:57:07

我认为您可以使用 position 来实现类似的结果。不要使用rewind,因为这会将position设置为0;请改用reset

You can use the position to achieve similar results, I think. Don't use rewind since this sets the position to 0; use reset instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文