00001
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
00044 public class CJDBCInputStream
00045 {
00046 private ObjectInputStream
input;
00047 private long bytesRead;
00048 private Socket
socket;
00049 private boolean useCompression =
false;
00050 private long speed = -1;
00051 private long dateCreated;
00052
00061 public CJDBCInputStream(InputStream in)
throws IOException,
00062 StreamCorruptedException
00063 {
00064
input =
new ObjectInputStream(
new BufferedInputStream(in));
00065
bytesRead = 0;
00066 }
00067
00075 public CJDBCInputStream(Socket socket)
throws IOException,
00076 StreamCorruptedException
00077 {
00078
this(
socket.getInputStream());
00079
this.socket =
socket;
00080
dateCreated = System.currentTimeMillis();
00081 }
00082
00090 public Object
readObject() throws ClassNotFoundException, IOException,
00091 OptionalDataException
00092 {
00093
if (
useCompression)
00094 {
00095
int len =
input.readInt();
00096 byte[] read =
new byte[len];
00097
input.readFully(read);
00098
00099
bytesRead += len + 4;
00100
00101
return CJDBCStream.decompressObject(read);
00102 }
00103
else
00104 {
00105
00106
00107
00108
return input.readUnshared();
00109 }
00110 }
00111
00116 public void close() throws IOException
00117 {
00118
input.close();
00119 }
00120
00126 public boolean readBoolean() throws IOException
00127 {
00128
bytesRead++;
00129
return input.readBoolean();
00130 }
00131
00137 public int readInt() throws IOException
00138 {
00139
bytesRead += 4;
00140
return input.readInt();
00141 }
00142
00148 public long readLong() throws IOException
00149 {
00150
bytesRead += 8;
00151
return input.readLong();
00152 }
00153
00159 public String
readUTF() throws IOException
00160 {
00161
try
00162 {
00163 Object read =
this.readObject();
00164
if (read == null)
00165
return null;
00166
bytesRead += ((String) read).length();
00167
return (String) read;
00168 }
00169
catch (ClassNotFoundException e)
00170 {
00171
00172
00173
throw new IOException(e.getMessage());
00174 }
00175 }
00176
00182 public int available() throws IOException
00183 {
00184
return input.available();
00185 }
00186
00190 public long getBytesRead()
00191 {
00192
return bytesRead;
00193 }
00194
00198 public Socket
getSocket()
00199 {
00200
return socket;
00201 }
00202
00206 public boolean getUseCompression()
00207 {
00208
return useCompression;
00209 }
00210
00214 public long getDateCreated()
00215 {
00216
return dateCreated;
00217 }
00218
00222 public long getSpeed()
00223 {
00224
return speed;
00225 }
00226 }