1 module capnproto.FileDescriptor;
2 
3 import std.stdio : File;
4 
5 import java.nio.ByteBuffer;
6 import java.nio.channels.ReadableByteChannel;
7 import java.nio.channels.WritableByteChannel;
8 
9 final class FileDescriptor : ReadableByteChannel, WritableByteChannel
10 {
11 public: //Methods.
12 	this(File file)
13 	{
14 		this.file = file;
15 	}
16 	
17 	bool isOpen()
18 	{
19 		return true;
20 	}
21 	
22 	void close()
23 	{
24 		file.close();
25 	}
26 	
27 	///Reads from fd to dst.
28 	size_t read(ref ByteBuffer dst)
29 	{
30 		if(dst.buffer is null)
31 			dst.buffer = new ubyte[](dst.remaining);
32 		file.rawRead(dst.buffer);
33 		dst.position = dst.buffer.length;
34 		dst.limit = dst.buffer.length;
35 		return dst.buffer.length;
36 	}
37 	
38 	///Writes from src to fd.
39 	size_t write(ref ByteBuffer src)
40 	{
41 		file.rawWrite(src.buffer[0..src.limit]);
42 		src.position = src.limit;
43 		return src.limit;
44 	}
45 
46 private: //Variables.
47 	File file;
48 }