/**
        * Generate an object that contains an CMS Compressed Data
        */
        public CmsCompressedData Generate(
            CmsProcessable	content,
            string			compressionOid)
        {
            AlgorithmIdentifier comAlgId;
            Asn1OctetString comOcts;

            try
            {
                MemoryStream bOut = new MemoryStream();
                ZDeflaterOutputStream zOut = new ZDeflaterOutputStream(bOut);

				content.Write(zOut);

				zOut.Close();

				comAlgId = new AlgorithmIdentifier(
					new DerObjectIdentifier(compressionOid),
					null);

				comOcts = new BerOctetString(bOut.ToArray());
            }
            catch (IOException e)
            {
                throw new CmsException("exception encoding data.", e);
            }

            ContentInfo comContent = new ContentInfo(CmsObjectIdentifiers.Data, comOcts);
            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.CompressedData,
                new CompressedData(comAlgId, comContent));

			return new CmsCompressedData(contentInfo);
        }
 internal CmsCompressedOutputStream(
     ZDeflaterOutputStream outStream,
     BerSequenceGenerator sGen,
     BerSequenceGenerator cGen,
     BerSequenceGenerator eiGen)
 {
     _out = outStream;
     _sGen = sGen;
     _cGen = cGen;
     _eiGen = eiGen;
 }
Exemple #3
0
        public override void ToPdf(PdfWriter writer, Stream os)
        {
            if (inputStream != null && compressed)
                Put(PdfName.FILTER, PdfName.FLATEDECODE);
            PdfEncryption crypto = null;
            if (writer != null)
                crypto = writer.Encryption;
            if (crypto != null) {
                PdfObject filter = Get(PdfName.FILTER);
                if (filter != null) {
                    if (PdfName.CRYPT.Equals(filter))
                        crypto = null;
                    else if (filter.IsArray()) {
                        PdfArray a = ((PdfArray)filter);
                        if (a.Size > 0 && PdfName.CRYPT.Equals(a[0]))
                            crypto = null;
                    }
                }
            }
            PdfObject nn = Get(PdfName.LENGTH);
            if (crypto != null && nn != null && nn.IsNumber()) {
                int sz = ((PdfNumber)nn).IntValue;
                Put(PdfName.LENGTH, new PdfNumber(crypto.CalculateStreamSize(sz)));
                SuperToPdf(writer, os);
                Put(PdfName.LENGTH, nn);
            }
            else
                SuperToPdf(writer, os);
            os.Write(STARTSTREAM, 0, STARTSTREAM.Length);
            if (inputStream != null) {
                rawLength = 0;
                ZDeflaterOutputStream def = null;
                OutputStreamCounter osc = new OutputStreamCounter(os);
                OutputStreamEncryption ose = null;
                Stream fout = osc;
                if (crypto != null && !crypto.IsEmbeddedFilesOnly())
                    fout = ose = crypto.GetEncryptionStream(fout);
                if (compressed)
                    fout = def = new ZDeflaterOutputStream(fout, compressionLevel);

                byte[] buf = new byte[4192];
                while (true) {
                    int n = inputStream.Read(buf, 0, buf.Length);
                    if (n <= 0)
                        break;
                    fout.Write(buf, 0, n);
                    rawLength += n;
                }
                if (def != null)
                    def.Finish();
                if (ose != null)
                    ose.Finish();
                inputStreamLength = osc.Counter;
            }
            else {
                if (crypto != null && !crypto.IsEmbeddedFilesOnly()) {
                    byte[] b;
                    if (streamBytes != null) {
                        b = crypto.EncryptByteArray(streamBytes.ToArray());
                    }
                    else {
                        b = crypto.EncryptByteArray(bytes);
                    }
                    os.Write(b, 0, b.Length);
                }
                else {
                    if (streamBytes != null)
                        streamBytes.WriteTo(os);
                    else
                        os.Write(bytes, 0, bytes.Length);
                }
            }
            os.Write(ENDSTREAM, 0, ENDSTREAM.Length);
        }
Exemple #4
0
 /**
  * Sets the data associated with the stream, either compressed or
  * uncompressed. Note that the data will never be compressed if
  * Document.compress is set to false.
  *
  * @param data raw data, decrypted and uncompressed.
  * @param compress true if you want the stream to be compresssed.
  * @param compressionLevel  a value between -1 and 9 (ignored if compress == false)
  * @since   iText 2.1.3
  */
 public void SetData(byte[] data, bool compress, int compressionLevel)
 {
     Remove(PdfName.FILTER);
     this.offset = -1;
     if (Document.Compress && compress) {
     MemoryStream stream = new MemoryStream();
     ZDeflaterOutputStream zip = new ZDeflaterOutputStream(stream, compressionLevel);
     zip.Write(data, 0, data.Length);
     zip.Close();
     bytes = stream.ToArray();
     this.compressionLevel = compressionLevel;
     Put(PdfName.FILTER, PdfName.FLATEDECODE);
     }
     else
     bytes = data;
     Length = bytes.Length;
 }