他の人が言ったように、ByteBuffer はバイトのバッファーのラップであるため、クラスをシリアル化する必要がある場合は、byte[] に変更し、この Bean にデータを読み書きするクラスで ByteBuffer を使用することをお勧めします。
ただし、ByteBuffer プロパティをシリアル化する必要がある場合 (たとえば、Cassandra blob を使用する場合) は、いつでもカスタムのシリアル化を実装できます (この URL http://www.oracle.com/technetwork/articles/java/javaserial-1536170.htmlを確認してください)。
主なポイントは次のとおりです。
- ByteBuffer を一時的なものとしてマークします (したがって、デフォルトではシリアル化されません)
- シリアライゼーション用に独自の読み取り/書き込みを実装します。シリアライズの場合は 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;
}
}