title: Sending an Object over a Pipe
body: <p>I'm a beginner in Java and maybe I want to do somting tha does't really works in Java, but I hope someone here can help me.  </p>
  
  <p>What i want to do:
  I have two Threads, one Thread creates couple of the same Object and work with it, in the end I want to send these Objects via Pipe to the other Thread, where i want save it in a File.</p>
  
  <p>I know, I could use Vector, so the other Thread could get the Objects from there, but in the next step I want to separate this project in Sever and Client, so I need these Pipes where I can send Objects. I already searched the Internet for the right answer, but i did find anything what i need. Here is a idea what I thought could work but it didn't realy: </p>
  
  <p>MainThread:</p>
  
  <pre><code>public class ControlerThread {
  
  public static void main(String[] args) {
      PipedReader pr = new PipedReader();
      PipedWriter pw = new PipedWriter();
  
  
      PipedOutputStream pos =  null;
      PipedInputStream pis = null;
      ObjectOutputStream oos =null;
      ObjectInputStream ois = null;
      try
      {    
          pw.connect(pr);
          pis =  new PipedInputStream();
          pos = new PipedOutputStream(pis);
          oos = new ObjectOutputStream(pos);
          ois = new ObjectInputStream(pis);
  
      }catch(IOException ioe)
      {
  
      }
          ModelThread mt = new ModelThread(ois,pr);
          ViewThread vt = new ViewThread(oos,pw);
  
          vt.start();
          mt.start();
   }
  }
  </code></pre>
  
  <p>ViewThread where I creat the Object...</p>
  
  <pre><code>public class ViewThread extends Thread{
  PipedWriter pw;
  BufferedWriter bw;
  
  ObjectOutputStream oos;
  public ViewThread(ObjectOutputStream oos,PipedWriter pwr )
  {
      this.oos = oos;
      this.pw = pwr;
      bw = new BufferedWriter(pw);
  
  }
  public void run()
  {
      int iEingabe = 0;
      Menu mu = new Menu();
      do {
  
          iEingabe = 0;
  
          System.out.println("\t  eine neue Person aufnehmen: &gt; 1");
          System.out.println("\t \t   Records auflisten: &gt; 2");
          System.out.println("       Records in eine Datei sichern: &gt; 3");
          System.out.println("       Records aus einer Datei laden: &gt; 4");
          System.out.println("         in-memory Records sortieren: &gt; 5");
          System.out.println("\t\t       Datei löschen: &gt; 6");
          System.out.println("\t      das Programm verlassen: &gt; 7");
  
          System.out.print("\nIhre Eingabe: ");           
          iEingabe = Eingabe.readInt();
  
          switch (iEingabe) {
          case 1: mu.addContact(); break;
  
          case 2: mu.outputMatrix(); break;
  
          case 3: try{
  
                  bw.write("3");
                  bw.write("\n");
                  bw.flush();
  
                  Kontakt k = new Kontakt();
  
                  oos.writeObject(k);
                  oos.flush();
  
              }catch(IOException ioe)
              {
                  ioe.printStackTrace();
              }; break;
  
          case 4:  break;
          case 5: mu.kontakteSortieren();break;
  
          case 6: /*mu.deleteFile()*/; break;
  
          case 7: System.out.println("Programm wird beended!"); break;
          }
      } while (iEingabe != 7);
  }
  }
  </code></pre>
  
  <p>This is later the Thread which should get the Object:</p>
  
  <pre><code>package Thread;
  import java.io.*;
  public class ModelThread  extends Thread{
  
  private PipedReader pr = null;
  private BufferedReader br = null;
  //private String datei = "test.csv";
  //private FileOutputStream fos;
  //private ObjectOutputStream oos = null;
  
  //  private FileInputStream fis;
  private ObjectInputStream ois = null;
  
  public ModelThread(ObjectInputStream ois,PipedReader pr)
  {
      this.pr = pr;
      br = new BufferedReader(pr);
      this.ois = ois;
  }
  
  public void run()
  {
      try
      { 
          if(br.readLine().equals("3"))
          {
              Kontakt k = (Kontakt) ois.readObject();
              System.out.println(k.name);
          }
      }catch(IOException e)
      {
  
      }catch(ClassNotFoundException ce)
      {
      }
  }
  }
  </code></pre>
  
  <p>But if I Do it like above, I get an IOException "Read end dead". I hope everybody understand what I want to do and can help me.</p>
  
  <p>Exception:</p>
  
  <pre><code>java.io.IOException: Read end dead
  at java.io.PipedInputStream.checkStateForReceive(Unknown Source)
  at java.io.PipedInputStream.receive(Unknown Source)
  at java.io.PipedOutputStream.write(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
  at java.io.ObjectOutputStream$BlockDataOutputStream.flush(Unknown Source)
  at java.io.ObjectOutputStream.flush(Unknown Source)
  at Thread.ViewThread.run(ViewThread.java:57)
  </code></pre>
  
  <p>Thanks!!!</p>