Created
December 8, 2011 16:34
-
-
Save komiya-atsushi/1447523 to your computer and use it in GitHub Desktop.
Java の Closeable なオブジェクトを確実・安全にクローズするためのヘルパークラス。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package biz.k11i.io; | |
import java.io.BufferedReader; | |
import java.io.Closeable; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.Stack; | |
/** | |
* Closeable なオブジェクトを確実・安全にクローズするためのヘルパーです。 | |
* | |
* @author komiya | |
*/ | |
public class CloseableHelper { | |
/** Closeable なオブジェクトのスタック */ | |
private Stack<Closeable> objects = new Stack<Closeable>(); | |
/** | |
* Closeable なオブジェクトを追加します。 | |
* | |
* @param obj | |
* @return | |
*/ | |
public <T extends Closeable> T add(T obj) { | |
objects.push(obj); | |
return obj; | |
} | |
/** | |
* このオブジェクトが保有している Closeable オブジェクトすべてに対して、Closeable{@link #clone()} | |
* メソッドを呼び出します。 | |
* | |
* @return 途中で IOException が発生した場合、その例外オブジェクトを返却します。例外が発生しなかった場合は null | |
* を返却します。 | |
*/ | |
public IOException closeAll() { | |
IOException result = null; | |
while (!objects.isEmpty()) { | |
try { | |
Closeable obj = objects.pop(); | |
System.out.format("%s のオブジェクトをクローズします。\n", obj.getClass() | |
.toString()); | |
obj.close(); | |
} catch (IOException e) { | |
if (result != null) { | |
result = e; | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* デモコードのエントリポイント | |
* | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
demoWithCloseableHelper(args); | |
demoWithoutCloseableHelper(args); | |
} | |
/** | |
* CloseableHelper を使うデモコード。 | |
* | |
* @param args | |
*/ | |
public static void demoWithCloseableHelper(String[] args) { | |
CloseableHelper ch = new CloseableHelper(); | |
try { | |
FileInputStream fis = ch.add(new FileInputStream(args[0])); | |
InputStreamReader isr = ch.add(new InputStreamReader(fis, args[1])); | |
BufferedReader br = ch.add(new BufferedReader(isr, Integer | |
.parseInt(args[2]))); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
System.out.println(line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
ch.closeAll(); | |
} | |
} | |
/** | |
* CloseableHelper を使わないデモコード。 | |
* | |
* @param args | |
*/ | |
public static void demoWithoutCloseableHelper(String[] args) { | |
FileInputStream fis = null; | |
InputStreamReader isr = null; | |
BufferedReader br = null; | |
try { | |
fis = new FileInputStream(args[0]); | |
isr = new InputStreamReader(fis, args[1]); | |
br = new BufferedReader(isr, Integer.parseInt(args[2])); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
System.out.println(line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
if (br != null) { | |
try { | |
br.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (isr != null) { | |
try { | |
isr.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
if (fis != null) { | |
try { | |
fis.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment