如何序列化ByteBuffer

8

我希望使用 RMI 将 java.nio.ByteBuffer 通过网络发送,但是 ByteBuffer 不可序列化。我尝试了以下自定义类但没有成功:

public class NetByteBuffer implements java.io.Serializable {

ByteBuffer buffer;

public NetByteBuffer(ByteBuffer buffer) {
    this.buffer = buffer;
}

public ByteBuffer getByteBuffer() {
    return this.buffer;
}

客户端仍然收到一个不可序列化的异常。有什么想法吗?

谢谢

4个回答

7
你无法直接发送ByteBuffer,最好获取byte[]并发送,然后在另一端重建ByteBuffer。当然,这样做会失去它作为缓冲区的优势。

我会失去什么优势?这是针对来自Filechannel.map的PDF文件。 - jtnire
4
如果你没有特别的原因使用ByteBuffer,那么也许直接使用字节数组更为安全可靠。 - Bozho

5
像其他人所说的,ByteBuffer是一个字节缓冲区的包装器,因此如果您需要序列化您的类,则最好将其更改为byte []并在读取/写入数据到此bean的类中使用ByteBuffer。
但是,如果您需要序列化ByteBuffer属性(例如使用Cassandra blobs),则始终可以实现自定义序列化(请检查此网址http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html)。
主要要点如下:
1. 将ByteBuffer标记为瞬态(因此默认情况下不会进行序列化) 2. 实现自己的读/写序列化,其中ByteBuffer -> byte []在序列化时,byte [] -> ByteBuffer在反序列化时。
尝试使用此类,并告诉我是否适用于您:
public class NetByteBuffer implements java.io.Serializable {
    private static final long serialVersionUID = -2831273345165209113L;

    //serializable property
    String anotherProperty;

    // mark as transient so this is not serialized by default
    transient ByteBuffer data;

    public NetByteBuffer(String anotherProperty, ByteBuffer data) {
        this.data = data;
        this.anotherProperty = anotherProperty;
    }

    public ByteBuffer getData() {
        return this.data;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        // write default properties
        out.defaultWriteObject();
        // write buffer capacity and data
        out.writeInt(data.capacity());
        out.write(data.array());

    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        //read default properties
        in.defaultReadObject();

        //read buffer data and wrap with ByteBuffer
        int bufferSize = in.readInt();
        byte[] buffer = new byte[bufferSize];
        in.read(buffer, 0, bufferSize);
        this.data = ByteBuffer.wrap(buffer, 0, bufferSize);
    }

    public String getAnotherProperty() {
        return anotherProperty;
    }

}

1

你可能需要更多地解释为什么要序列化一个字节缓冲区。如果你只是想在网络上发送一堆字节,@Bozho的答案已经涵盖了你的需求。

如果你实际上想要发送一个包括其内容和状态的ByteBuffer,你可能需要重新考虑你的设计,或者至少在这里解释一下,以便其他人可以提供更多的指导。


-1

虽然路途遥远,但你的目标可以实现:

您可以创建一个具有“ByteBuffer”实例变量类型的远程对象,并定义getter和setter远程方法以访问该变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接