views:

39

answers:

1

Hi everyone, I'm having a bit of problem with an example I'm currently testing. For some reason, the execution blocks when writing at oos.writeObject(new SimpleObject());, despite that fact that the pipe should transfer the data across, even (I'd assume) if it had to do it in smaller operations due to a small pipe size. Anyway, the example succeeds when the pipe size is larger than the object, and fails when the pipe size is smaller than the object. If anyone could shed some light on this, it'd be much appreciated.

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.ByteBuffer;

import org.apache.mina.filter.codec.serialization.ObjectSerializationInputStream;
import org.apache.mina.filter.codec.serialization.ObjectSerializationOutputStream;

public class ObjTest4 {
 public static void main(String[] args) {
  System.out.println("exec1");
  int objectsToSend = 10;
  int objectsRecvd = 0;

  try {
   System.out.println("exec2");

   PipedOutputStream pos = new PipedOutputStream();
   ObjectSerializationOutputStream oos = new ObjectSerializationOutputStream(pos);

   PipedInputStream pis = new PipedInputStream(pos, 500);
   ObjectSerializationInputStream ois = new ObjectSerializationInputStream(pis);

   oos.setMaxObjectSize(2000);
   ois.setMaxObjectSize(2000);

   while (objectsRecvd < objectsToSend) {
    System.out.println("exec3");

    oos.writeObject(new SimpleObject());

    System.out.println("exec3.1");

    oos.flush();

    System.out.println("exec3.2");

    System.out.println("oisavail: " + ois.available());

    Object o = ois.readObject();
    if (o != null) {
     objectsRecvd++;
     System.out.println("o: " + o);
    } else {
     System.out.println("recvd null");
    }
   }
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }
}

and the class for the object being serialised:

package objtest;

import java.io.Serializable;
import java.util.EnumSet;

public class SimpleObject implements Serializable {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 public String moo = "moo";
 public EnumSet<EnumTest> set = EnumSet.of(EnumTest.Test);
 public String moo2 = "moo2";
 public String moo3 = "moo3";
 public String moo4 = "moo4_";

 {
  for (int i = 0; i < 8; i++) {
   moo4 += moo4;
  }
 }

 /**
  * 
  */
 public SimpleObject() {
 // TODO Auto-generated constructor stub
 }

 /**
  * @return the moo
  */
 public String getMoo() {
  return moo;
 }

 /**
  * @param moo the moo to set
  */
 public void setMoo(String moo) {
  this.moo = moo;
 }
}

Cheers,
Chris

A: 

Sorry, I found the problem -- the Java documentation says not to use piped streams from a single thread, as it may deadlock the thread:

Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.

Chris Dennett