KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > etf > StreamConnectionBase


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

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 /**
28  * This an abstract base implementation of the ETF::Connection interface.
29  *
30  *
31  *
32  * @author Nicolas Noffke / Andre Spiegel
33  * @version $Id: StreamConnectionBase.java,v 1.2 2004/08/25 10:55:16 simon.mcqueen Exp $
34  */

35
36 public abstract class StreamConnectionBase
37     extends ConnectionBase
38 {
39    /**
40     * Reads performed on this stream.
41     */

42     protected InputStream in_stream = null;
43     
44     /**
45     * Writes performed on this stream.
46     */

47     protected OutputStream out_stream = null;
48     
49     protected StreamConnectionBase()
50     {
51     }
52     
53     /**
54     * Initialise this instance as a copy of another. Intended for use within subclass
55     * constructors.
56     */

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     /**
65      * read actual messages
66      */

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 JavaDoc();
99                 }
100                 else
101                 {
102                     throw new org.omg.CORBA.TRANSIENT JavaDoc ("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 JavaDoc ("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 JavaDoc( 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     /**
171      * Simply return true if calling a read on this instance would
172      * find data in the connection. Otherwise, the function shall
173      * return false.
174      */

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