views:

49

answers:

3

Hi everyone,

I wish to send a java.nio.ByteBuffer accross a network using RMI, however ByteBuffer isn't serializable. I've tried the following custom class to no avail:

public class NetByteBuffer implements java.io.Serializable {

ByteBuffer buffer;

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

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

}

The client still gets a non-serialzable exception. Any ideas?

Thanks

+4  A: 

You can't. You'd better obtain the byte[] and send it instead and reconstruct the ByteBuffer on the other side. You are of course losing the advantages of it being a buffer.

Bozho
What advantages would I be loosing? This is for a PDF file from Filechannel.map
jtnire
If you are using `ByteBuffer` without a specific reason, then perhaps you are safe using the byte array ;)
Bozho
A: 

You probably need to say more about why you're serializing a byte buffer. If you're simply trying to send a bunch of bytes across the network, @Bozho's answer has got you covered.

If you actually want to send across a ByteBuffer including its contents and state, you probably need to rethink your design, or at the very least explain it here so others can provide more direction.

andersoj
A: 

Long way but your goal can be accomplish:

u can create a remote object with instance variable type of "ByteBuffer" and defined the getter and setter remote methods to access that variable.

Static Void Main