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

Improve resource handling for empty files contained in jars #28850

Closed
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 @@ -20,13 +20,15 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.NoSuchFileException;
import java.nio.file.StandardOpenOption;
import java.util.jar.JarEntry;

import org.springframework.util.ResourceUtils;

Expand Down Expand Up @@ -114,6 +116,16 @@ boolean checkReadable(URL url) {
return false;
}
}
else if (con instanceof JarURLConnection) {
JarURLConnection jarCon = (JarURLConnection) con;
JarEntry jarEntry = jarCon.getJarEntry();
if (jarEntry == null) {
return false;
}
else {
return !jarEntry.isDirectory();
}
}
long contentLength = con.getContentLengthLong();
if (contentLength > 0) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@

package org.springframework.core.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
Expand Down Expand Up @@ -119,6 +127,34 @@ void directoryNotReadable() {
assertThat(jarDir.isReadable()).isFalse();
}

@Test
void emptyFileReadable(@TempDir File tempDir) throws IOException {
File file = new File(tempDir, "empty.txt");
assertThat(file.createNewFile()).isTrue();
assertThat(file.isFile());

ClassLoader fileClassLoader = new URLClassLoader(new URL[]{tempDir.toURI().toURL()});

Resource emptyFile = new ClassPathResource("empty.txt", fileClassLoader);
assertThat(emptyFile.exists()).isTrue();
assertThat(emptyFile.isReadable()).isTrue();
assertThat(emptyFile.contentLength()).isEqualTo(0);

File jarFile = new File(tempDir, "test.jar");
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(jarFile))) {
zipOut.putNextEntry(new ZipEntry("empty2.txt"));
zipOut.closeEntry();
}
assertThat(jarFile.isFile());

ClassLoader jarClassLoader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()});

Resource emptyJarEntry = new ClassPathResource("empty2.txt", jarClassLoader);
assertThat(emptyJarEntry.exists()).isTrue();
assertThat(emptyJarEntry.isReadable()).isTrue();
assertThat(emptyJarEntry.contentLength()).isEqualTo(0);
}


private void assertDescriptionContainsExpectedPath(ClassPathResource resource, String expectedPath) {
Matcher matcher = DESCRIPTION_PATTERN.matcher(resource.getDescription());
Expand Down