8345249: Apply some conservative cleanups in FileURLConnection

Reviewed-by: jpai, djelinski
This commit is contained in:
Eirik Bjørsnøs 2025-01-24 13:00:28 +00:00
parent c5a69b620a
commit 9768f60a94

View file

@ -46,17 +46,15 @@ public class FileURLConnection extends URLConnection {
private static final String TEXT_PLAIN = "text/plain"; private static final String TEXT_PLAIN = "text/plain";
private static final String LAST_MODIFIED = "last-modified"; private static final String LAST_MODIFIED = "last-modified";
String contentType; private final File file;
InputStream is; private InputStream is;
private List<String> directoryListing;
File file; private boolean isDirectory = false;
String filename; private boolean exists = false;
boolean isDirectory = false;
boolean exists = false;
List<String> files;
long length = -1; private long length = -1;
long lastModified = 0; private long lastModified = 0;
protected FileURLConnection(URL u, File file) { protected FileURLConnection(URL u, File file) {
super(u); super(u);
@ -71,20 +69,17 @@ public class FileURLConnection extends URLConnection {
*/ */
public void connect() throws IOException { public void connect() throws IOException {
if (!connected) { if (!connected) {
try {
filename = file.toString(); isDirectory = file.isDirectory();
isDirectory = file.isDirectory(); if (isDirectory) {
if (isDirectory) { String[] fileList = file.list();
String[] fileList = file.list(); if (fileList == null)
if (fileList == null) throw new FileNotFoundException(file.getPath() + " exists, but is not accessible");
throw new FileNotFoundException(filename + " exists, but is not accessible"); directoryListing = Arrays.asList(fileList);
files = Arrays.<String>asList(fileList); } else {
} else { is = new BufferedInputStream(new FileInputStream(file.getPath()));
is = new BufferedInputStream(new FileInputStream(filename));
}
} catch (IOException e) {
throw e;
} }
connected = true; connected = true;
} }
} }
@ -109,11 +104,11 @@ public class FileURLConnection extends URLConnection {
if (!isDirectory) { if (!isDirectory) {
FileNameMap map = java.net.URLConnection.getFileNameMap(); FileNameMap map = java.net.URLConnection.getFileNameMap();
contentType = map.getContentTypeFor(filename); String contentType = map.getContentTypeFor(file.getPath());
if (contentType != null) { if (contentType != null) {
properties.add(CONTENT_TYPE, contentType); properties.add(CONTENT_TYPE, contentType);
} }
properties.add(CONTENT_LENGTH, String.valueOf(length)); properties.add(CONTENT_LENGTH, Long.toString(length));
/* /*
* Format the last-modified field into the preferred * Format the last-modified field into the preferred
@ -179,32 +174,26 @@ public class FileURLConnection extends URLConnection {
public synchronized InputStream getInputStream() public synchronized InputStream getInputStream()
throws IOException { throws IOException {
int iconHeight;
int iconWidth;
connect(); connect();
if (is == null) { if (is == null) {
if (isDirectory) { if (isDirectory) {
FileNameMap map = java.net.URLConnection.getFileNameMap();
StringBuilder sb = new StringBuilder(); if (directoryListing == null) {
throw new FileNotFoundException(file.getPath());
if (files == null) {
throw new FileNotFoundException(filename);
} }
files.sort(Collator.getInstance()); directoryListing.sort(Collator.getInstance());
for (int i = 0 ; i < files.size() ; i++) { StringBuilder sb = new StringBuilder();
String fileName = files.get(i); for (String fileName : directoryListing) {
sb.append(fileName); sb.append(fileName);
sb.append("\n"); sb.append("\n");
} }
// Put it into a (default) locale-specific byte-stream. // Put it into a (default) locale-specific byte-stream.
is = new ByteArrayInputStream(sb.toString().getBytes()); is = new ByteArrayInputStream(sb.toString().getBytes());
} else { } else {
throw new FileNotFoundException(filename); throw new FileNotFoundException(file.getPath());
} }
} }
return is; return is;