KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > jdbc > StreamDatabaseInterface


1 /**
2  * com.mckoi.database.jdbc.StreamDatabaseInterface 16 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.jdbc;
26
27 import java.sql.SQLException JavaDoc;
28 import java.io.*;
29
30 /**
31  * An stream implementation of an interface to a McKoi database. This
32  * is a stream based communication protocol.
33  *
34  * @author Tobias Downer
35  */

36
37 class StreamDatabaseInterface extends RemoteDatabaseInterface {
38
39   /**
40    * The data output stream for the db protocol.
41    */

42   protected DataOutputStream out;
43
44   /**
45    * The data input stream for the db protocol.
46    */

47   protected DataInputStream in;
48
49   private boolean closed = false;
50
51
52 // /**
53
// * Constructor.
54
// */
55
// StreamDatabaseInterface(String db_name) {
56
// super(db_name);
57
// }
58

59   /**
60    * Sets up the stream connection with the given input/output stream.
61    */

62   void setup(InputStream rawin, OutputStream rawout) throws IOException {
63 // System.out.println("rawin: " + rawin);
64
// System.out.println("rawout: " + rawout);
65
if (rawin == null || rawout == null) {
66       throw new IOException("rawin or rawin is null");
67     }
68     // Get the input and output and wrap around Data streams.
69
in = new DataInputStream(new BufferedInputStream(rawin, 32768));
70     out = new DataOutputStream(new BufferedOutputStream(rawout, 32768));
71   }
72
73
74   /**
75    * Writes the given command to the server. The stream protocol flushes the
76    * byte array onto the stream.
77    */

78   void writeCommandToServer(byte[] command, int offset, int size)
79                                                           throws IOException {
80     out.writeInt(size);
81     out.write(command, 0, size);
82     out.flush();
83   }
84
85   /**
86    * Blocks until the next command is received from the server. The stream
87    * protocol waits until we receive something from the server.
88    */

89   byte[] nextCommandFromServer(int timeout) throws IOException {
90     if (closed) {
91       throw new IOException("DatabaseInterface is closed!");
92     }
93     try {
94 // System.out.println("I'm waiting for a command: " + this);
95
// new Error().printStackTrace();
96
int command_length = in.readInt();
97       byte[] buf = new byte[command_length];
98       in.readFully(buf, 0, command_length);
99       return buf;
100     }
101     catch (NullPointerException JavaDoc e) {
102       System.out.println("Throwable generated at: " + this);
103       throw e;
104     }
105   }
106
107   void closeConnection() throws IOException {
108 // System.out.println("Closed: " + this);
109
closed = true;
110     try {
111       out.close();
112     }
113     catch (IOException e) {
114       in.close();
115       throw e;
116     }
117     in.close();
118   }
119
120 }
121
Popular Tags