23
23
import java .io .IOException ;
24
24
import java .io .InputStream ;
25
25
import java .io .OutputStream ;
26
- import java .nio .channels .FileChannel ;
27
- import javax .annotation .Nullable ;
28
26
29
27
/** This class implements the FileSystem interface using direct calls to the UNIX filesystem. */
30
28
@ ThreadSafe
@@ -58,49 +56,42 @@ protected InputStream getInputStream(Path path) throws IOException {
58
56
}
59
57
60
58
/** Returns either normal or profiled FileInputStream. */
61
- private InputStream createFileInputStream (Path path ) throws IOException {
59
+ private InputStream createFileInputStream (Path path ) throws FileNotFoundException {
62
60
final String name = path .toString ();
63
61
if (profiler .isActive ()
64
62
&& (profiler .isProfiling (ProfilerTask .VFS_READ )
65
63
|| profiler .isProfiling (ProfilerTask .VFS_OPEN ))) {
66
64
long startTime = Profiler .nanoTimeMaybe ();
67
65
try {
68
66
// Replace default FileInputStream instance with the custom one that does profiling.
69
- return new ProfiledInputStream (name , newFileInputStream ( name ) );
67
+ return new ProfiledFileInputStream (name );
70
68
} finally {
71
69
profiler .logSimpleTask (startTime , ProfilerTask .VFS_OPEN , name );
72
70
}
73
71
} else {
74
72
// Use normal FileInputStream instance if profiler is not enabled.
75
- return newFileInputStream ( name );
73
+ return new FileInputStream ( path . toString () );
76
74
}
77
75
}
78
76
79
- protected InputStream newFileInputStream (String path ) throws IOException {
80
- return new FileInputStream (path );
81
- }
82
-
83
- protected OutputStream newFileOutputStream (String path , boolean append ) throws IOException {
84
- return new FileOutputStream (path , append );
85
- }
86
-
87
77
/**
88
78
* Returns either normal or profiled FileOutputStream. Should be used by subclasses to create
89
79
* default OutputStream instance.
90
80
*/
91
- protected OutputStream createFileOutputStream (Path path , boolean append ) throws IOException {
81
+ protected OutputStream createFileOutputStream (Path path , boolean append )
82
+ throws FileNotFoundException {
92
83
final String name = path .toString ();
93
84
if (profiler .isActive ()
94
85
&& (profiler .isProfiling (ProfilerTask .VFS_WRITE )
95
86
|| profiler .isProfiling (ProfilerTask .VFS_OPEN ))) {
96
87
long startTime = Profiler .nanoTimeMaybe ();
97
88
try {
98
- return new ProfiledFileOutputStream (name , newFileOutputStream ( name , append ) );
89
+ return new ProfiledFileOutputStream (name , append );
99
90
} finally {
100
91
profiler .logSimpleTask (startTime , ProfilerTask .VFS_OPEN , name );
101
92
}
102
93
} else {
103
- return newFileOutputStream (name , append );
94
+ return new FileOutputStream (name , append );
104
95
}
105
96
}
106
97
@@ -119,34 +110,12 @@ protected OutputStream getOutputStream(Path path, boolean append) throws IOExcep
119
110
}
120
111
}
121
112
122
- private static final class ProfiledInputStream extends InputStream implements
123
- FileChannelSupplier {
113
+ private static final class ProfiledFileInputStream extends FileInputStream {
124
114
private final String name ;
125
- private final InputStream stm ;
126
115
127
- public ProfiledInputStream (String name , InputStream stm ) {
116
+ public ProfiledFileInputStream (String name ) throws FileNotFoundException {
117
+ super (name );
128
118
this .name = name ;
129
- this .stm = stm ;
130
- }
131
-
132
- @ Override
133
- public int available () throws IOException {
134
- return stm .available ();
135
- }
136
-
137
- @ Override
138
- public void close () throws IOException {
139
- stm .close ();
140
- }
141
-
142
- @ Override
143
- public void mark (int readlimit ) {
144
- stm .mark (readlimit );
145
- }
146
-
147
- @ Override
148
- public boolean markSupported () {
149
- return stm .markSupported ();
150
119
}
151
120
152
121
@ Override
@@ -155,7 +124,7 @@ public int read() throws IOException {
155
124
try {
156
125
// Note that FileInputStream#read() does *not* call any of our overridden methods,
157
126
// so there's no concern with double counting here.
158
- return stm .read ();
127
+ return super .read ();
159
128
} finally {
160
129
profiler .logSimpleTask (startTime , ProfilerTask .VFS_READ , name );
161
130
}
@@ -170,55 +139,19 @@ public int read(byte[] b) throws IOException {
170
139
public int read (byte [] b , int off , int len ) throws IOException {
171
140
long startTime = Profiler .nanoTimeMaybe ();
172
141
try {
173
- return stm .read (b , off , len );
142
+ return super .read (b , off , len );
174
143
} finally {
175
144
profiler .logSimpleTask (startTime , ProfilerTask .VFS_READ , name );
176
145
}
177
146
}
178
-
179
- @ Override
180
- public void reset () throws IOException {
181
- stm .reset ();
182
- }
183
-
184
- @ Override
185
- public long skip (long n ) throws IOException {
186
- return stm .skip (n );
187
- }
188
-
189
- @ Override
190
- public FileChannel getChannel () {
191
- return stm instanceof FileInputStream
192
- ? ((FileInputStream ) stm ).getChannel ()
193
- : null ;
194
- }
195
147
}
196
148
197
- /**
198
- * Interface to return a {@link FileChannel}.
199
- */
200
- public interface FileChannelSupplier {
201
- @ Nullable
202
- FileChannel getChannel ();
203
- }
204
-
205
- private static final class ProfiledFileOutputStream extends OutputStream {
149
+ private static final class ProfiledFileOutputStream extends FileOutputStream {
206
150
private final String name ;
207
- private final OutputStream stm ;
208
151
209
- public ProfiledFileOutputStream (String name , OutputStream stm ) {
152
+ public ProfiledFileOutputStream (String name , boolean append ) throws FileNotFoundException {
153
+ super (name , append );
210
154
this .name = name ;
211
- this .stm = stm ;
212
- }
213
-
214
- @ Override
215
- public void close () throws IOException {
216
- stm .close ();
217
- }
218
-
219
- @ Override
220
- public void flush () throws IOException {
221
- stm .flush ();
222
155
}
223
156
224
157
@ Override
@@ -230,17 +163,7 @@ public void write(byte[] b) throws IOException {
230
163
public void write (byte [] b , int off , int len ) throws IOException {
231
164
long startTime = Profiler .nanoTimeMaybe ();
232
165
try {
233
- stm .write (b , off , len );
234
- } finally {
235
- profiler .logSimpleTask (startTime , ProfilerTask .VFS_WRITE , name );
236
- }
237
- }
238
-
239
- @ Override
240
- public void write (int b ) throws IOException {
241
- long startTime = Profiler .nanoTimeMaybe ();
242
- try {
243
- stm .write (b );
166
+ super .write (b , off , len );
244
167
} finally {
245
168
profiler .logSimpleTask (startTime , ProfilerTask .VFS_WRITE , name );
246
169
}
0 commit comments