BcpgOutputStream.cs :  » PDF » iTextSharp » Org » BouncyCastle » Bcpg » 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 » Bcpg » BcpgOutputStream.cs
using System;
using System.IO;

using Org.BouncyCastle.Utilities.IO;

namespace Org.BouncyCastle.Bcpg{
  /// <remarks>Basic output stream.</remarks>
    public class BcpgOutputStream
        : BaseOutputStream
    {
    internal static BcpgOutputStream Wrap(
      Stream outStr)
    {
      if (outStr is BcpgOutputStream)
      {
        return (BcpgOutputStream) outStr;
      }

      return new BcpgOutputStream(outStr);
    }

    private Stream outStr;
        private byte[] partialBuffer;
        private int partialBufferLength;
        private int partialPower;
        private int partialOffset;
        private const int BufferSizePower = 16; // 2^16 size buffer on long files

    /// <summary>Create a stream representing a general packet.</summary>
    /// <param name="outStr">Output stream to write to.</param>
    public BcpgOutputStream(
            Stream outStr)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

      this.outStr = outStr;
        }

    /// <summary>Create a stream representing an old style partial object.</summary>
    /// <param name="outStr">Output stream to write to.</param>
    /// <param name="tag">The packet tag for the object.</param>
        public BcpgOutputStream(
            Stream    outStr,
            PacketTag  tag)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

      this.outStr = outStr;
            this.WriteHeader(tag, true, true, 0);
        }

    /// <summary>Create a stream representing a general packet.</summary>
    /// <param name="outStr">Output stream to write to.</param>
    /// <param name="tag">Packet tag.</param>
    /// <param name="length">Size of chunks making up the packet.</param>
    /// <param name="oldFormat">If true, the header is written out in old format.</param>
    public BcpgOutputStream(
            Stream    outStr,
            PacketTag  tag,
            long    length,
            bool    oldFormat)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

      this.outStr = outStr;

            if (length > 0xFFFFFFFFL)
            {
                this.WriteHeader(tag, false, true, 0);
                this.partialBufferLength = 1 << BufferSizePower;
                this.partialBuffer = new byte[partialBufferLength];
        this.partialPower = BufferSizePower;
        this.partialOffset = 0;
            }
            else
            {
                this.WriteHeader(tag, oldFormat, false, length);
            }
        }

    /// <summary>Create a new style partial input stream buffered into chunks.</summary>
    /// <param name="outStr">Output stream to write to.</param>
    /// <param name="tag">Packet tag.</param>
    /// <param name="length">Size of chunks making up the packet.</param>
    public BcpgOutputStream(
            Stream    outStr,
            PacketTag  tag,
            long    length)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

            this.outStr = outStr;
            this.WriteHeader(tag, false, false, length);
        }

    /// <summary>Create a new style partial input stream buffered into chunks.</summary>
    /// <param name="outStr">Output stream to write to.</param>
    /// <param name="tag">Packet tag.</param>
    /// <param name="buffer">Buffer to use for collecting chunks.</param>
        public BcpgOutputStream(
            Stream    outStr,
            PacketTag  tag,
            byte[]    buffer)
        {
      if (outStr == null)
        throw new ArgumentNullException("outStr");

            this.outStr = outStr;
            this.WriteHeader(tag, false, true, 0);

      this.partialBuffer = buffer;

      uint length = (uint) partialBuffer.Length;
            for (partialPower = 0; length != 1; partialPower++)
            {
                length >>= 1;
            }

      if (partialPower > 30)
            {
                throw new IOException("Buffer cannot be greater than 2^30 in length.");
            }
            this.partialBufferLength = 1 << partialPower;
            this.partialOffset = 0;
        }

    private void WriteNewPacketLength(
            long bodyLen)
        {
            if (bodyLen < 192)
            {
                outStr.WriteByte((byte)bodyLen);
            }
            else if (bodyLen <= 8383)
            {
                bodyLen -= 192;

                outStr.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
                outStr.WriteByte((byte)bodyLen);
            }
            else
            {
                outStr.WriteByte(0xff);
                outStr.WriteByte((byte)(bodyLen >> 24));
                outStr.WriteByte((byte)(bodyLen >> 16));
                outStr.WriteByte((byte)(bodyLen >> 8));
                outStr.WriteByte((byte)bodyLen);
            }
        }

        private void WriteHeader(
            PacketTag  tag,
            bool    oldPackets,
            bool    partial,
            long    bodyLen)
        {
            int hdr = 0x80;

            if (partialBuffer != null)
            {
                PartialFlush(true);
                partialBuffer = null;
            }

            if (oldPackets)
            {
                hdr |= ((int) tag) << 2;

                if (partial)
                {
                    this.WriteByte((byte)(hdr | 0x03));
                }
                else
                {
                    if (bodyLen <= 0xff)
                    {
                        this.WriteByte((byte) hdr);
                        this.WriteByte((byte)bodyLen);
                    }
                    else if (bodyLen <= 0xffff)
                    {
                        this.WriteByte((byte)(hdr | 0x01));
                        this.WriteByte((byte)(bodyLen >> 8));
                        this.WriteByte((byte)(bodyLen));
                    }
                    else
                    {
                        this.WriteByte((byte)(hdr | 0x02));
                        this.WriteByte((byte)(bodyLen >> 24));
                        this.WriteByte((byte)(bodyLen >> 16));
                        this.WriteByte((byte)(bodyLen >> 8));
                        this.WriteByte((byte)bodyLen);
                    }
                }
            }
            else
            {
                hdr |= 0x40 | (int) tag;
                this.WriteByte((byte) hdr);

                if (partial)
                {
                    partialOffset = 0;
                }
                else
                {
                    this.WriteNewPacketLength(bodyLen);
                }
            }
        }

        private void PartialFlush(
            bool isLast)
        {
            if (isLast)
            {
                WriteNewPacketLength(partialOffset);
                outStr.Write(partialBuffer, 0, partialOffset);
            }
            else
            {
                outStr.WriteByte((byte)(0xE0 | partialPower));
                outStr.Write(partialBuffer, 0, partialBufferLength);
            }

            partialOffset = 0;
        }

    private void WritePartial(
            byte b)
        {
            if (partialOffset == partialBufferLength)
            {
                PartialFlush(false);
            }

      partialBuffer[partialOffset++] = b;
        }

    private void WritePartial(
            byte[]  buffer,
            int    off,
            int    len)
        {
            if (partialOffset == partialBufferLength)
            {
                PartialFlush(false);
            }

            if (len <= (partialBufferLength - partialOffset))
            {
                Array.Copy(buffer, off, partialBuffer, partialOffset, len);
                partialOffset += len;
            }
            else
            {
                int diff = partialBufferLength - partialOffset;
                Array.Copy(buffer, off, partialBuffer, partialOffset, diff);
                off += diff;
                len -= diff;
                PartialFlush(false);
                while (len > partialBufferLength)
                {
                    Array.Copy(buffer, off, partialBuffer, 0, partialBufferLength);
                    off += partialBufferLength;
                    len -= partialBufferLength;
                    PartialFlush(false);
                }
                Array.Copy(buffer, off, partialBuffer, 0, len);
                partialOffset += len;
            }
        }
        public override void WriteByte(
      byte value)
        {
            if (partialBuffer != null)
            {
                WritePartial(value);
            }
            else
            {
                outStr.WriteByte(value);
            }
        }
        public override void Write(
            byte[]  buffer,
            int    offset,
            int    count)
        {
            if (partialBuffer != null)
            {
                WritePartial(buffer, offset, count);
            }
            else
            {
                outStr.Write(buffer, offset, count);
            }
        }

    // Additional helper methods to write primitive types
    internal virtual void WriteShort(
      short n)
    {
      this.Write(
        (byte)(n >> 8),
        (byte)n);
    }
    internal virtual void WriteInt(
      int n)
    {
      this.Write(
        (byte)(n >> 24),
        (byte)(n >> 16),
        (byte)(n >> 8),
        (byte)n);
    }
    internal virtual void WriteLong(
      long n)
    {
      this.Write(
        (byte)(n >> 56),
        (byte)(n >> 48),
        (byte)(n >> 40),
        (byte)(n >> 32),
        (byte)(n >> 24),
        (byte)(n >> 16),
        (byte)(n >> 8),
        (byte)n);
    }

    public void WritePacket(
            ContainedPacket p)
        {
            p.Encode(this);
        }

        internal void WritePacket(
            PacketTag  tag,
            byte[]    body,
            bool    oldFormat)
        {
            this.WriteHeader(tag, oldFormat, false, body.Length);
            this.Write(body);
        }

    public void WriteObject(
            BcpgObject bcpgObject)
        {
            bcpgObject.Encode(this);
        }

    public void WriteObjects(
      params BcpgObject[] v)
    {
      foreach (BcpgObject o in v)
      {
        o.Encode(this);
      }
    }

    /// <summary>Flush the underlying stream.</summary>
        public override void Flush()
        {
            outStr.Flush();
        }

    /// <summary>Finish writing out the current packet without closing the underlying stream.</summary>
        public void Finish()
        {
            if (partialBuffer != null)
            {
                PartialFlush(true);
                partialBuffer = null;
            }
        }

    public override void Close()
        {
      this.Finish();
      outStr.Flush();
      outStr.Close();
      base.Close();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.