~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/OutputStream.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Sharpen
 
2
{
 
3
        using System;
 
4
        using System.IO;
 
5
 
 
6
        public class OutputStream : IDisposable
 
7
        {
 
8
                protected Stream Wrapped;
 
9
 
 
10
                public static implicit operator OutputStream (Stream s)
 
11
                {
 
12
                        return Wrap (s);
 
13
                }
 
14
 
 
15
                public static implicit operator Stream (OutputStream s)
 
16
                {
 
17
                        return s.GetWrappedStream ();
 
18
                }
 
19
                
 
20
                public virtual void Close ()
 
21
                {
 
22
                        if (this.Wrapped != null) {
 
23
                                this.Wrapped.Close ();
 
24
                        }
 
25
                }
 
26
 
 
27
                public void Dispose ()
 
28
                {
 
29
                        this.Close ();
 
30
                }
 
31
 
 
32
                public virtual void Flush ()
 
33
                {
 
34
                        if (this.Wrapped != null) {
 
35
                                this.Wrapped.Flush ();
 
36
                        }
 
37
                }
 
38
 
 
39
                internal Stream GetWrappedStream ()
 
40
                {
 
41
                        // Always create a wrapper stream (not directly Wrapped) since the subclass
 
42
                        // may be overriding methods that need to be called when used through the Stream class
 
43
                        return new WrappedSystemStream (this);
 
44
                }
 
45
 
 
46
                static internal OutputStream Wrap (Stream s)
 
47
                {
 
48
                        OutputStream stream = new OutputStream ();
 
49
                        stream.Wrapped = s;
 
50
                        return stream;
 
51
                }
 
52
 
 
53
                public virtual void Write (int b)
 
54
                {
 
55
                        if (Wrapped is WrappedSystemStream)
 
56
                                ((WrappedSystemStream)Wrapped).OutputStream.Write (b);
 
57
                        else {
 
58
                                if (this.Wrapped == null)
 
59
                                        throw new NotImplementedException ();
 
60
                                this.Wrapped.WriteByte ((byte)b);
 
61
                        }
 
62
                }
 
63
 
 
64
                public virtual void Write (byte[] b)
 
65
                {
 
66
                        this.Write (b, 0, b.Length);
 
67
                }
 
68
 
 
69
                public virtual void Write (byte[] b, int offset, int len)
 
70
                {
 
71
                        if (Wrapped is WrappedSystemStream)
 
72
                                ((WrappedSystemStream)Wrapped).OutputStream.Write (b, offset, len);
 
73
                        else {
 
74
                                if (this.Wrapped != null) {
 
75
                                        this.Wrapped.Write (b, offset, len);
 
76
                                } else {
 
77
                                        for (int i = 0; i < len; i++) {
 
78
                                                this.Write (b[i + offset]);
 
79
                                        }
 
80
                                }
 
81
                        }
 
82
                }
 
83
        }
 
84
}