KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > stream > CJDBCInputStream


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk
22  * Contributor(s): Emmanuel Cecchet. Marc Herbert.
23  */

24
25 package org.objectweb.cjdbc.common.stream;
26
27 import java.io.BufferedInputStream JavaDoc;
28 import java.io.DataInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.StreamCorruptedException JavaDoc;
32 import java.net.Socket JavaDoc;
33
34 /**
35  * DataInputStream wrapper used between the controller and the driver.
36  *
37  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
38  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
39  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
40  */

41 public class CJDBCInputStream
42 {
43   private DataInputStream JavaDoc input;
44   private Socket JavaDoc socket;
45   private long dateCreated;
46
47   /**
48    * Creates a new CJDBCInputStream from the given input stream.
49    *
50    * @param in inputstream to wrap
51    */

52   public CJDBCInputStream(InputStream JavaDoc in)
53   {
54     input = new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(in));
55   }
56
57   /**
58    * Useful constructor for statistics on sockets ..
59    *
60    * @param socket socket for this stream
61    * @throws IOException if an error occurs
62    * @throws StreamCorruptedException if an error occurs
63    */

64   public CJDBCInputStream(Socket JavaDoc socket) throws IOException JavaDoc,
65       StreamCorruptedException JavaDoc
66   {
67     this(socket.getInputStream());
68     this.socket = socket;
69     dateCreated = System.currentTimeMillis();
70   }
71
72   /**
73    * @see java.io.DataInputStream#close()
74    * @throws IOException if an error occurs
75    */

76   public void close() throws IOException JavaDoc
77   {
78     input.close();
79   }
80
81   /**
82    * @see java.io.DataInputStream#readBoolean()
83    * @return a boolean value
84    * @throws IOException if an error occurs
85    */

86   public boolean readBoolean() throws IOException JavaDoc
87   {
88     return input.readBoolean();
89   }
90
91   /**
92    * @see java.io.DataInputStream#readInt()
93    * @return an int value
94    * @throws IOException if an error occurs
95    */

96   public int readInt() throws IOException JavaDoc
97   {
98     return input.readInt();
99   }
100
101   /**
102    * @see java.io.DataInputStream#readLong()
103    * @return a long value
104    * @throws IOException if an error occurs
105    */

106   public long readLong() throws IOException JavaDoc
107   {
108     return input.readLong();
109   }
110
111   /**
112    * @see java.io.DataInputStream#readFloat()
113    * @return a floatvalue
114    * @throws IOException if an error occurs
115    */

116   public double readFloat() throws IOException JavaDoc
117   {
118     return input.readFloat();
119   }
120
121   /**
122    * @see java.io.DataInputStream#readDouble()
123    * @return a doublevalue
124    * @throws IOException if an error occurs
125    */

126   public double readDouble() throws IOException JavaDoc
127   {
128     return input.readDouble();
129   }
130
131   /**
132    * @see java.io.DataInputStream#readFully(byte[])
133    * @param b the byte array to fill up
134    * @throws IOException if an error occurs
135    */

136   public void readFully(byte[] b) throws IOException JavaDoc
137   {
138     input.readFully(b);
139   }
140
141   /**
142    * @see java.io.DataInputStream#readUTF()
143    * @see CJDBCOutputStream#writeUTF(String)
144    * @return a String in UTF format
145    * @throws IOException if an error occurs
146    */

147   public String JavaDoc readUTF() throws IOException JavaDoc
148   {
149     if (!input.readBoolean())
150       return null;
151
152     final int maxSize = CJDBCStream.STRING_CHUNK_SIZE;
153
154     int strlen = input.readInt();
155     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(strlen);
156
157     // idx semantic: chars at idx and after had not yet the opportunity
158
// to be received.
159
for (int idx = 0; idx < strlen; idx += maxSize)
160       sbuf.append(input.readUTF());
161
162     return new String JavaDoc(sbuf);
163   }
164
165   /**
166    * @see java.io.DataInputStream#available()
167    * @return the number of available bytes.
168    * @throws IOException if an error occurs
169    */

170   public int available() throws IOException JavaDoc
171   {
172     return input.available();
173   }
174
175   /**
176    * @return Returns the socket.
177    */

178   public Socket JavaDoc getSocket()
179   {
180     return socket;
181   }
182
183   /**
184    * @return Returns the dateCreated.
185    */

186   public long getDateCreated()
187   {
188     return dateCreated;
189   }
190
191 }
Popular Tags