KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

93   public DriverBufferedOutputStream(Socket JavaDoc clientSocket) throws IOException JavaDoc
94   {
95     this(clientSocket, SequoiaUrl.DEBUG_LEVEL_OFF);
96   }
97
98   /**
99    * Creates a new <code>DriverBufferedOutputStream</code> on a socket and
100    * sets the creation date to the current system time.
101    *
102    * @param socket socket to monitor
103    * @param debugLevel debug level
104    * @throws IOException if an IO error occurs
105    */

106   public DriverBufferedOutputStream(Socket JavaDoc socket, int debugLevel)
107       throws IOException JavaDoc
108   {
109     super(new BufferedOutputStream JavaDoc((socket.getOutputStream())));
110     this.socket = socket;
111     this.debugLevel = debugLevel;
112     dateCreated = System.currentTimeMillis();
113   }
114
115   /**
116    * @return Returns the socket.
117    */

118   public Socket JavaDoc getSocket()
119   {
120     return socket;
121   }
122
123   /**
124    * @return Returns the creation date.
125    */

126   public long getDateCreated()
127   {
128     return dateCreated;
129   }
130 }
131
Popular Tags