📄 socketwithlayeredtransport.java
字号:
package com.maverick.http;
import java.net.*;
import java.io.*;
import java.lang.reflect.Method;
import java.lang.reflect.*;
public class SocketWithLayeredTransport
extends Socket {
LayeredTransport head = null;
/* DEBUG */org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(SocketWithLayeredTransport.class);
public SocketWithLayeredTransport(String hostname, int port)
throws UnknownHostException,
IOException {
super(hostname, port);
}
public synchronized void pushTransport(Object obj) throws IOException {
if(head==null)
head = new LayeredTransport(obj, this);
else
head = new LayeredTransport(obj, head);
}
public synchronized InputStream getInputStream() throws IOException {
if(head==null)
return super.getInputStream();
else
return head.getInputStream();
}
public synchronized OutputStream getOutputStream() throws IOException {
if(head==null)
return super.getOutputStream();
else
return head.getOutputStream();
}
public void close() throws IOException {
if (head!=null)
head.close();
try {
super.close();
} catch(Throwable t) { }
}
public static String getExceptionMessageChain(Throwable t) {
StringBuffer buf = new StringBuffer();
while(t != null) {
if(buf.length() > 0 && !buf.toString().endsWith(".")) {
buf.append(". ");
}
if(t.getMessage() != null) {
buf.append(t.getMessage().trim());
}
try {
Method m = t.getClass().getMethod("getCause", (Class[])null);
t = (Throwable) m.invoke(t, (Object[])null);
}
catch(Throwable ex) {
}
}
return buf.toString();
}
class LayeredTransport {
Object transport;
Object source;
InputStream in, rawIn;
OutputStream out, rawOut;
Method close;
Method rawClose;
LayeredTransport(Object transport, Object source) throws IOException {
try {
// Get the source objects
Method m = source.getClass().getMethod("getInputStream", (Class[])null);
rawIn = (InputStream) m.invoke(source, (Object[])null);
m = source.getClass().getMethod("getOutputStream", (Class[])null);
rawOut = (OutputStream) m.invoke(source, (Object[])null);
rawClose = source.getClass().getMethod("close", (Class[])null);
// Get the transport objects
m = transport.getClass().getMethod("initialize", new Class[] { InputStream.class, OutputStream.class});
m.invoke(transport, new Object[] { rawIn, rawOut});
m = transport.getClass().getMethod("getInputStream", (Class[])null);
in = (InputStream) m.invoke(transport, (Object[])null);
m = transport.getClass().getMethod("getOutputStream", (Class[])null);
out = (OutputStream) m.invoke(transport, (Object[])null);
close = transport.getClass().getMethod("close", (Class[])null);
// Everything ok
this.source = source;
this.transport = transport;
}
catch(InvocationTargetException ite) {
Throwable t = ite.getTargetException();
if(t != null && t instanceof IOException) {
throw (IOException)t;
}
else {
throw new IOException("Failed to layer transport: " + ( t == null ? ite.getMessage() : getExceptionMessageChain(t)));
}
}
catch (Throwable ex) {
/* DEBUG */log.info("Failed to create layered socket", ex);
throw new IOException("Failed to layer transport: " + ex.getMessage());
}
}
public InputStream getInputStream() throws IOException {
return in;
}
public OutputStream getOutputStream() throws IOException {
return out;
}
public void close() throws IOException {
boolean isThrowing = false;
try {
close.invoke(transport, (Object[])null);
}
catch(InvocationTargetException ex) {
///* DEBUG */log.info("Invoked transport close method threw exception", ex.getTargetException());
}
catch (Throwable ex) {
/* DEBUG */log.info("Failed to invoke transport close", ex);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -