MappedByteBuffer(在 Android Studio 中)构造函数被破坏(超级构造函数被破坏)

MappedByteBuffer(in Android Studio) constructor is broken(super constructor broken)

我有一个字节数组,必须将其转换为 MappedByteBuffer。

但是当我尝试创建 MappedByteBuffer 时,出现错误。

error: cannot find symbol method MappedByteBuffer(int,int,int,int,byte[],int)

MappedByteBuffer.java

package java.nio;

import java.io.FileDescriptor;
import sun.misc.Unsafe;

public abstract class MappedByteBuffer
    extends ByteBuffer
{

   ...

// Android-added: Additional constructor for use by Android's DirectByteBuffer.
    MappedByteBuffer(int mark, int pos, int lim, int cap, byte[] buf, int offset) {
        super(mark, pos, lim, cap, buf, offset);  // <- when I hover mouse here, ByteBuffer() in ByteBuffer cannot be applied to message appears with a red underline.
        this.fd = null;
    }

   ...

}

ByteBuffer.java

package java.nio;
import libcore.io.Memory;
import dalvik.annotation.codegen.CovariantReturnType;

public abstract class ByteBuffer
    extends Buffer
    implements Comparable<ByteBuffer>
{

    // These fields are declared here rather than in Heap-X-Buffer in order to
    // reduce the number of virtual method invocations needed to access these
    // values, which is especially costly when coding small buffers.
    //
    final byte[] hb;                  // Non-null only for heap buffers
    final int offset;
    boolean isReadOnly;                 // Valid only for heap buffers

    // Creates a new buffer with the given mark, position, limit, capacity,
    // backing array, and array offset
    //
    ByteBuffer(int mark, int pos, int lim, int cap,   // package-private
                 byte[] hb, int offset)
    {
        // Android-added: elementSizeShift parameter (log2 of element size).
        super(mark, pos, lim, cap, 0 /* elementSizeShift */);
        this.hb = hb;
        this.offset = offset;
    }

    ...

}

我觉得奇怪的是,当在 MappedByteBuffer.java 中转到 extends ByteBuffer 的定义时,它显示 ByteBuffer.annotated.java,而不是 ByteBuffer.java

ByteBuffer.annotated.java


// -- This file was mechanically generated: Do not edit! -- //


package java.nio;


@SuppressWarnings({"unchecked", "deprecation", "all"})
public abstract class ByteBuffer extends java.nio.Buffer implements java.lang.Comparable<java.nio.ByteBuffer> {

ByteBuffer(int mark, int pos, int lim, int cap) { super(0, 0, 0, 0, 0); throw new RuntimeException("Stub!"); }

我不知道 {classname}.annotated.java 是做什么的,所以这可能不是错误,但我粘贴是因为我觉得它很奇怪。

那么如何从字节数组创建 MappedByteBuffer? 只有 1 个构造函数,但它已损坏。

There is only 1 constructor, but it's broken

那个构造函数不是 public(它是包私有的),所以你不能调用它。

So how can I create MappedByteBuffer from byte array?

你不能,除非先将它写入文件。来自 docs:

A direct byte buffer whose content is a memory-mapped region of a file.

如果您确实需要专门创建一个 MappedByteBuffer 而不仅仅是一个字节数组的 ByteBuffer,您需要将它写入一个文件并使用 FileChannel.map. If you just need a ByteBuffer, you can use ByteBuffer.wrap