2
我接管其包含以下代码的现有Java项目:我应该在ObjectOutputSream的close()之前调用reset()吗?
class ConnectionHandler extends Thread {
private Socket socket;
public ConnectionHandler(Socket s) {
this.socket = s;
}
private void doSthForRequest(ObjectInputStream in, ObjectOutputStream out) throws Exception {
// Do something and write output to out:
// out.writeObject(someOutput);
}
public void run() {
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
in = new ObjectInputStream(socket.getInputStream());
out = new ObjectOutputStream(socket.getOutputStream());
while (true) {
out.reset();
doSthForRequest(in, out);
}
} catch (Exception ex) {
if (out != null && !socket.isOutputShutdown()) {
try {
out.writeObject(ex);
out.flush();
} catch (Exception ex2) {}
}
} finally {
if (out != null) {
try {
out.reset(); // any reason for this?
} catch (Exception ee) {}
}
if (out != null) {
try {
out.close();
} catch (Exception ee) {}
}
try {
socket.close();
} catch (Exception e) {}
}
socket = null;
}
}
有这一个插座上的服务请求,并产生输出ConnectionHandler线程。而我的问题是:
如果在它之后立即有close()调用,reset()调用仍然有意义吗?
原作者刚刚离开一个行注释// clear outputstream cache
这让我困惑...
感谢您的帮助!
我认为这个代码中可能还有更多问题,请更正我的问题!非常感谢! –