1 // =================================
 2 // Copyright (c) 2022 Seppo Laakko
 3 // Distributed under the MIT license
 4 // =================================
 5 
 6 using System;
 7 
 8 namespace System.IO
 9 {
10     public abstract class Stream
11     {
12         public virtual default ~Stream();
13         suppress Stream(const Stream&);
14         suppress void operator=(const Stream&);
15         public default nothrow Stream(Stream&&);
16         public default nothrow void operator=(Stream&&);
17         public abstract int ReadByte();
18         public abstract long Read(byte* buflong count);
19         public abstract long Write(byte x);
20         public abstract long Write(byte* buflong count);
21         public virtual void Flush()
22         {
23         }
24         public virtual long Seek(long posOrigin origin)
25         {
26             string streamClassName = typename(*this);
27             throw SystemError(EBADFstreamClassName + " stream does not support seek");
28         }
29         public virtual long Tell()
30         {
31             string streamClassName = typename(*this);
32             throw SystemError(EBADFstreamClassName + " stream does not support tell");
33         }
34         public virtual bool IsHostTextFile() const
35         {
36             return false;
37         }
38         public virtual bool IsConsole() const
39         {
40             return false;
41         }
42         public void CopyTo(Stream& destination)
43         {
44             this->CopyTo(destination4096);
45         }
46         public void CopyTo(Stream& destinationlong bufferSize)
47         {
48             if (bufferSize <= 0)
49             {
50                 ThrowInvalidParameterException();
51             }
52             Buffer buffer(bufferSize);
53             long bytesRead = 0;
54             do
55             {
56                 bytesRead = Read(buffer.Mem()bufferSize);
57                 if (bytesRead > 0)
58                 {
59                     long count = bytesRead;
60                     long offset = 0;
61                     do
62                     {
63                         long bytesWritten = destination.Write(buffer.Mem() + offsetcount);
64                         if (bytesWritten <= 0)
65                         {
66                             throw SystemError(EFAIL"stream write failed");
67                         }
68                         count = count - bytesWritten;
69                         offset = offset + bytesWritten;
70                     }
71                     while (count > 0);
72                 }
73             }
74             while (bytesRead > 0);
75         }
76     }
77 }