Initial Commit
This commit is contained in:
47
iTechSharp/srcbc/util/io/BaseInputStream.cs
Normal file
47
iTechSharp/srcbc/util/io/BaseInputStream.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.IO
|
||||
{
|
||||
public abstract class BaseInputStream : Stream
|
||||
{
|
||||
private bool closed;
|
||||
|
||||
public sealed override bool CanRead { get { return !closed; } }
|
||||
public sealed override bool CanSeek { get { return false; } }
|
||||
public sealed override bool CanWrite { get { return false; } }
|
||||
public override void Close() { closed = true; }
|
||||
public sealed override void Flush() {}
|
||||
public sealed override long Length { get { throw new NotSupportedException(); } }
|
||||
public sealed override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int pos = offset;
|
||||
try
|
||||
{
|
||||
int end = offset + count;
|
||||
while (pos < end)
|
||||
{
|
||||
int b = ReadByte();
|
||||
if (b == -1) break;
|
||||
buffer[pos++] = (byte) b;
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
if (pos == offset) throw;
|
||||
}
|
||||
return pos - offset;
|
||||
}
|
||||
|
||||
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
|
||||
public sealed override void SetLength(long value) { throw new NotSupportedException(); }
|
||||
public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
|
||||
}
|
||||
}
|
47
iTechSharp/srcbc/util/io/BaseOutputStream.cs
Normal file
47
iTechSharp/srcbc/util/io/BaseOutputStream.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.IO
|
||||
{
|
||||
public abstract class BaseOutputStream : Stream
|
||||
{
|
||||
private bool closed;
|
||||
|
||||
public sealed override bool CanRead { get { return false; } }
|
||||
public sealed override bool CanSeek { get { return false; } }
|
||||
public sealed override bool CanWrite { get { return !closed; } }
|
||||
public override void Close() { closed = true; }
|
||||
public override void Flush() {}
|
||||
public sealed override long Length { get { throw new NotSupportedException(); } }
|
||||
public sealed override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
public sealed override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
|
||||
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
|
||||
public sealed override void SetLength(long value) { throw new NotSupportedException(); }
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
Debug.Assert(buffer != null);
|
||||
Debug.Assert(0 <= offset && offset <= buffer.Length);
|
||||
Debug.Assert(count >= 0);
|
||||
|
||||
int end = offset + count;
|
||||
|
||||
Debug.Assert(0 <= end && end <= buffer.Length);
|
||||
|
||||
for (int i = offset; i < end; ++i)
|
||||
{
|
||||
this.WriteByte(buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Write(params byte[] buffer)
|
||||
{
|
||||
Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
}
|
52
iTechSharp/srcbc/util/io/PushbackStream.cs
Normal file
52
iTechSharp/srcbc/util/io/PushbackStream.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Org.BouncyCastle.Asn1.Utilities;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.IO
|
||||
{
|
||||
public class PushbackStream
|
||||
: FilterStream
|
||||
{
|
||||
private int buf = -1;
|
||||
|
||||
public PushbackStream(
|
||||
Stream s)
|
||||
: base(s)
|
||||
{
|
||||
}
|
||||
|
||||
public override int ReadByte()
|
||||
{
|
||||
if (buf != -1)
|
||||
{
|
||||
int tmp = buf;
|
||||
buf = -1;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
return base.ReadByte();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
if (buf != -1 && count > 0)
|
||||
{
|
||||
// TODO Can this case be made more efficient?
|
||||
buffer[offset] = (byte) buf;
|
||||
buf = -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return base.Read(buffer, offset, count);
|
||||
}
|
||||
|
||||
public virtual void Unread(int b)
|
||||
{
|
||||
if (buf != -1)
|
||||
throw new InvalidOperationException("Can only push back one byte");
|
||||
|
||||
buf = b & 0xFF;
|
||||
}
|
||||
}
|
||||
}
|
57
iTechSharp/srcbc/util/io/Streams.cs
Normal file
57
iTechSharp/srcbc/util/io/Streams.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Org.BouncyCastle.Utilities.IO
|
||||
{
|
||||
public sealed class Streams
|
||||
{
|
||||
private const int BufferSize = 512;
|
||||
|
||||
private Streams()
|
||||
{
|
||||
}
|
||||
|
||||
public static void Drain(Stream inStr)
|
||||
{
|
||||
byte[] bs = new byte[BufferSize];
|
||||
while (inStr.Read(bs, 0, bs.Length) > 0)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] ReadAll(Stream inStr)
|
||||
{
|
||||
MemoryStream buf = new MemoryStream();
|
||||
PipeAll(inStr, buf);
|
||||
return buf.ToArray();
|
||||
}
|
||||
|
||||
public static int ReadFully(Stream inStr, byte[] buf)
|
||||
{
|
||||
return ReadFully(inStr, buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public static int ReadFully(Stream inStr, byte[] buf, int off, int len)
|
||||
{
|
||||
int totalRead = 0;
|
||||
while (totalRead < len)
|
||||
{
|
||||
int numRead = inStr.Read(buf, off + totalRead, len - totalRead);
|
||||
if (numRead < 1)
|
||||
break;
|
||||
totalRead += numRead;
|
||||
}
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
public static void PipeAll(Stream inStr, Stream outStr)
|
||||
{
|
||||
byte[] bs = new byte[BufferSize];
|
||||
int numRead;
|
||||
while ((numRead = inStr.Read(bs, 0, bs.Length)) > 0)
|
||||
{
|
||||
outStr.Write(bs, 0, numRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user