1 20 package org.jacorb.orb.etf; 21 22 import java.io.*; 23 24 import org.apache.avalon.framework.logger.Logger; 25 import org.apache.avalon.framework.configuration.*; 26 27 35 36 public abstract class StreamConnectionBase 37 extends ConnectionBase 38 { 39 42 protected InputStream in_stream = null; 43 44 47 protected OutputStream out_stream = null; 48 49 protected StreamConnectionBase() 50 { 51 } 52 53 57 protected StreamConnectionBase(StreamConnectionBase other) 58 { 59 super((ConnectionBase)other); 60 this.in_stream = other.in_stream; 61 this.out_stream = other.out_stream; 62 } 63 64 67 68 public void read (org.omg.ETF.BufferHolder data, 69 int offset, 70 int min_length, 71 int max_length, 72 long time_out) 73 { 74 int read = 0; 75 76 while( read < min_length ) 77 { 78 int n = 0; 79 80 try 81 { 82 n = in_stream.read( data.value, 83 offset + read, 84 min_length - read ); 85 86 } 87 catch( InterruptedIOException e ) 88 { 89 int soTimeout = getTimeout(); 90 91 if (soTimeout != 0) 92 { 93 if (logger.isDebugEnabled()) 94 { 95 logger.debug("Socket timeout (timeout period: " + 96 soTimeout + ")" ); 97 } 98 throw new org.omg.CORBA.TIMEOUT (); 99 } 100 else 101 { 102 throw new org.omg.CORBA.TRANSIENT ("Interrupted I/O: " + e); 103 } 104 } 105 catch( IOException se ) 106 { 107 if (logger.isDebugEnabled()) 108 { 109 logger.debug("Transport to " + connection_info + 110 ": stream closed " + se.getMessage() ); 111 } 112 throw to_COMM_FAILURE (se); 113 } 114 115 if( n < 0 ) 116 { 117 if (logger.isDebugEnabled()) 118 { 119 logger.debug("Transport to " + connection_info + 120 ": stream closed on read < 0" ); 121 } 122 throw new org.omg.CORBA.COMM_FAILURE ("read() did not return any data"); 123 } 124 125 read += n; 126 } 127 } 128 129 public void write (boolean is_first, 130 boolean is_last, 131 byte[] data, 132 int offset, 133 int length, 134 long time_out ) 135 { 136 try 137 { 138 out_stream.write( data, offset, length ); 139 if( b_out != null ) 140 { 141 b_out.write( data, offset, length ); 142 } 143 } 144 catch (IOException ex) 145 { 146 throw to_COMM_FAILURE (ex); 147 } 148 149 } 150 151 public void flush() 152 { 153 try 154 { 155 if( b_out != null ) 156 { 157 byte[] b = b_out.toByteArray(); 158 if (logger.isInfoEnabled()) 159 logger.info("sendMessages(): " + new String ( b) ); 160 b_out.reset(); 161 } 162 out_stream.flush(); 163 } 164 catch (IOException ex) 165 { 166 throw to_COMM_FAILURE (ex); 167 } 168 } 169 170 175 public boolean is_data_available() 176 { 177 try 178 { 179 return in_stream.available() > 0; 180 } 181 catch (IOException ex) 182 { 183 throw to_COMM_FAILURE (ex); 184 } 185 } 186 187 188 } 189 190 | Popular Tags |