PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tango/io/model/IConduit.d

http://github.com/SiegeLord/Tango-D2
D | 329 lines | 61 code | 66 blank | 202 comment | 0 complexity | c41b912ae3fd7c197172ca3c2599bed6 MD5 | raw file
  1. /*******************************************************************************
  2. copyright: Copyright (c) 2004 Kris Bell. All rights reserved
  3. license: BSD style: $(LICENSE)
  4. version: Initial release: March 2004$(BR)
  5. Outback release: December 2006
  6. author: Kris
  7. *******************************************************************************/
  8. module tango.io.model.IConduit;
  9. /*******************************************************************************
  10. Conduits provide virtualized access to external content, and
  11. represent things like files or Internet connections. Conduits
  12. expose a pair of streams, are modelled by tango.io.model.IConduit,
  13. and are implemented via classes such as File & SocketConduit.
  14. Additional kinds of conduit are easy to construct: one either
  15. subclasses tango.io.device.Conduit, or implements tango.io.model.IConduit.
  16. A conduit typically reads and writes from/to an IBuffer in large
  17. chunks, typically the entire buffer. Alternatively, one can invoke
  18. input.read(dst[]) and/or output.write(src[]) directly.
  19. *******************************************************************************/
  20. interface IConduit : InputStream, OutputStream
  21. {
  22. /***********************************************************************
  23. Return a preferred size for buffering conduit I/O.
  24. ***********************************************************************/
  25. @property abstract size_t bufferSize ();
  26. /***********************************************************************
  27. Return the name of this conduit.
  28. ***********************************************************************/
  29. abstract immutable(char)[] toString ();
  30. /***********************************************************************
  31. Is the conduit alive?
  32. ***********************************************************************/
  33. @property abstract const bool isAlive ();
  34. /***********************************************************************
  35. Release external resources.
  36. ***********************************************************************/
  37. abstract void detach ();
  38. /***********************************************************************
  39. Throw a generic IO exception with the provided msg.
  40. ***********************************************************************/
  41. abstract void error (const(char[]) msg);
  42. /***********************************************************************
  43. All streams now support seek(), so this is used to signal
  44. a seekable conduit instead.
  45. ***********************************************************************/
  46. interface Seek {}
  47. /***********************************************************************
  48. Indicates the conduit supports resize/truncation.
  49. ***********************************************************************/
  50. interface Truncate
  51. {
  52. void truncate (long size);
  53. }
  54. }
  55. /*******************************************************************************
  56. Describes how to make an IO entity usable with selectors.
  57. *******************************************************************************/
  58. interface ISelectable
  59. {
  60. version (Windows)
  61. alias void* Handle; /// Opaque OS file-handle.
  62. else
  63. alias int Handle;/* = -1;*/ /// Opaque OS file-handle.
  64. /***********************************************************************
  65. Models a handle-oriented device.
  66. TODO: Figure out how to avoid exposing this in the general
  67. case.
  68. ***********************************************************************/
  69. @property Handle fileHandle ();
  70. }
  71. /*******************************************************************************
  72. The common attributes of streams.
  73. *******************************************************************************/
  74. interface IOStream
  75. {
  76. enum Eof = -1; /// the End-of-Flow identifer
  77. /***********************************************************************
  78. The anchor positions supported by seek().
  79. ***********************************************************************/
  80. enum Anchor {
  81. Begin = 0,
  82. Current = 1,
  83. End = 2,
  84. };
  85. /***********************************************************************
  86. Move the stream position to the given offset from the
  87. provided anchor point, and return adjusted position.
  88. Those conduits which don't support seeking will throw
  89. an IOException (and don't implement IConduit.Seek).
  90. ***********************************************************************/
  91. long seek (long offset, Anchor anchor = Anchor.Begin);
  92. /***********************************************************************
  93. Return the host conduit.
  94. ***********************************************************************/
  95. @property IConduit conduit ();
  96. /***********************************************************************
  97. Flush buffered content. For InputStream this is equivalent
  98. to clearing buffered content.
  99. ***********************************************************************/
  100. IOStream flush ();
  101. /***********************************************************************
  102. Close the input.
  103. ***********************************************************************/
  104. void close ();
  105. /***********************************************************************
  106. Marks a stream that performs read/write mutation, rather than
  107. generic decoration. This is used to identify those stream that
  108. should explicitly not share an upstream buffer with downstream
  109. siblings.
  110. Many streams add simple decoration (such as DataStream) while
  111. others are merely template aliases. However, streams such as
  112. EndianStream mutate content as it passes through the read and
  113. write methods, which must be respected. On one hand we wish
  114. to share a single buffer instance, while on the other we must
  115. ensure correct data flow through an arbitrary combinations of
  116. streams.
  117. There are two stream variations: one which operate directly
  118. upon memory (and thus must have access to a buffer) and another
  119. that prefer to have buffered input (for performance reasons) but
  120. can operate without. EndianStream is an example of the former,
  121. while DataStream represents the latter.
  122. In order to sort out who gets what, each stream makes a request
  123. for an upstream buffer at construction time. The request has an
  124. indication of the intended purpose (array-based access, or not).
  125. ***********************************************************************/
  126. interface Mutator {}
  127. }
  128. /*******************************************************************************
  129. The Tango input stream.
  130. *******************************************************************************/
  131. interface InputStream : IOStream
  132. {
  133. /***********************************************************************
  134. Read from stream into a target array. The provided dst
  135. will be populated with content from the stream.
  136. Returns the number of bytes read, which may be less than
  137. requested in dst. Eof is returned whenever an end-of-flow
  138. condition arises.
  139. ***********************************************************************/
  140. size_t read (void[] dst);
  141. /***********************************************************************
  142. Load the bits from a stream, and return them all in an
  143. array. The optional max value indicates the maximum
  144. number of bytes to be read.
  145. Returns an array representing the content, and throws
  146. IOException on error.
  147. ***********************************************************************/
  148. void[] load (size_t max = -1);
  149. /***********************************************************************
  150. Return the upstream source.
  151. ***********************************************************************/
  152. @property InputStream input ();
  153. }
  154. /*******************************************************************************
  155. The Tango output stream.
  156. *******************************************************************************/
  157. interface OutputStream : IOStream
  158. {
  159. /***********************************************************************
  160. Write to stream from a source array. The provided src
  161. content will be written to the stream.
  162. Returns the number of bytes written from src, which may
  163. be less than the quantity provided. Eof is returned when
  164. an end-of-flow condition arises.
  165. ***********************************************************************/
  166. size_t write (const(void)[] src);
  167. /***********************************************************************
  168. Transfer the content of another stream to this one. Returns
  169. a reference to this class, and throws IOException on failure.
  170. ***********************************************************************/
  171. OutputStream copy (InputStream src, size_t max = -1);
  172. /***********************************************************************
  173. Return the upstream sink.
  174. ***********************************************************************/
  175. @property OutputStream output ();
  176. }
  177. /*******************************************************************************
  178. A buffered input stream.
  179. *******************************************************************************/
  180. interface InputBuffer : InputStream
  181. {
  182. void[] slice ();
  183. bool next (scope size_t delegate(const(void)[]) scan);
  184. size_t reader (scope size_t delegate(const(void)[]) consumer);
  185. }
  186. /*******************************************************************************
  187. A buffered output stream.
  188. *******************************************************************************/
  189. interface OutputBuffer : OutputStream
  190. {
  191. alias append opCall;
  192. void[] slice ();
  193. OutputBuffer append (const(void)[]);
  194. size_t writer (size_t delegate(void[]) producer);
  195. }