Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #553: MemoryConduitEnhanced.d

File MemoryConduitEnhanced.d, 2.9 kB (added by Nietsnie, 1 year ago)

Example MemoryConduit? with the above implementation

Line 
1 module tango.io.MemoryConduitEnhanced;
2
3 public import tango.io.MemoryConduit;
4 public import tango.io.model.IConduit;
5
6 class MemoryConduitEnhanced : Conduit, IConduit.Seek
7 {
8
9     private void[] content;
10     private long _pos = 0;
11
12     /***********************************************************************
13
14       Return a preferred size for buffering conduit I/O
15
16      ***********************************************************************/
17
18     uint bufferSize ()
19     {
20         return 1024 * 4;
21     }
22
23     /***********************************************************************
24
25       Return the name of this device
26
27      ***********************************************************************/
28
29     protected char[] toUtf8()
30     {
31         return "<memory>";
32     }
33
34     /***********************************************************************
35
36       Return the underlying OS handle of this Conduit
37
38      ***********************************************************************/
39
40     final Handle fileHandle ()
41     {
42         return 0;
43     }
44
45     /***********************************************************************
46
47       Return the content held
48
49      ***********************************************************************/
50
51     final void[] slice ()
52     {
53         return content;
54     }
55
56     /***********************************************************************
57
58       Read a chunk of bytes from the file into the provided
59       array (typically that belonging to an IBuffer)
60
61      ***********************************************************************/
62
63     protected override uint read(void[] dst)
64     {
65         uint len = Eof;
66
67         if (content.length)
68         {
69             len = content.length;
70             if (len > dst.length)
71                 len = dst.length;
72
73             dst[0 .. len] = content[this._pos .. (len + this._pos)];
74             this._pos += len;
75         }
76
77         return len;
78     }
79
80     /***********************************************************************
81
82       Write a chunk of bytes to the file from the provided
83       array (typically that belonging to an IBuffer)
84
85      ***********************************************************************/
86
87     protected override uint write (void[] src)
88     {
89         content ~= src;
90         return src.length;
91     }
92
93     /***********************************************************************
94
95       Return the position of the conduit
96
97      ***********************************************************************/
98
99     long position()
100     {
101         return this._pos;
102     }
103
104     /***********************************************************************
105
106       Basic seek support
107
108      ***********************************************************************/
109
110     // this doesn't support seeking past the end to grow the array... suppose it could?
111     long seek(long offset, Seek.Anchor anchor = Seek.Anchor.Begin)
112     {
113         long rtn = -1;
114
115         switch (anchor)
116         {
117             case Seek.Anchor.Begin:
118                 {
119                     if (offset <= content.length)
120                         this._pos = offset;
121                     break;
122                 }
123             case Seek.Anchor.Current:
124                 {
125                     if ((offset + this._pos) <= content.length)
126                         this._pos += offset;
127                     break;
128                 }
129             default: break;
130         }
131
132         return rtn;
133     }
134
135 }