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.BufferedInputStream;
00028 import java.io.IOException;
00029 import java.io.InputStream;
00030 import java.io.ObjectInputStream;
00031 import java.io.OptionalDataException;
00032 import java.io.StreamCorruptedException;
00033 import java.net.Socket;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 public class CJDBCInputStream
00046 {
00047 private ObjectInputStream input;
00048 private long bytesRead;
00049 private Socket socket;
00050 private boolean useCompression = false;
00051 private long speed = -1;
00052 private long dateCreated;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 public CJDBCInputStream(InputStream in) throws IOException,
00063 StreamCorruptedException
00064 {
00065 input = new ObjectInputStream(new BufferedInputStream(in));
00066 bytesRead = 0;
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076 public CJDBCInputStream(Socket socket) throws IOException,
00077 StreamCorruptedException
00078 {
00079 this(socket.getInputStream());
00080 this.socket = socket;
00081 dateCreated = System.currentTimeMillis();
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 public Object readObject() throws ClassNotFoundException, IOException,
00092 OptionalDataException
00093 {
00094 if (useCompression)
00095 {
00096 int len = input.readInt();
00097 byte[] read = new byte[len];
00098 input.readFully(read);
00099
00100 bytesRead += len + 4;
00101
00102 return CJDBCStream.decompressObject(read);
00103 }
00104 else
00105 {
00106 int objectType = input.readInt();
00107 switch (objectType)
00108 {
00109 case CJDBCStream.OBJECT :
00110
00111
00112
00113 return input.readUnshared();
00114 case CJDBCStream.NULL :
00115 return null;
00116 case CJDBCStream.STRING_OBJECT :
00117 return input.readUTF();
00118 case CJDBCStream.INTEGER_OBJECT :
00119 return new Integer(input.readInt());
00120 case CJDBCStream.LONG_OBJECT :
00121 return new Long(input.readLong());
00122 case CJDBCStream.BOOLEAN_OBJECT :
00123 return input.readBoolean() ? Boolean.TRUE : Boolean.FALSE;
00124 default :
00125 throw new RuntimeException("Unhandled type on stream: " + objectType);
00126 }
00127 }
00128 }
00129
00130
00131
00132
00133
00134 public void close() throws IOException
00135 {
00136 input.close();
00137 }
00138
00139
00140
00141
00142
00143
00144 public boolean readBoolean() throws IOException
00145 {
00146 bytesRead++;
00147 return input.readBoolean();
00148 }
00149
00150
00151
00152
00153
00154
00155 public int readInt() throws IOException
00156 {
00157 bytesRead += 4;
00158 return input.readInt();
00159 }
00160
00161
00162
00163
00164
00165
00166 public long readLong() throws IOException
00167 {
00168 bytesRead += 8;
00169 return input.readLong();
00170 }
00171
00172
00173
00174
00175
00176
00177 public String readUTF() throws IOException
00178 {
00179 Object read;
00180 try
00181 {
00182 read = this.readObject();
00183 }
00184 catch (Exception e)
00185 {
00186 throw new IOException(e.getMessage());
00187 }
00188 if (read == null)
00189 return null;
00190 bytesRead += ((String) read).length();
00191 return (String) read;
00192 }
00193
00194
00195
00196
00197
00198
00199 public int available() throws IOException
00200 {
00201 return input.available();
00202 }
00203
00204
00205
00206
00207 public long getBytesRead()
00208 {
00209 return bytesRead;
00210 }
00211
00212
00213
00214
00215 public Socket getSocket()
00216 {
00217 return socket;
00218 }
00219
00220
00221
00222
00223 public boolean getUseCompression()
00224 {
00225 return useCompression;
00226 }
00227
00228
00229
00230
00231 public long getDateCreated()
00232 {
00233 return dateCreated;
00234 }
00235
00236
00237
00238
00239 public long getSpeed()
00240 {
00241 return speed;
00242 }
00243 }