jellyfin/Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/OutputStream.cs

87 lines
1.7 KiB
C#
Raw Normal View History

2017-04-01 17:36:06 -07:00
using System;
using System.IO;
namespace SharpCifs.Util.Sharpen
{
public class OutputStream : IDisposable
2017-07-07 20:12:21 -07:00
{
protected Stream Wrapped;
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public static implicit operator OutputStream (Stream s)
{
return Wrap (s);
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public static implicit operator Stream (OutputStream s)
{
return s.GetWrappedStream ();
}
public virtual void Close ()
{
if (Wrapped != null) {
2017-04-01 17:36:06 -07:00
//Stream.`Close` method deleted
//Wrapped.Close ();
Wrapped.Dispose();
}
2017-07-07 20:12:21 -07:00
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public void Dispose ()
{
Close ();
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public virtual void Flush ()
{
if (Wrapped != null) {
Wrapped.Flush ();
}
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
internal Stream GetWrappedStream ()
{
// Always create a wrapper stream (not directly Wrapped) since the subclass
// may be overriding methods that need to be called when used through the Stream class
return new WrappedSystemStream (this);
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
static internal OutputStream Wrap (Stream s)
{
OutputStream stream = new OutputStream ();
stream.Wrapped = s;
return stream;
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public virtual void Write (int b)
{
if (Wrapped is WrappedSystemStream)
((WrappedSystemStream)Wrapped).OutputStream.Write (b);
else {
if (Wrapped == null)
throw new NotImplementedException ();
Wrapped.WriteByte ((byte)b);
}
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public virtual void Write (byte[] b)
{
Write (b, 0, b.Length);
}
2017-04-01 17:36:06 -07:00
2017-07-07 20:12:21 -07:00
public virtual void Write (byte[] b, int offset, int len)
{
if (Wrapped is WrappedSystemStream)
((WrappedSystemStream)Wrapped).OutputStream.Write (b, offset, len);
else {
if (Wrapped != null) {
Wrapped.Write (b, offset, len);
} else {
for (int i = 0; i < len; i++) {
Write (b[i + offset]);
}
}
}
}
}
2017-04-01 17:36:06 -07:00
}