protected internal virtual void InitOutputStream(Stream stream) { if (GetOutputStream() == null && inputStream == null) { outputStream = new PdfOutputStream(stream != null ? stream : new ByteArrayOutputStream()); } }
/// <summary>This constructor is for reusing ByteArrayOutputStreams of indexStream and outputStream.</summary> /// <remarks> /// This constructor is for reusing ByteArrayOutputStreams of indexStream and outputStream. /// NOTE Only for internal use in PdfWriter! /// </remarks> /// <param name="prev">previous PdfObjectStream.</param> internal PdfObjectStream(iText.Kernel.Pdf.PdfObjectStream prev) : this(prev.GetIndirectReference().GetDocument(), prev.GetOutputStream().GetOutputStream()) { indexStream = new PdfOutputStream(prev.indexStream.GetOutputStream()); ((ByteArrayOutputStream)outputStream.GetOutputStream()).JReset(); ((ByteArrayOutputStream)indexStream.GetOutputStream()).JReset(); prev.ReleaseContent(true); }
private void ReleaseContent(bool close) { if (close) { outputStream = null; indexStream = null; base.ReleaseContent(); } }
/// <summary>Sets or appends <code>bytes</code> to stream content.</summary> /// <remarks> /// Sets or appends <code>bytes</code> to stream content. /// Could not be used with streams which were created by <code>InputStream</code>. /// </remarks> /// <param name="bytes"> /// New content for stream. These bytes are considered to be a raw data (i.e. not encoded/compressed/encrypted) /// and if it's not true, the corresponding filters shall be set to the PdfStream object manually. /// Data compression generally should be configured via /// <see cref="SetCompressionLevel(int)"/> /// and /// is handled on stream writing to the output document. /// If <code>null</code> and <code>append</code> is false then stream's content will be discarded. /// </param> /// <param name="append"> /// If set to true then <code>bytes</code> will be appended to the end, /// rather then replace original content. The original content will be decoded if needed. /// </param> public virtual void SetData(byte[] bytes, bool append) { if (IsFlushed()) { throw new PdfException(PdfException.CannotOperateWithFlushedPdfStream); } if (inputStream != null) { throw new PdfException(PdfException.CannotSetDataToPdfstreamWhichWasCreatedByInputStream); } bool outputStreamIsUninitialized = outputStream == null; if (outputStreamIsUninitialized) { outputStream = new PdfOutputStream(new ByteArrayOutputStream()); } if (append) { if (outputStreamIsUninitialized && GetIndirectReference() != null && GetIndirectReference().GetReader() != null || !outputStreamIsUninitialized && ContainsKey(PdfName.Filter)) { // here is the same as in the getBytes() method: this logic makes sense only when stream is created // by reader and in this case indirect reference won't be null and stream is not in the MustBeIndirect state. byte[] oldBytes; try { oldBytes = GetBytes(); } catch (PdfException ex) { throw new PdfException(PdfException.CannotReadAStreamInOrderToAppendNewBytes, ex); } outputStream.AssignBytes(oldBytes, oldBytes.Length); } if (bytes != null) { outputStream.WriteBytes(bytes); } } else { if (bytes != null) { outputStream.AssignBytes(bytes, bytes.Length); } else { outputStream.Reset(); } } offset = 0; // Bytes that are set shall be not encoded, and moreover the existing bytes in cases of the appending are decoded, // therefore all filters shall be removed. Compression will be handled on stream flushing. Remove(PdfName.Filter); Remove(PdfName.DecodeParms); }
/// <summary>This constructor is for reusing ByteArrayOutputStreams of indexStream and outputStream.</summary> /// <remarks> /// This constructor is for reusing ByteArrayOutputStreams of indexStream and outputStream. /// NOTE Only for internal use in PdfWriter! /// </remarks> /// <param name="prev">previous PdfObjectStream.</param> internal PdfObjectStream(iText.Kernel.Pdf.PdfObjectStream prev) : this(prev.GetIndirectReference().GetDocument()) { ByteArrayOutputStream prevOutputStream = (ByteArrayOutputStream)prev.GetOutputStream().GetOutputStream(); prevOutputStream.JReset(); InitOutputStream(prevOutputStream); ByteArrayOutputStream prevIndexStream = ((ByteArrayOutputStream)indexStream.GetOutputStream()); prevIndexStream.JReset(); indexStream = new PdfOutputStream(prevIndexStream); }
private void ReleaseContent(bool close) { if (close) { base.ReleaseContent(); try { indexStream.Close(); } catch (System.IO.IOException e) { throw new PdfException(PdfException.IoException, e); } indexStream = null; } }
/// <summary>Release content of PdfStream.</summary> protected internal override void ReleaseContent() { base.ReleaseContent(); try { if (outputStream != null) { outputStream.Dispose(); outputStream = null; } } catch (System.IO.IOException e) { throw new PdfException(PdfException.IoException, e); } }
/// <summary> /// Constructs a /// <c>PdfStream</c> /// -object. /// </summary> /// <param name="bytes"> /// initial content of /// <see cref="PdfOutputStream"/>. /// </param> /// <param name="compressionLevel">the compression level (0 = best speed, 9 = best compression, -1 is default) /// </param> public PdfStream(byte[] bytes, int compressionLevel) : base() { SetState(MUST_BE_INDIRECT); this.compressionLevel = compressionLevel; if (bytes != null && bytes.Length > 0) { this.outputStream = new PdfOutputStream(new ByteArrayOutputStream(bytes.Length)); this.outputStream.WriteBytes(bytes); } else { this.outputStream = new PdfOutputStream(new ByteArrayOutputStream()); } }
/// <summary>Sets or appends <code>bytes</code> to stream content.</summary> /// <remarks> /// Sets or appends <code>bytes</code> to stream content. /// Could not be used with streams which were created by <code>InputStream</code>. /// </remarks> /// <param name="bytes"> /// new content for stream; if <code>null</code> and <code>append</code> is false then /// stream's content will be discarded /// </param> /// <param name="append"> /// if set to true then <code>bytes</code> will be appended to the end, /// rather then replace original content /// </param> public virtual void SetData(byte[] bytes, bool append) { if (inputStream != null) { throw new PdfException(PdfException.CannotSetDataToPdfstreamWhichWasCreatedByInputstream); } bool outputStreamIsUninitialized = outputStream == null; if (outputStreamIsUninitialized) { outputStream = new PdfOutputStream(new ByteArrayOutputStream()); } if (append) { if (outputStreamIsUninitialized && GetIndirectReference() != null && GetIndirectReference().GetReader() != null) { // here is the same as in the getBytes() method: this logic makes sense only when stream is created // by reader and in this case indirect reference won't be null and stream is not in the MustBeIndirect state. byte[] oldBytes; try { oldBytes = GetBytes(); } catch (PdfException ex) { throw new PdfException(PdfException.CannotReadAStreamInOrderToAppendNewBytes, ex); } offset = 0; outputStream.WriteBytes(oldBytes); } if (bytes != null) { outputStream.WriteBytes(bytes); } } else { if (bytes != null) { outputStream.AssignBytes(bytes, bytes.Length); } else { outputStream.Reset(); } } // Only when we remove old filter will the compression logic be triggered on flushing the stream Remove(PdfName.Filter); }
/// <summary>Adds object to the object stream.</summary> /// <param name="object">object to add.</param> public virtual void AddObject(PdfObject @object) { if (size.IntValue() == MAX_OBJ_STREAM_SIZE) { throw new PdfException(PdfException.PdfObjectStreamReachMaxSize); } PdfOutputStream outputStream = GetOutputStream(); indexStream.WriteInteger(@object.GetIndirectReference().GetObjNumber()).WriteSpace().WriteLong(outputStream .GetCurrentPos()).WriteSpace(); outputStream.Write(@object); @object.GetIndirectReference().SetObjStreamNumber(GetIndirectReference().GetObjNumber()); @object.GetIndirectReference().SetIndex(size.IntValue()); outputStream.WriteSpace(); size.Increment(); ((PdfNumber)Get(PdfName.First)).SetValue(indexStream.GetCurrentPos()); }
protected internal PdfStream(Stream outputStream) { this.outputStream = new PdfOutputStream(outputStream); this.compressionLevel = CompressionConstants.UNDEFINED_COMPRESSION; SetState(MUST_BE_INDIRECT); }
public PdfObjectStream(PdfDocument doc) : this(doc, new ByteArrayOutputStream()) { indexStream = new PdfOutputStream(new ByteArrayOutputStream()); }
private iText.Kernel.Pdf.PdfWriter SetDebugMode() { duplicateStream = new PdfOutputStream(new ByteArrayOutputStream()); return(this); }