1 2 package EDU.oswego.cs.dl.util.concurrent.misc; 3 import EDU.oswego.cs.dl.util.concurrent.*; 4 5 import java.io.*; 6 7 25 26 public class PipedChannel extends SemaphoreControlledChannel { 27 protected ObjectInputStream in_; 28 protected ObjectOutputStream out_; 29 30 protected final PipedOutputStream outp_; 31 protected final PipedInputStream inp_; 32 33 34 public PipedChannel() { 35 super(1); 36 37 try { 38 outp_ = new PipedOutputStream(); 39 inp_ = new PipedInputStream(); 40 inp_.connect(outp_); 41 } 42 catch (IOException ex) { 43 ex.printStackTrace(); 44 throw new Error ("Cannot construct Pipe?"); 45 } 46 } 47 48 49 53 54 protected synchronized ObjectInputStream in() { 55 try { 56 if (in_ == null) in_ = new ObjectInputStream(inp_); 57 return in_; 58 } 59 catch (IOException ex) { 60 ex.printStackTrace(); 61 throw new Error ("IO exception during open"); 62 } 63 } 64 65 69 protected synchronized ObjectOutputStream out() { 70 try { 71 if (out_ == null) out_ = new ObjectOutputStream(outp_); 72 return out_; 73 } 74 catch (IOException ex) { 75 ex.printStackTrace(); 76 throw new Error ("IO exception during open"); 77 } 78 } 79 80 81 82 protected void insert(Object x) { 83 try { 84 out().writeObject(x); 85 } 86 catch (InterruptedIOException ex) { 87 Thread.currentThread().interrupt(); 88 } 89 catch (IOException ex) { 90 ex.printStackTrace(); 91 throw new Error ("IO exception during put"); 92 } 93 } 94 95 96 protected Object extract() { 97 try { 98 return in().readObject(); 99 } 100 catch (InterruptedIOException ex) { 101 Thread.currentThread().interrupt(); 102 return null; 103 } 104 catch (IOException ex) { 105 ex.printStackTrace(); 106 throw new Error ("IO exception during take"); 107 } 108 catch (ClassNotFoundException ex) { 109 ex.printStackTrace(); 110 throw new Error ("Serialization exception during take"); 111 } 112 } 113 114 115 public Object peek() { return null; } 116 } 117 118 | Popular Tags |