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
這讓我困惑...
感謝您的幫助!
我認爲這個代碼中可能還有更多問題,請更正我的問題!非常感謝! –