views:

861

answers:

3

I'm just starting out with Java serialization, and I'm not clear on how you are supposed to get objects from a source in a scenario with non-blocking I/O .

All the documentation I can find suggests using ObjectInputStream is the proper way to read in serialized objects. However, as I mentioned I'm using java.nio and performing non-blocking operations. If readObject() will block until a new object is available, this can't help me

Summary .. How do you do serialization when working with Java NIO?

A: 

You will need to implement (or find an implementation) of an InputStream that reads from a buffer that you can append to from another thread.

It does seem a rather strange thing to try to do. NIO is all about high performance, and serialisation isn't.

Tom Hawtin - tackline
I'm using nio because I found the interface easier to work with when dealing with a large number of streams. Performance isn't a primary concern.
Mike
A: 

This is classic - how can you .read() if there is nothing to read? NIO is clearly performance intensive, dedicated to achieving non-blocking io. Try doing nio on Socket.read() - you still will get either a blocking operation or fall through on timeout ... no amount of fancy will cause data to appear on the 'port' unless you generate some data ...

final SecureRandom dataGenerator = SecureRandom.getInstance("SHA1PRNG");

final Integer range = new Integer( 'z' - 'a' );

for ( big loop ) 
{
   buffer.append( dataGenerator.nextInt ( range.intValue() + (int) 'a' ) );

// ............

do.something( buffer.toString() );

Now your development skills move, even if at a Glacial Pace but they move.

try {
    // Create a read/writeable file channel
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    // Create an output stream on the channel
    OutputStream os = Channels.newOutputStream(channel);

    // Create an inputstream on the channel
    InputStream is = Channels.newInputStream(channel);

    // Close the channel
    is.close();
} catch (IOException e) {
}

Message edit: I grasp your critical response, wait till your twelve thousand lines into CORBA trying to implement readObjectNoData() ( useful for initializing deserialized objects properly despite a "hostile" or incomplete source stream )

My one-off psuedo-code was expected to provide some sort of data stream to decode, reload or whatever - I re-read your post; I think it says how to read() non-blocking io on an object that may not be there ... which drills deeply into known issues of Exception and scheduling, gets dicey trying to work this with someone who asks the question in the manner you worded it, try re-wording and having others explain to me what it is you are asking.

I'm gonna go to the taco stand right at the moment.

Nicholas Jordan
I don't think you read my post. I'm looking for a way of doing serialization on a non-blocking stream.
Mike
+2  A: 

Wrap the serialized instances in a protocol that reports a payload length, and the payload is the instance in question. Then once you know you have a segment that represents a complete instance you can use ObjectInputStream safely knowing it won't block.

Protocol like this First 32 bits: Payload length Payload length bits: Serialized data

Sometimes I amaze even myself.

Mike
Now I know why fortran strings start with the string size then content.http://www.ibiblio.org/pub/languages/fortran/ch2-13.html
whatnick