1 module capnproto.MemoryMapped; 2 3 import std.mmfile : MmFile; 4 5 import java.nio.ByteBuffer; 6 import java.nio.channels.ReadableByteChannel; 7 8 final class MemoryMapped : ReadableByteChannel 9 { 10 public: //Methods. 11 this(MmFile file) 12 { 13 this.file = file; 14 } 15 16 bool isOpen() 17 { 18 return true; 19 } 20 21 void close() 22 { 23 24 } 25 26 ///Setup map from file to dst. 27 size_t read(ref ByteBuffer dst) 28 { 29 import std.algorithm : min; 30 auto size = min(dst.remaining(), file.length - index); 31 if(size == 0) 32 return 0; 33 dst.buffer = cast(ubyte[])file[index..index+size]; 34 dst.position += size; 35 index += size; 36 return size; 37 } 38 39 private: //Variables. 40 MmFile file; 41 size_t index; 42 }