回答編集履歴
1
finalize
test
CHANGED
@@ -1 +1,57 @@
|
|
1
1
|
GC(ガベコレ)が発生したときにとじられるやつですね(やってはいけないこと)
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
# 追記
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
参考資料としてほぼ同様の質問の回答
|
12
|
+
|
13
|
+
(今回とは InputStream がちがうので自分でさかのぼってみてください。大体は同じく finalize で閉じている )
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
[stackoverflow.com 引用](https://stackoverflow.com/questions/45456509/is-inline-created-inputstream-closed-automatically-by-gc)
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
/**
|
24
|
+
|
25
|
+
* Ensures that the <code>close</code> method of this file input stream is
|
26
|
+
|
27
|
+
* called when there are no more references to it.
|
28
|
+
|
29
|
+
*
|
30
|
+
|
31
|
+
* @exception IOException if an I/O error occurs.
|
32
|
+
|
33
|
+
* @see java.io.FileInputStream#close()
|
34
|
+
|
35
|
+
*/
|
36
|
+
|
37
|
+
protected void finalize() throws IOException {
|
38
|
+
|
39
|
+
if ((fd != null) && (fd != FileDescriptor.in)) {
|
40
|
+
|
41
|
+
/* if fd is shared, the references in FileDescriptor
|
42
|
+
|
43
|
+
* will ensure that finalizer is only called when
|
44
|
+
|
45
|
+
* safe to do so. All references using the fd have
|
46
|
+
|
47
|
+
* become unreachable. We can call close()
|
48
|
+
|
49
|
+
*/
|
50
|
+
|
51
|
+
close();
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|