KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedOutputStream JavaDoc;
28 import java.io.DataOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.net.Socket JavaDoc;
32
33 /**
34  * DataOutputStream wrapper used between the controller and the driver.
35  *
36  * @see org.objectweb.cjdbc.common.stream.CJDBCStream
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</a>
40  */

41 public class CJDBCOutputStream
42 {
43   private DataOutputStream JavaDoc output;
44   private Socket JavaDoc socket;
45   private long dateCreated;
46
47   /**
48    * Creates a new <code>CJDBCOutputStream</code> object for statistics on
49    * sockets
50    *
51    * @param socket socket to monitor
52    * @throws IOException if an IO error occurs
53    */

54   public CJDBCOutputStream(Socket JavaDoc socket) throws IOException JavaDoc
55   {
56     this(socket.getOutputStream());
57     this.socket = socket;
58   }
59
60   /**
61    * Creates a new CJDBCOutputStream from the given input stream.
62    *
63    * @param out outputstream to wrap
64    */

65   public CJDBCOutputStream(OutputStream JavaDoc out)
66   {
67     output = new DataOutputStream JavaDoc(new BufferedOutputStream JavaDoc(out));
68     dateCreated = System.currentTimeMillis();
69   }
70
71   /**
72    * @see java.io.DataOutputStream#flush()
73    * @throws IOException if an error occurs
74    */

75   public void flush() throws IOException JavaDoc
76   {
77     output.flush();
78   }
79
80   /**
81    * @see java.io.DataOutputStream#close()
82    * @throws IOException if an error occurs
83    */

84   public void close() throws IOException JavaDoc
85   {
86     output.close();
87   }
88
89   /**
90    * We split the String into 21K-long chunks and encode+send them one after the
91    * other. This is because Java's writeUTF() fails when UTFlen is < 64K. UTFlen
92    * max is: 2 + 3 * string.length()
93    * <p>
94    * Java's writeUTF() is fully specified in
95    * {@link java.io.DataOutput#writeUTF(java.lang.String)} and stable since
96    * JDK1.0. Implementation is located in
97    * {@link java.io.DataOutputStream#writeUTF(java.lang.String,
98    * java.io.DataOutput)}
99    * <p>
100    * At first this looks like a kludge but: (1) On one hand it's almost required
101    * anyway to split into chunks (whatever technique we choose) since strings
102    * can be infinitely long and we don't want to allocate tons of memory. (2) On
103    * the other hand it is hard to find out a String serialization which is both
104    * efficient and based upon a <em>standard</em> encoding. Let's briefly look
105    * at the alternatives:
106    * <ul>
107    * <li>We would like to send java chars as is using DataOutput.writeChars()
108    * (since they are just from an UTF-16 subset) but there is no readChars()
109    * method (unknown size problem?);
110    * <li>string.getBytes("UTFXX") is highly portable and flexible but involves
111    * a heavy-weight and overkill encoding framework, whereas the encoding of
112    * 64K-limited writeUTF() is hard-wired.
113    * <li>OutputStreamWriter relies on the same framework and is even more
114    * complex.
115    * <li>Maybe some hope from the JNI side? Something like GetStringChars() but
116    * in Java?
117    * </ul>
118    *
119    * @see java.io.DataOutputStream#writeUTF(java.lang.String)
120    * @param string a String to write in UTF form to the stream
121    * @throws IOException if an error occurs
122    */

123   public void writeUTF(String JavaDoc string) throws IOException JavaDoc
124   {
125     if (null == string)
126     {
127       output.writeBoolean(false);
128       return;
129     }
130
131     output.writeBoolean(true);
132     int idx;
133     final int maxSize = CJDBCStream.STRING_CHUNK_SIZE;
134
135     this.writeInt(string.length());
136
137     // First send all full, maxSize long chunks
138
for (idx = 0; idx + maxSize <= string.length(); idx += maxSize)
139       // substring() does no copy, cool.
140
output.writeUTF(string.substring(idx, idx + maxSize));
141
142     // Send the tail separately because
143
// - string.substring(begin, TOO_LONG) is unfortunately not legal.
144
// - we do not send any empty string, this is useless and would complexify
145
// the receiver.
146
// The tail is in most (short) cases just the string as is.
147

148     if (string.length() > idx)
149       output.writeUTF(string.substring(idx));
150   }
151
152   /**
153    * @see java.io.DataOutputStream#writeInt(int)
154    * @param value an int value to write to the stream
155    * @throws IOException if an error occurs
156    */

157   public void writeInt(int value) throws IOException JavaDoc
158   {
159     output.writeInt(value);
160   }
161
162   /**
163    * @see java.io.DataOutputStream#writeLong(long)
164    * @param value a long value to write to the stream
165    * @throws IOException if an error occurs
166    */

167   public void writeLong(long value) throws IOException JavaDoc
168   {
169     output.writeLong(value);
170   }
171
172   /**
173    * @see java.io.DataOutputStream#writeFloat(float)
174    * @param value a float value to write to the stream
175    * @throws IOException if an error occurs
176    */

177   public void writeFloat(float value) throws IOException JavaDoc
178   {
179     output.writeFloat(value);
180   }
181
182   /**
183    * @see java.io.DataOutputStream#writeDouble(double)
184    * @param value a double value to write to the stream
185    * @throws IOException if an error occurs
186    */

187   public void writeDouble(double value) throws IOException JavaDoc
188   {
189     output.writeDouble(value);
190   }
191
192   /**
193    * @see java.io.DataOutputStream#write(byte[])
194    * @param b an array of bytes to write to the stream
195    * @throws IOException if an error occurs
196    */

197   public void write(byte[] b) throws IOException JavaDoc
198   {
199     this.write(b, 0, b.length);
200   }
201
202   /**
203    * @see java.io.DataOutputStream#write(byte[], int, int)
204    */

205   public void write(byte[] b, int offset, int length) throws IOException JavaDoc
206   {
207     output.write(b, offset, length);
208   }
209
210   /**
211    * @see java.io.DataOutputStream#writeBoolean(boolean)
212    * @param value a boolean value to write to the stream
213    * @throws IOException if an error occurs
214    */

215   public void writeBoolean(boolean value) throws IOException JavaDoc
216   {
217     output.writeBoolean(value);
218   }
219
220   /**
221    * @return Returns the socket.
222    */

223   public Socket JavaDoc getSocket()
224   {
225     return socket;
226   }
227
228   /**
229    * @return Returns the dateCreated.
230    */

231   public long getDateCreated()
232   {
233     return dateCreated;
234   }
235
236 }
237
Popular Tags