1 14 15 package org.quickserver.net.client; 16 17 import java.io.*; 18 import java.net.*; 19 import java.util.logging.*; 20 21 26 public class BlockingClient implements ClientService { 27 private static Logger logger = Logger.getLogger(BlockingClient.class.getName()); 28 private static String charset = "ISO-8859-1"; 29 30 private String host = "localhost"; 31 private int port = 0; 32 33 private Socket socket; 34 35 private OutputStream out; 36 private BufferedOutputStream b_out; 37 private ObjectOutputStream o_out; 38 39 private InputStream in; 40 private BufferedInputStream b_in; 41 private BufferedReader br; 42 private ObjectInputStream o_in; 43 44 45 public int getMode() { 46 return ClientService.BLOCKING; 47 } 48 49 public void connect(String host, int port) throws IOException { 50 this.host = host; 51 this.port = port; 52 53 logger.fine("Connecting to "+host+":"+port); 54 socket = new Socket(host, port); 55 56 in = socket.getInputStream(); 57 out = socket.getOutputStream(); 58 logger.fine("Connected"); 59 } 60 61 public boolean isConnected() { 62 if(socket==null) return false; 63 return socket.isConnected(); 64 } 65 66 public void close() throws IOException { 67 logger.fine("Closing"); 68 try { 69 if(out!=null) { 70 logger.finest("Closing output streams"); 71 try { 72 out.flush(); 73 } catch(IOException ioe) { 74 logger.finest("Flushing output streams failed: "+ioe); 75 } 76 77 if(socket!=null ) { 78 socket.shutdownOutput(); 79 } 80 if(o_out != null) { 81 o_out.close(); 82 } 83 if(b_out != null) { 84 b_out.close(); 85 } 86 if(out!=null) out.close(); 87 } 88 89 if(in!=null) { 90 logger.finest("Closing input streams"); 91 95 96 if(o_in != null) { 97 o_in.close(); 98 } 99 if(b_in != null) { 100 b_in.close(); 101 } 102 if(br != null) { 103 br.close(); 104 } 105 if(in!=null) in.close(); 106 } 107 } catch(IOException e) { 108 logger.warning("Error in closing streams: "+e); 109 } 110 socket.close(); 111 socket = null; 112 } 113 114 public void sendBinary(byte[] data) throws IOException { 115 logger.fine("Sending bytes: "+data.length); 116 checkBufferedOutputStream(); 117 b_out.write(data); 118 b_out.flush(); 119 } 120 121 public void sendBytes(String data) throws IOException { 122 logger.fine("Sending: "+data); 123 checkBufferedOutputStream(); 124 byte d[] = data.getBytes(charset); 125 b_out.write(d, 0 , d.length); 126 b_out.flush(); 127 } 128 129 public void sendString(String data) throws IOException { 130 logger.fine("Sending: "+data); 131 checkBufferedOutputStream(); 132 byte d[] = data.getBytes(charset); 133 b_out.write(d, 0 , d.length); 134 d = "\r\n".getBytes(charset); 135 b_out.write(d, 0 , d.length); 136 b_out.flush(); 137 } 138 139 public void sendObject(Object data) throws IOException { 140 checkObjectOutputStream(); 141 o_out.writeObject(data); 142 o_out.flush(); 143 } 144 145 public byte[] readBinary() throws IOException { 146 checkBufferedInputStream(); 147 return readInputStream(b_in); 148 } 149 150 public String readBytes() throws IOException { 151 byte data[] = readBinary(); 152 return new String (data, charset); 153 } 154 155 public String readString() throws IOException { 156 checkBufferedReader(); 157 return br.readLine(); 158 } 159 160 public Object readObject() throws IOException, ClassNotFoundException { 161 checkObjectInputStream(); 162 return o_in.readObject(); 163 } 164 165 public Socket getSocket() { 166 return socket; 167 } 168 169 private void checkObjectOutputStream() throws IOException { 170 if(o_out==null) { 171 b_out = null; 172 o_out = new ObjectOutputStream(out); 173 o_out.flush(); 174 } 175 } 176 private void checkBufferedOutputStream() throws IOException { 177 if(b_out==null) { 178 o_out = null; 179 b_out = new BufferedOutputStream(out); 180 } 181 } 182 183 private void checkBufferedInputStream() throws IOException { 184 if(b_in==null) { 185 br = null; 186 o_in = null; 187 b_in = new BufferedInputStream(in); 188 } 189 } 190 private void checkBufferedReader() throws IOException { 191 if(br==null) { 192 b_in = null; 193 o_in = null; 194 br = new BufferedReader(new InputStreamReader(in)); 195 } 196 } 197 private void checkObjectInputStream() throws IOException { 198 if(o_in==null) { 199 b_in = null; 200 br = null; 201 o_in = new ObjectInputStream(in); 202 } 203 } 204 205 protected static byte[] readInputStream(InputStream _in) throws IOException { 206 byte data[] = null; 207 if(_in==null) 208 throw new IOException("InputStream can't be null!"); 209 210 int s = _in.read(); 211 if(s==-1) { 212 return null; } 214 int alength = _in.available(); 215 if(alength > 0) { 216 data = new byte[alength+1]; 217 _in.read(data, 1, alength); 218 } else { 219 data = new byte[1]; 220 } 221 data[0] = (byte)s; 222 return data; 223 } 224 } 225 | Popular Tags |