1 52 53 package com.go.trove.net; 54 55 import java.io.*; 56 import java.net.*; 57 import java.lang.ref.*; 58 59 67 public class CheckedSocketException extends SocketException { 68 static CheckedSocketException create 69 (Exception cause, Socket source) { 70 if (cause instanceof CheckedSocketException) { 71 return (CheckedSocketException)cause; 72 } 73 else { 74 return new CheckedSocketException(cause, source); 75 } 76 } 77 78 static CheckedSocketException create 79 (Exception cause, Socket source, String message) { 80 if (cause instanceof CheckedSocketException) { 81 return (CheckedSocketException)cause; 82 } 83 else { 84 return new CheckedSocketException(cause, source, message); 85 } 86 } 87 88 static String createMessagePrefix(Socket source) { 89 return 90 "[" + source.getLocalAddress().getHostAddress() + 91 ':' + source.getLocalPort() + 92 ',' + source.getInetAddress().getHostAddress() + 93 ':' + source.getPort() + ']'; 94 } 95 96 private Exception mCause; 97 private String mMessagePrefix; 98 private Reference mSource; 99 100 private CheckedSocketException(Exception cause, Socket source) { 101 this(cause, source, cause.getMessage()); 102 } 103 104 private CheckedSocketException(Exception cause, Socket source, 105 String message) { 106 super(createMessagePrefix(source) + ' ' + message); 107 mCause = cause; 108 mMessagePrefix = createMessagePrefix(source); 109 mSource = new WeakReference(source); 110 } 111 112 public Exception getCause() { 113 return mCause; 114 } 115 116 120 public Socket getSource() { 121 return (Socket)mSource.get(); 122 } 123 124 public void printStackTrace() { 125 printStackTrace(System.err); 126 } 127 128 public void printStackTrace(PrintStream ps) { 129 synchronized (ps) { 130 ps.print(mMessagePrefix); 131 ps.print(": "); 132 mCause.printStackTrace(ps); 133 } 134 } 135 136 public void printStackTrace(PrintWriter pw) { 137 synchronized (pw) { 138 pw.print(mMessagePrefix); 139 pw.print(": "); 140 mCause.printStackTrace(pw); 141 } 142 } 143 } 144 | Popular Tags |