summary refs log tree commit diff
path: root/crypto/src/tls/ByteQueueInputStream.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2021-07-12 15:15:36 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2021-07-12 15:15:36 +0700
commit68c795fe81277f73aeb90d8ad4c6f4305f32c906 (patch)
tree59643344aafef91bbd4c4a3a7973deba3d837a00 /crypto/src/tls/ByteQueueInputStream.cs
parentTLS test tweaks (diff)
downloadBouncyCastle.NET-ed25519-68c795fe81277f73aeb90d8ad4c6f4305f32c906.tar.xz
Port of new TLS API from bc-java
Diffstat (limited to 'crypto/src/tls/ByteQueueInputStream.cs')
-rw-r--r--crypto/src/tls/ByteQueueInputStream.cs72
1 files changed, 72 insertions, 0 deletions
diff --git a/crypto/src/tls/ByteQueueInputStream.cs b/crypto/src/tls/ByteQueueInputStream.cs
new file mode 100644
index 000000000..b59b5d1e7
--- /dev/null
+++ b/crypto/src/tls/ByteQueueInputStream.cs
@@ -0,0 +1,72 @@
+using System;
+
+using Org.BouncyCastle.Utilities.IO;
+
+namespace Org.BouncyCastle.Tls
+{
+    public sealed class ByteQueueInputStream
+        : BaseInputStream
+    {
+        private readonly ByteQueue m_buffer;
+
+        public ByteQueueInputStream()
+        {
+            this.m_buffer = new ByteQueue();
+        }
+
+        public void AddBytes(byte[] buf)
+        {
+            m_buffer.AddData(buf, 0, buf.Length);
+        }
+
+        public void AddBytes(byte[] buf, int bufOff, int bufLen)
+        {
+            m_buffer.AddData(buf, bufOff, bufLen);
+        }
+
+        public int Peek(byte[] buf)
+        {
+            int bytesToRead = System.Math.Min(m_buffer.Available, buf.Length);
+            m_buffer.Read(buf, 0, bytesToRead, 0);
+            return bytesToRead;
+        }
+
+        public override int ReadByte()
+        {
+            if (m_buffer.Available == 0)
+                return -1;
+
+            return m_buffer.RemoveData(1, 0)[0];
+        }
+
+        public override int Read(byte[] buf, int off, int len)
+        {
+            int bytesToRead = System.Math.Min(m_buffer.Available, len);
+            m_buffer.RemoveData(buf, off, bytesToRead, 0);
+            return bytesToRead;
+        }
+
+        public long Skip(long n)
+        {
+            int bytesToRemove = System.Math.Min((int)n, m_buffer.Available);
+            m_buffer.RemoveData(bytesToRemove);
+            return bytesToRemove;
+        }
+
+        public int Available
+        {
+            get { return m_buffer.Available; }
+        }
+
+#if PORTABLE
+        //protected override void Dispose(bool disposing)
+        //{
+        //    base.Dispose(disposing);
+        //}
+#else
+        public override void Close()
+        {
+        }
+#endif
+    }
+}