Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CJDBCInputStream.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): Emmanuel Cecchet.
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  * This class is to provide an ObjectInputStream implementation, but we cannot
00037  * override the readObject method, so we made as a wrapper. The CJDBCInputStream
00038  * in pair with the CJDBCOutputStream offers a five times compression using the
00039  * java Inflater and Deflater class. <code>useCompression</code> can be set to
00040  * on or off anytime.
00041  * 
00042  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00043  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
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    * Creates a new CJDBCInputStream from the given input stream.
00056    * 
00057    * @param in inputstream to wrap
00058    * @throws IOException if <code>new ObjectInputStream()</code> throws
00059    *           anException
00060    * @throws StreamCorruptedException same as above
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    * Useful constructor for statistics on sockets ..
00071    * 
00072    * @param socket socket for this stream
00073    * @throws IOException if an error occurs
00074    * @throws StreamCorruptedException if an error occurs
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    * @return the Object read from the stream
00086    * @throws ClassNotFoundException if an error occurs
00087    * @throws IOException if an error occurs
00088    * @throws OptionalDataException if an error occurs
00089    * @see java.io.ObjectInputStream#readObject()
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       // we read an integer in the pipe for the size of the array.
00102       return CJDBCStream.decompressObject(read);
00103     }
00104     else
00105     {
00106       int objectType = input.readInt();
00107       switch (objectType)
00108       {
00109         case CJDBCStream.OBJECT :
00110           // Use the unshared version to prevent the stream to keep references
00111           // on objects. This prevents objects sent through the channel from
00112           // being garbage collected and results in memory leaks.
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    * @see java.io.ObjectInputStream#close()
00132    * @throws IOException if an error occurs
00133    */
00134   public void close() throws IOException
00135   {
00136     input.close();
00137   }
00138 
00139   /**
00140    * @see java.io.ObjectInputStream#readBoolean()
00141    * @return a boolean value
00142    * @throws IOException if an error occurs
00143    */
00144   public boolean readBoolean() throws IOException
00145   {
00146     bytesRead++;
00147     return input.readBoolean();
00148   }
00149 
00150   /**
00151    * @see java.io.ObjectInputStream#readInt()
00152    * @return an int value
00153    * @throws IOException if an error occurs
00154    */
00155   public int readInt() throws IOException
00156   {
00157     bytesRead += 4;
00158     return input.readInt();
00159   }
00160 
00161   /**
00162    * @see java.io.ObjectInputStream#readLong()
00163    * @return a long value
00164    * @throws IOException if an error occurs
00165    */
00166   public long readLong() throws IOException
00167   {
00168     bytesRead += 8;
00169     return input.readLong();
00170   }
00171 
00172   /**
00173    * @see java.io.ObjectInputStream#readUTF()
00174    * @return a String in UTF format
00175    * @throws IOException if an error occurs
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    * @see java.io.ObjectInputStream#available()
00196    * @return the number of available bytes.
00197    * @throws IOException if an error occurs
00198    */
00199   public int available() throws IOException
00200   {
00201     return input.available();
00202   }
00203 
00204   /**
00205    * @return Returns the bytesRead.
00206    */
00207   public long getBytesRead()
00208   {
00209     return bytesRead;
00210   }
00211 
00212   /**
00213    * @return Returns the socket.
00214    */
00215   public Socket getSocket()
00216   {
00217     return socket;
00218   }
00219 
00220   /**
00221    * @return Returns the useCompression.
00222    */
00223   public boolean getUseCompression()
00224   {
00225     return useCompression;
00226   }
00227 
00228   /**
00229    * @return Returns the dateCreated.
00230    */
00231   public long getDateCreated()
00232   {
00233     return dateCreated;
00234   }
00235 
00236   /**
00237    * @return Returns the speed.
00238    */
00239   public long getSpeed()
00240   {
00241     return speed;
00242   }
00243 }

Generated on Mon Apr 11 22:01:30 2005 for C-JDBC by  doxygen 1.3.9.1