KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > stream > DriverBufferedInputStream


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Nicolas Modrzyk
21  * Contributor(s): Emmanuel Cecchet. Marc Herbert.
22  */

23
24 package org.continuent.sequoia.common.stream;
25
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.StreamCorruptedException JavaDoc;
29 import java.net.Socket JavaDoc;
30
31 import org.continuent.sequoia.driver.SequoiaUrl;
32
33 /**
34  * LongUTFDataInputStream subclass used between the controller and the driver.
35  * <p>
36  * Creates a buffered LongUTFDataInputStream upon a given
37  * {@link java.net.Socket} and adds its creation date (as a <code>long</code>)
38  * for statistics purposes on the socket (eg.
39  * {@link org.continuent.sequoia.controller.virtualdatabase.VirtualDatabaseWorkerThread#retrieveClientData()})
40  * This class is now an implementation detail and references to it should be
41  * replaced by references to the more abstract LongUTFDataInputStream.
42  * Ultimately this class could/should be moved out of the protocol package,
43  * closer to the very few classes that really need to reference it.
44  *
45  * @see org.continuent.sequoia.common.stream.LongUTFDataInputStream
46  * @see org.continuent.sequoia.common.stream.DriverBufferedOutputStream
47  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
48  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
49  * @author <a HREF="mailto:Marc.Herbert@emicnetworks.com">Marc Herbert</a>
50  * @author <a HREF="mailto:Gilles.Rayrat@emicnetworks.com">Gilles Rayrat</a>
51  */

52 public class DriverBufferedInputStream
53     extends LongUTFDataInputStream
54 {
55   private Socket JavaDoc socket;
56   private long dateCreated;
57   private final int debugLevel;
58
59   /**
60    * @see java.lang.Object#finalize()
61    */

62   protected void finalize() throws Throwable JavaDoc
63   {
64     try
65     {
66       /*
67        * This is just an extra safety net: this socket must be ALREADY closed at
68        * this time (check finalize()-related litterature)
69        */

70       if (!socket.isClosed())
71       {
72         if (debugLevel >= SequoiaUrl.DEBUG_LEVEL_DEBUG)
73         {
74           System.err
75               .println("Socket was not closed, either someone forgot to"
76                   + " call Connection.close() on " + socket);
77           System.err.println("or a finally { close(); } block is missing");
78         }
79
80         socket.close();
81       }
82
83     }
84     finally
85     {
86       super.finalize();
87     }
88   }
89
90   /**
91    * Controller has a different logging scheme: debugLevel = OFF
92    *
93    * @see #DriverBufferedInputStream(Socket, int)
94    */

95   public DriverBufferedInputStream(Socket JavaDoc clientSocket) throws IOException JavaDoc,
96       StreamCorruptedException JavaDoc
97   {
98     this(clientSocket, SequoiaUrl.DEBUG_LEVEL_OFF);
99   }
100
101   /**
102    * Creates a buffered Stream on a socket and sets the creation date to the
103    * current system time.
104    *
105    * @param socket socket for this stream
106    * @param debugLevel debug level
107    * @throws IOException if an error occurs
108    * @throws StreamCorruptedException if an error occurs
109    */

110   public DriverBufferedInputStream(Socket JavaDoc socket, int debugLevel)
111       throws IOException JavaDoc, StreamCorruptedException JavaDoc
112   {
113     super(new BufferedInputStream JavaDoc(socket.getInputStream()));
114     this.socket = socket;
115     this.debugLevel = debugLevel;
116     dateCreated = System.currentTimeMillis();
117   }
118
119   /**
120    * @return Returns the socket.
121    */

122   public Socket JavaDoc getSocket()
123   {
124     return socket;
125   }
126
127   /**
128    * @return Returns the creation date.
129    */

130   public long getDateCreated()
131   {
132     return dateCreated;
133   }
134 }
Popular Tags