forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileDescriptor.java
More file actions
121 lines (97 loc) · 2.99 KB
/
Copy pathFileDescriptor.java
File metadata and controls
121 lines (97 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.dockerjava.transport;
import java.io.Closeable;
import java.io.IOException;
import java.util.function.IntConsumer;
/**
* Provides access to the underlying file system representation of an open file.
*
* @author Phillip Webb
* @see #acquire()
*/
class FileDescriptor {
private final Handle openHandle;
private final Handle closedHandler;
private final IntConsumer closer;
private Status status = Status.OPEN;
private int referenceCount;
FileDescriptor(int handle, IntConsumer closer) {
this.openHandle = new Handle(handle);
this.closedHandler = new Handle(-1);
this.closer = closer;
}
@Override
protected void finalize() throws Throwable {
close();
}
/**
* Acquire an instance of the actual {@link Handle}. The caller must
* {@link Handle#close() close} the resulting handle when done.
* @return the handle
*/
synchronized Handle acquire() {
this.referenceCount++;
return (this.status != Status.OPEN) ? this.closedHandler : this.openHandle;
}
private synchronized void release() {
this.referenceCount--;
if (this.referenceCount == 0 && this.status == Status.CLOSE_PENDING) {
this.closer.accept(this.openHandle.value);
this.status = Status.CLOSED;
}
}
/**
* Close the underlying file when all handles have been released.
*/
synchronized void close() {
if (this.status == Status.OPEN) {
if (this.referenceCount == 0) {
this.closer.accept(this.openHandle.value);
this.status = Status.CLOSED;
} else {
this.status = Status.CLOSE_PENDING;
}
}
}
/**
* The status of the file descriptor.
*/
private enum Status {
OPEN, CLOSE_PENDING, CLOSED
}
/**
* Provides access to the actual file descriptor handle.
*/
final class Handle implements Closeable {
private final int value;
private Handle(int value) {
this.value = value;
}
boolean isClosed() {
return this.value == -1;
}
int intValue() {
return this.value;
}
@Override
public void close() throws IOException {
if (!isClosed()) {
release();
}
}
}
}