KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > msg > IOControl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - 2000 Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): ScalAgent Distributed Technologies
22  */

23 package fr.dyade.aaa.jndi2.msg;
24
25 import java.io.*;
26 import java.net.*;
27
28 public class IOControl {
29   /**
30    * This property allow to enable/disable SO_TIMEOUT with the specified
31    * timeout in milliseconds.
32    */

33   public static final String JavaDoc SOCKET_SOTIMEOUT = "fr.dyade.aaa.jndi2.socketTimeOut";
34
35   /**
36    * Enable/disable SO_TIMEOUT with the specified timeout in milliseconds.
37    * The default value is zero which means the option is disabled.
38    * This value can be adjusted by setting the environment property
39    * <code>fr.dyade.aaa.jndi2.socketTimeOut</code>.
40    * With this option set to a non-zero timeout, a read() call on the
41    * InputStream associated with this Socket will block for only this amount
42    * of time.
43    */

44   private static int socketTimeOut =
45       Integer.getInteger(SOCKET_SOTIMEOUT, 0).intValue();
46
47   private Socket socket;
48
49   private BufferedInputStream bis;
50
51   private NetOutputStream nos;
52
53   public IOControl(Socket socket) throws IOException {
54     this.socket = socket;
55     socket.setTcpNoDelay(true);
56     socket.setSoTimeout(socketTimeOut);
57     socket.setSoLinger(true, 1000);
58     nos = new NetOutputStream(socket);
59     bis = new BufferedInputStream(socket.getInputStream());
60   }
61
62   public Object JavaDoc readObject()
63     throws IOException, ClassNotFoundException JavaDoc {
64     ObjectInputStream ois = new ObjectInputStream(bis);
65     return ois.readObject();
66   }
67
68   public int readInt()
69     throws IOException {
70     DataInputStream dis = new DataInputStream(bis);
71     return dis.readInt();
72   }
73   
74   public void writeObject(Object JavaDoc obj) throws IOException {
75     nos.send(obj);
76   }
77
78   public void writeInt(int i) throws IOException {
79     nos.send(i);
80   }
81
82   public void close() {
83     try {
84       socket.getInputStream().close();
85     } catch (IOException exc) {}
86     try {
87       socket.getOutputStream().close();
88     } catch (IOException exc) {}
89     try {
90       socket.close();
91     } catch (IOException exc) {}
92   }
93
94   public final Socket getSocket() {
95     return socket;
96   }
97
98   static class NetOutputStream {
99     private ByteArrayOutputStream baos = null;
100     private ObjectOutputStream oos = null;
101     private OutputStream os = null;
102
103     static private final byte[] streamHeader = {
104       (byte)((ObjectStreamConstants.STREAM_MAGIC >>> 8) & 0xFF),
105       (byte)((ObjectStreamConstants.STREAM_MAGIC >>> 0) & 0xFF),
106       (byte)((ObjectStreamConstants.STREAM_VERSION >>> 8) & 0xFF),
107       (byte)((ObjectStreamConstants.STREAM_VERSION >>> 0) & 0xFF)
108     };
109
110     NetOutputStream(Socket sock) throws IOException {
111       baos = new ByteArrayOutputStream(1024);
112       oos = new ObjectOutputStream(baos);
113       baos.reset();
114       os = sock.getOutputStream();
115     }
116
117     void send(Object JavaDoc msg) throws IOException {
118       try {
119         baos.write(streamHeader, 0, 4);
120         oos.writeObject(msg);
121         oos.flush();
122
123         baos.writeTo(os);
124         os.flush();
125       } finally {
126         oos.reset();
127         baos.reset();
128       }
129     }
130
131     void send(int i) throws IOException {
132       DataOutputStream daos = new DataOutputStream(os);
133       daos.writeInt(i);
134       daos.flush();
135     }
136   }
137 }
138
Popular Tags