File Handler - Smart Loading
Katsute opened this issue · 1 comments
Katsute commented
Prerequisites
If all checks are not passed then the request will be closed
- I have checked that no other similar feature request already exists
- The feature request makes sense for the project
Proposal
Describe the feature that should be added
Replace boolean param preload with enum for preload, watchload (watch file change), liveload
Reasoning
Explain why this feature should be added
- Allows more efficient file reading
Katsute commented
Implement in FileEntry
simplehttpserver/src/main/java/com/kttdevelopment/simplehttpserver/handler/FileEntry.java
Lines 1 to 112 in b8986ef
| package com.kttdevelopment.simplehttpserver.handler; | |
| import java.io.*; | |
| import java.nio.file.Files; | |
| import java.util.Arrays; | |
| /** | |
| * Represent a file in the {@link FileHandler}. Applications do not use this class. | |
| * | |
| * @see FileHandler | |
| * @since 02.00.00 | |
| * @version 02.00.00 | |
| * @author Ktt Development | |
| */ | |
| class FileEntry { | |
| private final boolean isPreloaded; | |
| private final File file; | |
| private byte[] preloadedBytes; | |
| /** | |
| * Creates a file entry. | |
| * | |
| * @param file file to represent | |
| * @param isPreloaded whether to read bytes now or at runtime | |
| * @param bytesAdapter how to process the bytes in {@link #getBytes()} | |
| * @throws FileNotFoundException if file was not found or is not a file | |
| * | |
| * @see FileBytesAdapter | |
| * @since 02.00.00 | |
| * @author Ktt Development | |
| */ | |
| FileEntry(final File file, final boolean isPreloaded, final FileBytesAdapter bytesAdapter) throws FileNotFoundException{ | |
| if(!file.exists() || file.isDirectory()) | |
| throw new FileNotFoundException("File at " + file.getAbsoluteFile() + " was not found"); | |
| this.file = file; | |
| this.isPreloaded = isPreloaded; | |
| if(isPreloaded) | |
| try{ | |
| preloadedBytes = bytesAdapter.getBytes(file,Files.readAllBytes(file.toPath())); | |
| }catch(final IOException ignored){ | |
| preloadedBytes = null; | |
| } | |
| } | |
| // | |
| /** | |
| * Returns if the file was preloaded. | |
| * | |
| * @return if file was preloaded | |
| * | |
| * @since 02.00.00 | |
| * @author Ktt Development | |
| */ | |
| public final boolean isPreloaded(){ | |
| return isPreloaded; | |
| } | |
| // | |
| /** | |
| * Returns the file being referenced | |
| * | |
| * @return reference file | |
| * | |
| * @since 02.00.00 | |
| * @author Ktt Development | |
| */ | |
| public final File getFile(){ | |
| return file; | |
| } | |
| /** | |
| * Returns the file's bytes after the {@link FileBytesAdapter} was used. | |
| * | |
| * @return processed file bytes | |
| * | |
| * @see FileBytesAdapter | |
| * @since 02.00.00 | |
| * @author Ktt Development | |
| */ | |
| public final byte[] getBytes(){ | |
| if(isPreloaded) | |
| return preloadedBytes; // adapter determined preloaded bytes | |
| else | |
| try{ | |
| return Files.readAllBytes(file.toPath()); // return literal bytes (no preload); adapt bytes in next part | |
| }catch(final IOException e){ | |
| return null; | |
| } | |
| } | |
| // | |
| @SuppressWarnings("StringBufferReplaceableByString") | |
| @Override | |
| public String toString(){ | |
| final StringBuilder OUT = new StringBuilder(); | |
| OUT.append("FileEntry") .append("{"); | |
| OUT.append("isPreloaded") .append("=") .append(isPreloaded) .append(", "); | |
| OUT.append("file") .append("=") .append(file.toString()) .append(", "); | |
| OUT.append("(preloaded) bytes") .append("=") .append(Arrays.toString(preloadedBytes)) .append(", "); | |
| OUT.append("}"); | |
| return OUT.toString(); | |
| } | |
| } |
WatchService for watchload
https://docs.oracle.com/javase/tutorial/essential/io/notification.html