1 module hunt.http.codec.http.model.PathContentProvider; 2 3 import hunt.logging; 4 5 6 // import java.io.Closeable; 7 // import hunt.Exceptions; 8 // import hunt.io.ByteBuffer; 9 // import java.nio.channels.SeekableByteChannel; 10 // import java.nio.file; 11 // import java.util.Iterator; 12 // import java.util.NoSuchElementException; 13 14 // /** 15 // * <p>A {@link ContentProvider} for files using JDK 7's {@code java.nio.file} APIs.</p> 16 // * <p>It is possible to specify, at the constructor, a buffer size used to read 17 // * content from the stream, by default 4096 bytes.</p> 18 // */ 19 // class PathContentProvider :AbstractTypedContentProvider { 20 21 22 23 // private Path filePath; 24 // private long fileSize; 25 // private int bufferSize; 26 27 // this(Path filePath) throws IOException { 28 // this(filePath, 4096); 29 // } 30 31 // this(Path filePath, int bufferSize) throws IOException { 32 // this("application/octet-stream", filePath, bufferSize); 33 // } 34 35 // this(string contentType, Path filePath) throws IOException { 36 // this(contentType, filePath, 4096); 37 // } 38 39 // this(string contentType, Path filePath, int bufferSize) throws IOException { 40 // super(contentType); 41 // if (!Files.isRegularFile(filePath)) 42 // throw new NoSuchFileException(filePath.toString()); 43 // if (!Files.isReadable(filePath)) 44 // throw new AccessDeniedException(filePath.toString()); 45 // this.filePath = filePath; 46 // this.fileSize = Files.size(filePath); 47 // this.bufferSize = bufferSize; 48 // } 49 50 // override 51 // long getLength() { 52 // return fileSize; 53 // } 54 55 // override 56 // Iterator!ByteBuffer iterator() { 57 // return new PathIterator(); 58 // } 59 60 // private class PathIterator : Iterator!ByteBuffer, Closeable { 61 // private SeekableByteChannel channel; 62 // private long position; 63 64 // override 65 // bool hasNext() { 66 // return position < getLength(); 67 // } 68 69 // override 70 // ByteBuffer next() { 71 // try { 72 // if (channel == null) { 73 // channel = Files.newByteChannel(filePath, StandardOpenOption.READ); 74 // version(HUNT_DEBUG) 75 // tracef("Opened file %s", filePath); 76 // } 77 78 // ByteBuffer buffer = BufferUtils.allocate(bufferSize); 79 // int read = channel.read(buffer); 80 // if (read < 0) 81 // throw new NoSuchElementException(); 82 83 // version(HUNT_DEBUG) 84 // tracef("Read %s bytes from %s", read, filePath); 85 86 // position += read; 87 88 // buffer.flip(); 89 // return buffer; 90 // } catch (NoSuchElementException x) { 91 // close(); 92 // throw x; 93 // } catch (Exception x) { 94 // close(); 95 // throw (NoSuchElementException) new NoSuchElementException().initCause(x); 96 // } 97 // } 98 99 // override 100 // void close() { 101 // try { 102 // if (channel != null) 103 // channel.close(); 104 // } catch (Exception x) { 105 // LOG.error("channel close error", x); 106 // } 107 // } 108 // } 109 // }