00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.stream;
00026
00027 import java.io.BufferedOutputStream;
00028 import java.io.IOException;
00029 import java.io.ObjectOutputStream;
00030 import java.io.OutputStream;
00031 import java.io.StreamCorruptedException;
00032 import java.net.Socket;
00033 import java.util.ArrayList;
00034
00035
00036
00037
00038
00039
00040
00041
00042 public class CJDBCOutputStream
00043 {
00044 private ObjectOutputStream output;
00045 private long bytesWritten;
00046 private Socket socket;
00047 private boolean useCompression = false;
00048 private long speed = -1;
00049 private long dateCreated;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 private static final long MAXIMUM_STRING_LENGTH_IN_WRITEUTF = 0xFFFFL / 3;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 public CJDBCOutputStream(Socket socket) throws IOException,
00072 StreamCorruptedException
00073 {
00074 this(socket.getOutputStream());
00075 this.socket = socket;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 public CJDBCOutputStream(OutputStream out) throws IOException,
00087 StreamCorruptedException
00088 {
00089 output = new ObjectOutputStream(new BufferedOutputStream(out));
00090 dateCreated = System.currentTimeMillis();
00091 bytesWritten = 0;
00092 }
00093
00094
00095
00096
00097
00098 public void flush() throws IOException
00099 {
00100 output.flush();
00101 }
00102
00103
00104
00105
00106
00107 public void close() throws IOException
00108 {
00109 output.close();
00110 }
00111
00112
00113
00114
00115
00116
00117 public void writeObject(Object obj) throws IOException
00118 {
00119 if (useCompression)
00120 {
00121 byte[] bytes = CJDBCStream.compressObject(obj);
00122 bytesWritten += bytes.length + 4;
00123
00124
00125 output.writeInt(bytes.length);
00126 output.write(bytes);
00127 }
00128 else
00129 {
00130 if (obj == null)
00131 {
00132 output.writeInt(CJDBCStream.NULL);
00133 return;
00134 }
00135 else if (obj instanceof String)
00136 {
00137 String str = (String) obj;
00138 if (str.length() < MAXIMUM_STRING_LENGTH_IN_WRITEUTF)
00139 {
00140
00141
00142 output.writeInt(CJDBCStream.STRING_OBJECT);
00143 output.writeUTF(str);
00144 return;
00145 }
00146 }
00147 else if (obj instanceof Integer)
00148 {
00149 output.writeInt(CJDBCStream.INTEGER_OBJECT);
00150 output.writeInt(((Integer) obj).intValue());
00151 return;
00152 }
00153 else if (obj instanceof Long)
00154 {
00155 output.writeInt(CJDBCStream.LONG_OBJECT);
00156 output.writeLong(((Long) obj).longValue());
00157 return;
00158 }
00159 else if (obj instanceof Boolean)
00160 {
00161 output.writeInt(CJDBCStream.BOOLEAN_OBJECT);
00162 output.writeBoolean(((Boolean) obj).booleanValue());
00163 return;
00164 }
00165
00166
00167
00168 output.writeInt(CJDBCStream.OBJECT);
00169
00170
00171
00172
00173 output.writeUnshared(obj);
00174
00175 if (obj instanceof ArrayList)
00176 {
00177
00178 output.flush();
00179 output.reset();
00180 }
00181 }
00182
00183 }
00184
00185
00186
00187
00188
00189
00190 public void writeUTF(String string) throws IOException
00191 {
00192
00193 this.writeObject(string);
00194 if (string != null)
00195 bytesWritten += string.length();
00196 }
00197
00198
00199
00200
00201
00202
00203 public void writeInt(int value) throws IOException
00204 {
00205 bytesWritten += 4;
00206 output.writeInt(value);
00207 }
00208
00209
00210
00211
00212
00213
00214 public void writeLong(long value) throws IOException
00215 {
00216 bytesWritten += 8;
00217 output.writeLong(value);
00218 }
00219
00220
00221
00222
00223
00224
00225 public void writeBoolean(boolean value) throws IOException
00226 {
00227 bytesWritten += 1;
00228 output.writeBoolean(value);
00229 }
00230
00231
00232
00233
00234 public long getBytesWritten()
00235 {
00236 return bytesWritten;
00237 }
00238
00239
00240
00241
00242 public Socket getSocket()
00243 {
00244 return socket;
00245 }
00246
00247
00248
00249
00250 public long getDateCreated()
00251 {
00252 return dateCreated;
00253 }
00254
00255
00256
00257
00258 public boolean getUseCompression()
00259 {
00260 return useCompression;
00261 }
00262
00263
00264
00265
00266 public long getSpeed()
00267 {
00268 return speed;
00269 }
00270 }