1 /*
2    Source File : PDFStream.h
3 
4 
5    Copyright 2011 Gal Kahana PDFWriter
6 
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10 
11        http://www.apache.org/licenses/LICENSE-2.0
12 
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 
19 
20 */
21 #pragma once
22 /*
23 	PDFStream objects represents a stream in the PDF.
24 	due to @#$@$ Length key in stream dictionary, stream writing in the library is a two step matter.
25 	first use PDFStream to write the stream content, then write the Stream to the PDF using ObjectContext WriteStream
26 */
27 
28 #include "EStatusCode.h"
29 #include "IOBasicTypes.h"
30 #include "ObjectsBasicTypes.h"
31 #include "OutputFlateEncodeStream.h"
32 #include "MyStringBuf.h"
33 #include "OutputStringBufferStream.h"
34 #include <sstream>
35 
36 
37 using namespace IOBasicTypes;
38 
39 
40 class IByteWriterWithPosition;
41 class IObjectsContextExtender;
42 class DictionaryContext;
43 
44 class PDFStream
45 {
46 public:
47 	PDFStream(
48 		bool inCompressStream,
49 		IByteWriterWithPosition* inOutputStream,
50 		ObjectIDType inExtentObjectID,
51 		IObjectsContextExtender* inObjectsContextExtender);
52 
53     PDFStream(
54         bool inCompressStream,
55         IByteWriterWithPosition* inOutputStream,
56         DictionaryContext* inStreamDictionaryContextForDirectExtentStream,
57         IObjectsContextExtender* inObjectsContextExtender);
58 
59 
60 	~PDFStream(void);
61 
62 	// Get the output stream of the PDFStream, make sure to use only before calling FinalizeStreamWrite, after which it becomes invalid
63 	IByteWriter* GetWriteStream();
64 
65 	// when done with writing to the stream call FinalizeWriteStream to get all writing resources released and calculate the stream extent. For streams where extent writing is direct object, there is still
66     // a call needed later, to FlushStreamContentForDirectExtentStream() to actually write it.
67 	void FinalizeStreamWrite();
68 
69 	bool IsStreamCompressed();
70 	ObjectIDType GetExtentObjectID();
71 
72 	LongFilePositionType GetLength(); // get the stream extent
73 
74     // direct extent specific
75     DictionaryContext* GetStreamDictionaryForDirectExtentStream();
76     void FlushStreamContentForDirectExtentStream();
77 
78 private:
79 	bool mCompressStream;
80 	OutputFlateEncodeStream mFlateEncodingStream;
81 	IByteWriterWithPosition* mOutputStream;
82 	ObjectIDType mExtendObjectID;
83 	LongFilePositionType mStreamLength;
84 	LongFilePositionType mStreamStartPosition;
85 	IByteWriter* mWriteStream;
86 	IObjectsContextExtender* mExtender;
87     MyStringBuf mTemporaryStream;
88     OutputStringBufferStream mTemporaryOutputStream;
89     DictionaryContext* mStreamDictionaryContextForDirectExtentStream;
90 };
91