ByteQueue.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Tls » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Tls » ByteQueue.cs
using System;

namespace Org.BouncyCastle.Crypto.Tls{
  /// <remarks>
  /// A queue for bytes.
  /// <p>
  /// This file could be more optimized.
  /// </p>
  /// </remarks>
  public class ByteQueue
  {
    /// <returns>The smallest number which can be written as 2^x which is bigger than i.</returns>
    public static int NextTwoPow(
      int i)
    {
      /*
      * This code is based of a lot of code I found on the Internet
      * which mostly referenced a book called "Hacking delight".
      * 
      */
      i |= (i >> 1);
      i |= (i >> 2);
      i |= (i >> 4);
      i |= (i >> 8);
      i |= (i >> 16);
      return i + 1;
    }

    /**
     * The initial size for our buffer.
     */
    private const int InitBufSize = 1024;

    /**
     * The buffer where we store our data.
     */
    private byte[] databuf = new byte[ByteQueue.InitBufSize];

    /**
     * How many bytes at the beginning of the buffer are skipped.
     */
    private int skipped = 0;

    /**
     * How many bytes in the buffer are valid data.
     */
    private int available = 0;

    /// <summary>Read data from the buffer.</summary>
    /// <param name="buf">The buffer where the read data will be copied to.</param>
    /// <param name="offset">How many bytes to skip at the beginning of buf.</param>
    /// <param name="len">How many bytes to read at all.</param>
    /// <param name="skip">How many bytes from our data to skip.</param>
    public void Read(
      byte[]  buf,
      int    offset,
      int    len,
      int    skip)
    {
      if ((available - skip) < len)
      {
        throw new TlsException("Not enough data to read");
      }
      if ((buf.Length - offset) < len)
      {
        throw new TlsException("Buffer size of " + buf.Length + " is too small for a read of " + len + " bytes");
      }
      Array.Copy(databuf, skipped + skip, buf, offset, len);
    }

    /// <summary>Add some data to our buffer.</summary>
    /// <param name="data">A byte-array to read data from.</param>
    /// <param name="offset">How many bytes to skip at the beginning of the array.</param>
    /// <param name="len">How many bytes to read from the array.</param>
    public void AddData(
      byte[]  data,
      int    offset,
      int    len)
    {
      if ((skipped + available + len) > databuf.Length)
      {
        byte[] tmp = new byte[ByteQueue.NextTwoPow(data.Length)];
        Array.Copy(databuf, skipped, tmp, 0, available);
        skipped = 0;
        databuf = tmp;
      }
      Array.Copy(data, offset, databuf, skipped + available, len);
      available += len;
    }

    /// <summary>Remove some bytes from our data from the beginning.</summary>
    /// <param name="i">How many bytes to remove.</param>
    public void RemoveData(
      int i)
    {
      if (i > available)
      {
        throw new TlsException("Cannot remove " + i + " bytes, only got " + available);
      }

      /*
      * Skip the data.
      */
      available -= i;
      skipped += i;

      /*
      * If more than half of our data is skipped, we will move the data
      * in the buffer.
      */
      if (skipped > (databuf.Length / 2))
      {
        Array.Copy(databuf, skipped, databuf, 0, available);
        skipped = 0;
      }
    }

    /// <summary>The number of bytes which are available in this buffer.</summary>
    public int Available
    {
      get { return available; }
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.