Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #VFS-834: Updated to avoid prematurely closing file objects. #383

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,11 @@ protected Enumeration<URL> findResources(final String name) throws IOException {
final List<URL> result = new ArrayList<>(2);

for (final FileObject baseFile : resources) {
try (FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF)) {
if (FileObjectUtils.exists(file)) {
result.add(new Resource(name, baseFile, file).getURL());
}
final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
if (FileObjectUtils.exists(file)) {
result.add(new Resource(name, baseFile, file).getURL());
} else {
file.close();
}
}

Expand Down Expand Up @@ -312,10 +313,11 @@ private boolean isSealed(final Resource res) throws FileSystemException {
*/
private Resource loadResource(final String name) throws FileSystemException {
for (final FileObject baseFile : resources) {
try (FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF)) {
if (FileObjectUtils.exists(file)) {
return new Resource(name, baseFile, file);
}
final FileObject file = baseFile.resolveFile(name, NameScope.DESCENDENT_OR_SELF);
if (FileObjectUtils.exists(file)) {
return new Resource(name, baseFile, file);
} else {
file.close();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,24 @@ protected void assertSameContent(final String expected, final FileObject file) t
* are encoded using UTF-8.
*/
protected void assertSameURLContent(final String expected, final URLConnection connection) throws Exception {
try (InputStream in = connection.getInputStream()) {
assertSameURLContent(expected, in, connection);
}
}

/**
* Asserts that the content of a file is the same as expected. Checks the length reported by getContentLength() is
* correct, then reads the content as a byte stream and compares the result with the expected content. Assumes files
* are encoded using UTF-8.
*/
protected void assertSameURLContent(final String expected, final InputStream instr, final URLConnection connection) throws Exception {
// Get file content as a binary stream
final byte[] expectedBin = expected.getBytes(StandardCharsets.UTF_8);

// Check lengths
assertEquals("same content length", expectedBin.length, connection.getContentLength());

// Read content into byte array
final InputStream instr = connection.getInputStream();
final ByteArrayOutputStream outstr;
try {
outstr = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.apache.commons.vfs2.VfsTestUtils.getTestDirectoryFile;

import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -196,11 +197,23 @@ public void testLoadClass() throws Exception {
public void testLoadResource() throws Exception {
final VFSClassLoader loader = createClassLoader();

final URL resource = loader.getResource("read-tests/file1.txt");
final URL resource1 = loader.getResource("read-tests/file1.txt");
assertNotNull(resource1);
final URLConnection urlCon1 = resource1.openConnection();
final InputStream instr1 = urlCon1.getInputStream();

assertNotNull(resource);
final URLConnection urlCon = resource.openConnection();
assertSameURLContent(FILE1_CONTENT, urlCon);
// VFS-834: testing that getting the resource again does not close out the previous input stream.
final URL resource2 = loader.getResource("read-tests/file1.txt");
assertNotNull(resource2);
final URLConnection urlCon2 = resource2.openConnection();

assertSameURLContent(FILE1_CONTENT, instr1, urlCon1);

// For tar files, getting the second input stream will reset the input (see TarFileSystem.resetTarFile())
// hence we need to actually get the input stream after asserting the contents of the first one.
final InputStream instr2 = urlCon2.getInputStream();

assertSameURLContent(FILE1_CONTENT, instr2, urlCon2);
}

/**
Expand Down