【问题标题】:MappedByteBuffer(in Android Studio) constructor is broken(super constructor broken)MappedByteBuffer(在 Android Studio 中)构造函数损坏(超级构造函数损坏)
【发布时间】:2020-02-26 02:50:38
【问题描述】:

我有一个字节数组,它必须转换为 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个,但是已经坏了。

【问题讨论】:

    标签: java android android-studio tensorflow-lite mappedbytebuffer


    【解决方案1】:

    只有1个构造函数,但是已经坏了

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

    那么如何从字节数组创建 MappedByteBuffer 呢?

    您不能,除非先将其写入文件。来自docs

    直接字节缓冲区,其内容是文件的内存映射区域。

    如果您确实需要专门从字节数组创建MappedByteBuffer 而不仅仅是ByteBuffer,则需要将其写入文件并使用FileChannel.map。如果只需要ByteBuffer,可以使用ByteBuffer.wrap

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多