Skip to content

Commit 1a99272

Browse files
committed
Refactor to always calling FileContent.getOutputStream
... instead of `AbstractFileObject.getOutputStream`. This allows us to not call `endOutput` explicitly from anywhere other than `DefaultFileContent`
1 parent 4177ecd commit 1a99272

3 files changed

Lines changed: 13 additions & 28 deletions

File tree

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/AbstractFileObject.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,9 @@ public void createFile() throws FileSystemException {
321321
}
322322

323323
if (!exists()) {
324-
getOutputStream().close();
325-
endOutput();
324+
try (FileContent content = getContent()) {
325+
content.getOutputStream().close();
326+
}
326327
}
327328
} catch (final RuntimeException re) {
328329
throw re;

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileOutputStream.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.io.IOException;
2020
import java.io.OutputStream;
2121

22-
import org.apache.commons.vfs2.FileSystemException;
23-
2422
/**
2523
* OutputStream to a RamFile.
2624
*/
@@ -92,13 +90,7 @@ public void close() throws IOException {
9290
if (exception != null) {
9391
throw exception;
9492
}
95-
try {
96-
this.closed = true;
97-
// Close the
98-
this.file.endOutput();
99-
} catch (final Exception e) {
100-
throw new FileSystemException(e);
101-
}
93+
this.closed = true;
10294
}
10395

10496
}

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ram/RamFileSystem.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import java.util.Collection;
2626
import java.util.Collections;
2727
import java.util.HashMap;
28-
import java.util.Iterator;
2928
import java.util.Map;
3029

3130
import org.apache.commons.vfs2.Capability;
31+
import org.apache.commons.vfs2.FileContent;
3232
import org.apache.commons.vfs2.FileName;
3333
import org.apache.commons.vfs2.FileObject;
3434
import org.apache.commons.vfs2.FileSystemException;
@@ -218,22 +218,14 @@ void toRamFileObject(final FileObject fo, final FileObject root) throws FileSyst
218218
}
219219
} else if (fo.isFile()) {
220220
// Read bytes
221-
try {
222-
final InputStream is = fo.getContent().getInputStream();
223-
try {
224-
final OutputStream os = new BufferedOutputStream(memFo.getOutputStream(), BUFFER_SIZE);
225-
int i;
226-
while ((i = is.read()) != -1) {
227-
os.write(i);
228-
}
229-
os.close();
230-
} finally {
231-
try {
232-
is.close();
233-
} catch (final IOException ignored) {
234-
/* ignore on close exception. */
235-
}
236-
// TODO: close os
221+
try (
222+
InputStream is = fo.getContent().getInputStream();
223+
FileContent content = memFo.getContent();
224+
OutputStream os = new BufferedOutputStream(content.getOutputStream(), BUFFER_SIZE)) {
225+
226+
int i;
227+
while ((i = is.read()) != -1) {
228+
os.write(i);
237229
}
238230
} catch (final IOException e) {
239231
throw new FileSystemException(e.getClass().getName() + " " + e.getMessage());

0 commit comments

Comments
 (0)