1 using System;
2 using System.IO;
3
4 public interface Reader
5 {
6 long Read(byte* buf, long count);
7 }
8
9 public interface Writer
10 {
11 void Write(byte* buf, long count);
12 }
13
14 public void Copy(Reader reader, Writer writer)
15 {
16 byte[16] buf;
17 long bytesRead = reader.Read(&buf[0], buf.Length());
18 while (bytesRead > 0)
19 {
20 writer.Write(&buf[0], bytesRead);
21 bytesRead = reader.Read(&buf[0], buf.Length());
22 }
23 }
24
25 public class StringReader : Reader
26 {
27 public StringReader(const string& s_) : s(s_), pos(0)
28 {
29 }
30 public long Read(byte* buf, long count)
31 {
32 long bytesLeft = s.Length() - pos;
33 long bytesRead = Min(bytesLeft, count);
34 for (long i = 0; i < bytesRead; ++i;)
35 {
36 buf[i] = cast<byte>(s[pos + i]);
37 }
38 pos = pos + bytesRead;
39 return bytesRead;
40 }
41 private string s;
42 private long pos;
43 }
44
45 public class ConsoleWriter : Writer
46 {
47 public ConsoleWriter() : standardOutputStream(stdout)
48 {
49 }
50 public void Write(byte* buf, long count)
51 {
52 standardOutputStream.Write(buf, count);
53 }
54 private FileByteStream standardOutputStream;
55 }
56
57 void main()
58 {
59 StringReader stringReader("Nothing is impossible, the word itself says 'I'm possible'!\n");
60 ConsoleWriter consoleWriter;
61 Copy(stringReader, consoleWriter);
62 }