1 module streams.types.file; 2 3 import std.stdio; 4 5 struct FileInputStream { 6 private File file; 7 8 this(File file) { 9 this.file = file; 10 } 11 12 this(string filename) { 13 this(File(filename, "rb")); 14 } 15 16 int read(ubyte[] buffer) { 17 ubyte[] slice = this.file.rawRead(buffer); 18 return cast(int) slice.length; 19 } 20 21 void close() { 22 if (this.file.isOpen()) { 23 this.file.close(); 24 } 25 } 26 } 27 28 unittest { 29 import streams.primitives; 30 31 assert(isInputStream!(FileInputStream, ubyte)); 32 assert(isClosableStream!FileInputStream); 33 } 34 35 struct FileOutputStream { 36 private File file; 37 38 this(File file) { 39 this.file = file; 40 } 41 42 this(string filename) { 43 this(File(filename, "wb")); 44 } 45 46 int write(ubyte[] buffer) { 47 this.file.rawWrite(buffer); 48 return cast(int) buffer.length; 49 } 50 51 void flush() { 52 this.file.flush(); 53 } 54 55 void close() { 56 if (this.file.isOpen()) { 57 this.file.close(); 58 } 59 } 60 } 61 62 unittest { 63 import streams.primitives; 64 65 assert(isOutputStream!(FileOutputStream, ubyte)); 66 assert(isClosableStream!FileOutputStream); 67 assert(isFlushableStream!FileOutputStream); 68 }