Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 8212082

Browse files
committed
Optimizations
1 parent ffcb3dd commit 8212082

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/main/java/com/kttdevelopment/simplehttpserver/handler/DirectoryEntry.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,12 @@ public final File getFile(final String path){
240240
final File parent = new File(directory.getAbsolutePath() + path).getParentFile();
241241
if(!parent.getAbsolutePath().startsWith(directory.getAbsolutePath())) return null;
242242
final String name = path.substring(path.lastIndexOf('/'));
243-
final File[] listFiles = parent.listFiles(pathname -> !pathname.isDirectory());
244243

245-
for(final File file : (listFiles == null) ? new File[0] : listFiles)
244+
for(final File file : Objects.requireNonNullElse(directory.listFiles(p -> !p.isDirectory()),new File[0]))
246245
if(adapter.getName(file).equals(name))
247246
return file;
248247
}else{
249-
final File[] listFiles = directory.listFiles(pathname -> !pathname.isDirectory());
250-
251-
for(final File file : (listFiles == null) ? new File[0] : listFiles)
248+
for(final File file : Objects.requireNonNullElse(directory.listFiles(p -> !p.isDirectory()),new File[0]))
252249
if(adapter.getName(file).equals(path))
253250
return file;
254251
}
@@ -273,16 +270,12 @@ public final byte[] getBytes(final String path){
273270
for(final String key : preloadedFiles.keySet())
274271
if(rel.startsWith(key) && key.startsWith(match))
275272
match = key;
276-
if(!match.isEmpty()){
277-
return preloadedFiles.get(match).getBytes();
278-
}else{
279-
return null;
280-
}
273+
return !match.isEmpty() ? preloadedFiles.get(match).getBytes() : null;
281274
}else{
282275
final File file = getFile(path);
283276
try{
284277
return adapter.getBytes(file,Files.readAllBytes(Objects.requireNonNull(file).toPath()));
285-
}catch(NullPointerException | IOException ignored){
278+
}catch(final Exception ignored){
286279
return null;
287280
}
288281
}
@@ -328,13 +321,13 @@ private static String getContext(final String path){
328321
@Override
329322
public String toString(){
330323
return
331-
"DirectoryEntry" + '{' +
332-
"directory" + '=' + directory + ", " +
333-
"adapter" + '=' + adapter + ", " +
334-
"loadingOption" + '=' + loadingOption + ", " +
335-
"isWalkthrough" + '=' + isWalkthrough + ", " +
336-
"files" + '=' + preloadedFiles +
337-
'}';
324+
"DirectoryEntry" + '{' +
325+
"directory" + '=' + directory + ", " +
326+
"adapter" + '=' + adapter + ", " +
327+
"loadingOption" + '=' + loadingOption + ", " +
328+
"isWalkthrough" + '=' + isWalkthrough + ", " +
329+
"files" + '=' + preloadedFiles +
330+
'}';
338331
}
339332

340333
}

0 commit comments

Comments
 (0)