From: adam Date: Wed, 28 Feb 2007 06:49:02 +0000 (+0000) Subject: improve exception handling X-Git-Url: http://git.megacz.com/?p=org.ibex.io.git;a=commitdiff_plain;h=5f7d531015927476bffd668f0a28960a60399093 improve exception handling darcs-hash:20070228064902-5007d-441df656d77baa5eb850c8c2d3442f43f7b1b2de.gz --- diff --git a/src/org/ibex/io/Connection.java b/src/org/ibex/io/Connection.java index 5c8c0fd..1ab0abe 100644 --- a/src/org/ibex/io/Connection.java +++ b/src/org/ibex/io/Connection.java @@ -15,13 +15,17 @@ public class Connection extends Stream { public Connection(Socket s, String vhost) { super(s); this.vhost = vhost; this.s = s; } protected Connection(InputStream i, OutputStream o) { super(i, o); vhost = null; s = null; } public Socket getSocket() { return s; } - public void close() { try{if (s!=null) s.close();}catch(Exception e){throw new Stream.StreamException(e);} super.close(); } + public int getLocalPort() { return s.getLocalPort(); } public InetAddress getLocalAddress() { return ((InetSocketAddress)s.getLocalSocketAddress()).getAddress(); } public int getRemotePort() { return s.getPort(); } public InetAddress getRemoteAddress() { return ((InetSocketAddress)s.getRemoteSocketAddress()).getAddress(); } public String getRemoteHostname() { return getRemoteAddress().getHostName(); } public String getVirtualHost() { return vhost; } - public void setTimeout(int ms) { try { s.setSoTimeout(ms); } catch (Exception e){throw new Stream.StreamException(e); }} - public void setTcpNoDelay(boolean delay) { try { s.setTcpNoDelay(delay);}catch(Exception e){throw new Stream.StreamException(e); }} + public void setTimeout(int ms) { try { s.setSoTimeout(ms); } catch(IOException e) { ioe(e); } } + public void setTcpNoDelay(boolean delay) { try { s.setTcpNoDelay(delay); } catch(IOException e) { ioe(e); } } + + /** never throws an exception */ + public void close() { try{if (s!=null) s.close();}catch(Exception e){/*ignore*/} super.close(); } } + diff --git a/src/org/ibex/io/Stream.java b/src/org/ibex/io/Stream.java index 0d6c169..8bfa8bc 100644 --- a/src/org/ibex/io/Stream.java +++ b/src/org/ibex/io/Stream.java @@ -25,7 +25,8 @@ public class Stream { return this; } - public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false")); + //public static boolean loggingEnabled = "true".equals(System.getProperty("ibex.io.stream.logEnabled", "false")); + public static boolean loggingEnabled = true; public void transcribe(Stream out) { transcribe(out, false); } public void transcribe(Stream out, boolean close) { @@ -37,7 +38,7 @@ public class Stream { out.out.write(buf, 0, numread); } if (close) out.close(); - } catch (IOException ioe) { throw new StreamException(ioe); } + } catch (IOException ioe) { ioe(ioe); } } public void transcribe(StringBuffer out) { @@ -48,7 +49,7 @@ public class Stream { if (numread==-1) return; out.append(buf, 0, numread); } - //} catch (IOException ioe) { throw new StreamException(ioe); } + //} catch (IOException ioe) { ioe(ioe); } } public static int countLines(Stream s) { @@ -63,15 +64,19 @@ public class Stream { public Stream(InputStream in, OutputStream out) { this.in = new Stream.In(in); this.out = new Stream.Out(out); } public Stream(String s) { this(new ByteArrayInputStream(s.getBytes())); } public Stream(File f) { - try { this.in = new Stream.In(new FileInputStream(f)); } catch (IOException e) { throw new StreamException(e); } + try { this.in = new Stream.In(new FileInputStream(f)); } catch (IOException e) { ioe(e); throw new Error(); } this.out = null; } public Stream(Socket s) { - try { this.in = new Stream.In(s.getInputStream()); } catch (IOException e) { throw new StreamException(e); } - try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { throw new StreamException(e); } + try { this.in = new Stream.In(s.getInputStream()); } catch (IOException e) { ioe(e); throw new Error(); } + try { this.out = new Stream.Out(s.getOutputStream()); } catch (IOException e) { ioe(e); throw new Error(); } } - private static int ioe(Exception e) { throw new StreamException(e); } + static int ioe(IOException e) { + if (e instanceof SocketException && e.toString().indexOf("Connection reset")!=-1) + throw new Closed(e.getMessage()); + throw new StreamException(e); + } public static class StreamException extends RuntimeException { public StreamException(Exception e) { super(e); } public StreamException(String s) { super(s); } @@ -164,6 +169,7 @@ public class Stream { public void unread(String s) { in.unread(s); } + /** should not throw exceptions */ public void close() { try { if (in!=null) in.close(); } finally { if (out!=null) out.close(); } } public void setNewline(String s) { newLine = s; }