這是本文件的舊版!


Close the System.in of the Scanner

如果有寫到CLI的tool,使用Scanner物件是很平常的事情,因此應會遇到是否該close Scanner物件的問題。假如有兩個類別都會使用到使用System.in的Scanner,先close的就會讓後面的發生問題。

閱讀幾篇文章後,有以下做法:

  1. 不理它,反正不會造成memory leak。
  2. 使用Decorator樣式,將System.in包裝在不會真的close的InputStream中。
  3. 使用Singleton樣式,大家都用同一個Global Scanner。

Decorator

參考apache common io程式碼:

public class CloseShieldInputStream extends ProxyInputStream {
 
    /**
     * Creates a proxy that shields the given input stream from being
     * closed.
     *
     * @param in underlying input stream
     */
    public CloseShieldInputStream(InputStream in) {
        super(in);
    }
 
    /**
     * Replaces the underlying input stream with a {@link ClosedInputStream}
     * sentinel. The original input stream will remain open, but this proxy
     * will appear closed.
     */
    public void close() {
        in = new ClosedInputStream();
    }
 
}
Client可以這樣使用:
		try( Scanner scanner = new Scanner(new CloseShieldInputStream(System.in))){
			// some operations
		}

Singleton