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

Commit 24abb7c

Browse files
committed
bug fixes
1 parent bcab169 commit 24abb7c

File tree

3 files changed

+49
-53
lines changed

3 files changed

+49
-53
lines changed

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,49 @@ else if(type == ENTRY_MODIFY)
101101
final String rel = dirPath.relativize(path).toString();
102102

103103
final File[] listFile = Objects.requireNonNullElse(p2f.listFiles(),new File[0]);
104-
for(final File file : listFile){
104+
for(final File file : listFile)
105105
try{
106106
preloadedFiles.put(
107107
(rel.isEmpty() || rel.equals("/") || rel.equals("\\") ? "" : getContext(rel)) + getContext(adapter.getName(file)),
108108
new FileEntry(file,adapter,loadingOption,true)
109109
);
110110
}catch(final RuntimeException ignored){ }
111111

112-
// watch service
113-
try{
114-
final WatchService service = FileSystems.getDefault().newWatchService();
115-
final Path dpath = file.toPath();
116-
dpath.register(service, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
117-
118-
new Thread(() -> {
119-
WatchKey key;
120-
try{
121-
while((key = service.take()) != null){
122-
for(WatchEvent<?> event : key.pollEvents()){
123-
try{
124-
final Path target = (Path) event.context();
125-
final File targFile = target.toFile();
126-
final WatchEvent.Kind<?> type = event.kind();
127-
128-
final String context = getContext(adapter.getName(targFile));
129-
130-
if(type == ENTRY_CREATE)
131-
preloadedFiles.put(
132-
context,
133-
new FileEntry(targFile,adapter,loadingOption,true)
134-
);
135-
else if(type == ENTRY_DELETE)
136-
preloadedFiles.remove(context);
137-
else if(type == ENTRY_MODIFY)
138-
preloadedFiles.get(context).reloadBytes();
139-
}catch(final ClassCastException ignored){ }
140-
}
112+
// watch service
113+
try{
114+
final WatchService service = FileSystems.getDefault().newWatchService();
115+
path.register(service, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
116+
final String relative = getContext(dirPath.relativize(path).toString());
117+
118+
new Thread(() -> {
119+
WatchKey key;
120+
try{
121+
while((key = service.take()) != null){
122+
for(WatchEvent<?> event : key.pollEvents()){
123+
try{
124+
final Path target = dirPath.resolve((Path) event.context());
125+
final File targFile = target.toFile();
126+
final WatchEvent.Kind<?> type = event.kind();
127+
128+
final String context = relative + getContext(adapter.getName(targFile));
129+
130+
if(type == ENTRY_CREATE)
131+
preloadedFiles.put(
132+
context,
133+
new FileEntry(targFile,adapter,loadingOption,true)
134+
);
135+
else if(type == ENTRY_DELETE)
136+
preloadedFiles.remove(context);
137+
else if(type == ENTRY_MODIFY)
138+
preloadedFiles.get(context).reloadBytes();
139+
}catch(final ClassCastException ignored){ }
141140
}
142-
}catch(final InterruptedException ignored){ }
143-
}).start();
144-
}catch(final IOException e){
145-
throw new RuntimeException(e);
146-
}
141+
key.reset();
142+
}
143+
}catch(final InterruptedException ignored){ }
144+
}).start();
145+
}catch(final IOException e){
146+
throw new RuntimeException(e);
147147
}
148148
});
149149
}catch(final IOException e){
@@ -242,11 +242,11 @@ public final File getFile(final String path){
242242
final String name = path.substring(path.lastIndexOf('/'));
243243

244244
for(final File file : Objects.requireNonNullElse(directory.listFiles(p -> !p.isDirectory()),new File[0]))
245-
if(adapter.getName(file).equals(name))
245+
if(getContext(adapter.getName(file)).equals(name))
246246
return file;
247247
}else{
248248
for(final File file : Objects.requireNonNullElse(directory.listFiles(p -> !p.isDirectory()),new File[0]))
249-
if(adapter.getName(file).equals(path))
249+
if(getContext(adapter.getName(file)).equals(path))
250250
return file;
251251
}
252252
return null;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class FileEntry {
4343
* Creates a file entry.
4444
*
4545
* @param file file to represent
46-
* @param bytesAdapter how to process the bytes in {@link #getBytes()}
46+
* @param adapter how to process the bytes in {@link #getBytes()}
4747
* @param loadingOption how to handle the initial file loading
4848
* @param skipWatchService skip creating a watch service ({@link ByteLoadingOption#WATCHLOAD} only).
4949
* @throws RuntimeException I/O failure to start watch service ({@link ByteLoadingOption#WATCHLOAD} only).
@@ -53,10 +53,10 @@ class FileEntry {
5353
* @since 03.05.00
5454
* @author Ktt Development
5555
*/
56-
FileEntry(final File file, final FileBytesAdapter bytesAdapter, final ByteLoadingOption loadingOption, final boolean skipWatchService){
56+
FileEntry(final File file, final FileBytesAdapter adapter, final ByteLoadingOption loadingOption, final boolean skipWatchService){
5757
this.file = file;
5858
this.loadingOption = loadingOption;
59-
this.adapter = bytesAdapter;
59+
this.adapter = adapter;
6060

6161
switch(loadingOption){
6262
case WATCHLOAD:
@@ -76,7 +76,7 @@ class FileEntry {
7676
final Path modified = path.resolve((Path) event.context());
7777
try{
7878
if(Files.isSameFile(target, modified))
79-
preloadedBytes = bytesAdapter.getBytes(file,Files.readAllBytes(target));
79+
preloadedBytes = adapter.getBytes(file,Files.readAllBytes(target));
8080
}catch(final IOException ignored){ } // don't overwrite if corrupt
8181
}catch(final ClassCastException ignored){ }
8282
}
@@ -89,7 +89,7 @@ class FileEntry {
8989
}
9090
case PRELOAD:
9191
try{
92-
preloadedBytes = bytesAdapter.getBytes(file,Files.readAllBytes(file.toPath()));
92+
preloadedBytes = adapter.getBytes(file,Files.readAllBytes(file.toPath()));
9393
}catch(final Exception ignored){
9494
preloadedBytes = null;
9595
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public final void addDirectory(final File directory, final String directoryName)
625625
* @since 02.00.00
626626
* @author Ktt Development
627627
*/
628-
@Deprecated
628+
@Deprecated // todo: convert to boolean to walk after depreciation
629629
public final void addDirectory(final File directory, final String directoryName, final boolean preload){ // todo: after depreciation replace with walk param
630630
addDirectory("",directory,directoryName,preload ? ByteLoadingOption.PRELOAD : ByteLoadingOption.LIVELOAD,false);
631631
}
@@ -759,7 +759,7 @@ public final void addDirectory(final String context, final File directory){
759759
* @since 02.00.00
760760
* @author Ktt Development
761761
*/
762-
@Deprecated
762+
@Deprecated // todo: after depreciation convert boolean to walk
763763
public final void addDirectory(final String context, final File directory, final boolean preload){ // todo: after depreciation replace with walk param
764764
addDirectory(context,directory,directory.getName(),preload ? ByteLoadingOption.PRELOAD : ByteLoadingOption.LIVELOAD,false);
765765
}
@@ -895,7 +895,7 @@ public final void addDirectory(final String context, final File directory, final
895895
* @since 02.00.00
896896
* @author Ktt Development
897897
*/
898-
@Deprecated
898+
@Deprecated // todo: after depreciation convert boolean to walk
899899
public final void addDirectory(final String context, final File directory, final String directoryName, final boolean preload){ // todo: after depreciation replace with walk param
900900
addDirectory(context,directory,directoryName,preload ? ByteLoadingOption.PRELOAD : ByteLoadingOption.LIVELOAD,false);
901901
}
@@ -988,7 +988,7 @@ public final void addDirectory(final String context, final File directory, final
988988
target.isEmpty() ? "/" : target,
989989
new DirectoryEntry(directory, adapter,loadingOption,walk)
990990
);
991-
}catch(final Exception ignored){ }
991+
}catch(final Exception ignored){}
992992
}
993993

994994
//
@@ -1016,16 +1016,12 @@ public final void handle(final SimpleHttpExchange exchange) throws IOException{
10161016
final String rel2;
10171017
try{
10181018
rel2 = rel.substring(match.length());
1019-
1019+
final File file = entry.getFile(rel2);
10201020
if(entry.getLoadingOption() != ByteLoadingOption.LIVELOAD){
1021-
final File file;
1022-
if((file = entry.getFile(rel2)) != null){
1023-
handle(exchange, file, entry.getBytes(rel2)); // use adapted preload
1024-
}
1021+
handle(exchange, file, entry.getBytes(rel2)); // use adapted preload
10251022
}else{
1026-
final File file = new File(entry.getDirectory().getAbsolutePath() + "\\" + rel2);
10271023
byte[] bytes = null;
1028-
try{ bytes = Files.readAllBytes(file.toPath());
1024+
try{ bytes = Files.readAllBytes(Objects.requireNonNull(file).toPath());
10291025
}catch(final Exception ignored){ }
10301026
handle(exchange,file,adapter.getBytes(file, bytes)); // use adapted now
10311027
}

0 commit comments

Comments
 (0)