1 24 25 package com.mckoi.database.jdbc; 26 27 import java.sql.SQLException ; 28 import java.io.*; 29 30 36 37 class StreamDatabaseInterface extends RemoteDatabaseInterface { 38 39 42 protected DataOutputStream out; 43 44 47 protected DataInputStream in; 48 49 private boolean closed = false; 50 51 52 59 62 void setup(InputStream rawin, OutputStream rawout) throws IOException { 63 if (rawin == null || rawout == null) { 66 throw new IOException("rawin or rawin is null"); 67 } 68 in = new DataInputStream(new BufferedInputStream(rawin, 32768)); 70 out = new DataOutputStream(new BufferedOutputStream(rawout, 32768)); 71 } 72 73 74 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 89 byte[] nextCommandFromServer(int timeout) throws IOException { 90 if (closed) { 91 throw new IOException("DatabaseInterface is closed!"); 92 } 93 try { 94 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 e) { 102 System.out.println("Throwable generated at: " + this); 103 throw e; 104 } 105 } 106 107 void closeConnection() throws IOException { 108 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 |