Skip to content

Commit 9768f60

Browse files
author
Eirik Bjørsnøs
committed
8345249: Apply some conservative cleanups in FileURLConnection
Reviewed-by: jpai, djelinski
1 parent c5a69b6 commit 9768f60

File tree

1 file changed

+25
-36
lines changed

1 file changed

+25
-36
lines changed

src/java.base/share/classes/sun/net/www/protocol/file/FileURLConnection.java

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ public class FileURLConnection extends URLConnection {
4646
private static final String TEXT_PLAIN = "text/plain";
4747
private static final String LAST_MODIFIED = "last-modified";
4848

49-
String contentType;
50-
InputStream is;
49+
private final File file;
50+
private InputStream is;
51+
private List<String> directoryListing;
5152

52-
File file;
53-
String filename;
54-
boolean isDirectory = false;
55-
boolean exists = false;
56-
List<String> files;
53+
private boolean isDirectory = false;
54+
private boolean exists = false;
5755

58-
long length = -1;
59-
long lastModified = 0;
56+
private long length = -1;
57+
private long lastModified = 0;
6058

6159
protected FileURLConnection(URL u, File file) {
6260
super(u);
@@ -71,20 +69,17 @@ protected FileURLConnection(URL u, File file) {
7169
*/
7270
public void connect() throws IOException {
7371
if (!connected) {
74-
try {
75-
filename = file.toString();
76-
isDirectory = file.isDirectory();
77-
if (isDirectory) {
78-
String[] fileList = file.list();
79-
if (fileList == null)
80-
throw new FileNotFoundException(filename + " exists, but is not accessible");
81-
files = Arrays.<String>asList(fileList);
82-
} else {
83-
is = new BufferedInputStream(new FileInputStream(filename));
84-
}
85-
} catch (IOException e) {
86-
throw e;
72+
73+
isDirectory = file.isDirectory();
74+
if (isDirectory) {
75+
String[] fileList = file.list();
76+
if (fileList == null)
77+
throw new FileNotFoundException(file.getPath() + " exists, but is not accessible");
78+
directoryListing = Arrays.asList(fileList);
79+
} else {
80+
is = new BufferedInputStream(new FileInputStream(file.getPath()));
8781
}
82+
8883
connected = true;
8984
}
9085
}
@@ -109,11 +104,11 @@ private void initializeHeaders() {
109104

110105
if (!isDirectory) {
111106
FileNameMap map = java.net.URLConnection.getFileNameMap();
112-
contentType = map.getContentTypeFor(filename);
107+
String contentType = map.getContentTypeFor(file.getPath());
113108
if (contentType != null) {
114109
properties.add(CONTENT_TYPE, contentType);
115110
}
116-
properties.add(CONTENT_LENGTH, String.valueOf(length));
111+
properties.add(CONTENT_LENGTH, Long.toString(length));
117112

118113
/*
119114
* Format the last-modified field into the preferred
@@ -179,32 +174,26 @@ public long getLastModified() {
179174
public synchronized InputStream getInputStream()
180175
throws IOException {
181176

182-
int iconHeight;
183-
int iconWidth;
184-
185177
connect();
186178

187179
if (is == null) {
188180
if (isDirectory) {
189-
FileNameMap map = java.net.URLConnection.getFileNameMap();
190181

191-
StringBuilder sb = new StringBuilder();
192-
193-
if (files == null) {
194-
throw new FileNotFoundException(filename);
182+
if (directoryListing == null) {
183+
throw new FileNotFoundException(file.getPath());
195184
}
196185

197-
files.sort(Collator.getInstance());
186+
directoryListing.sort(Collator.getInstance());
198187

199-
for (int i = 0 ; i < files.size() ; i++) {
200-
String fileName = files.get(i);
188+
StringBuilder sb = new StringBuilder();
189+
for (String fileName : directoryListing) {
201190
sb.append(fileName);
202191
sb.append("\n");
203192
}
204193
// Put it into a (default) locale-specific byte-stream.
205194
is = new ByteArrayInputStream(sb.toString().getBytes());
206195
} else {
207-
throw new FileNotFoundException(filename);
196+
throw new FileNotFoundException(file.getPath());
208197
}
209198
}
210199
return is;

0 commit comments

Comments
 (0)