4
// $Id: //poco/1.3/Zip/include/Poco/Zip/PartialStream.h#3 $
8
// Module: PartialStream
10
// Definition of the PartialStream class.
12
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
39
#ifndef Zip_PartialStream_INCLUDED
40
#define Zip_PartialStream_INCLUDED
43
#include "Poco/Zip/Zip.h"
44
#include "Poco/BufferedStreamBuf.h"
45
#include "Poco/Buffer.h"
54
class Zip_API PartialStreamBuf: public Poco::BufferedStreamBuf
55
/// A PartialStreamBuf is a class that limits one view on an inputstream to a selected view range
58
PartialStreamBuf(std::istream& in, std::ios::pos_type start, std::ios::pos_type end, const std::string& prefix, const std::string& postfix, bool initStream);
59
/// Creates the PartialStream.
60
/// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
63
PartialStreamBuf(std::ostream& out, std::size_t start, std::size_t end, bool initStream);
64
/// Creates the PartialStream.
65
/// If initStream is true the status of the stream will be cleared on the first access.
66
/// start and end acts as offset values for the written content. A start value greater than zero,
67
/// means that the first bytes are not written but discarded instead,
68
/// an end value not equal to zero means that the last end bytes are not written!
70
/// start = 3; end = 1
71
/// write("hello", 5) -> "l"
74
/// Destroys the PartialStream.
77
/// Flushes a writing streambuf
79
Poco::UInt64 bytesWritten() const;
82
int readFromDevice(char* buffer, std::streamsize length);
84
int writeToDevice(const char* buffer, std::streamsize length);
89
STREAM_BUFFER_SIZE = 1024
93
std::ios::pos_type _start;
94
Poco::UInt64 _numBytes;
95
Poco::UInt64 _bytesWritten;
100
std::size_t _ignoreStart;
101
Poco::Buffer<char> _buffer;
102
Poco::UInt32 _bufferOffset;
106
inline Poco::UInt64 PartialStreamBuf::bytesWritten() const
108
return _bytesWritten;
112
class Zip_API PartialIOS: public virtual std::ios
113
/// The base class for PartialInputStream and PartialOutputStream.
115
/// This class is needed to ensure the correct initialization
116
/// order of the stream buffer and base classes.
119
PartialIOS(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, const std::string& prefix, const std::string& postfix, bool initStream);
120
/// Creates the basic stream and connects it
121
/// to the given input stream.
122
/// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
123
/// to position start
125
PartialIOS(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream);
126
/// Creates the basic stream and connects it
127
/// to the given output stream.
128
/// If initStream is true the status of the stream will be cleared on the first access.
129
/// start and end acts as offset values for the written content. A start value greater than zero,
130
/// means that the first bytes are not written but discarded instead,
131
/// an end value not equal to zero means that the last end bytes are not written!
133
/// start = 3; end = 1
134
/// write("hello", 5) -> "l"
137
/// Destroys the stream.
139
PartialStreamBuf* rdbuf();
140
/// Returns a pointer to the underlying streambuf.
143
PartialStreamBuf _buf;
147
class Zip_API PartialInputStream: public PartialIOS, public std::istream
148
/// This stream copies all characters read through it
149
/// to one or multiple output streams.
152
PartialInputStream(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, bool initStream = true, const std::string& prefix = std::string(), const std::string& postfix = std::string());
153
/// Creates the PartialInputStream and connects it
154
/// to the given input stream. Bytes read are guaranteed to be in the range [start, end-1]
155
/// If initStream is true the status of the stream will be cleared on the first access, and the stream will be repositioned
156
/// to position start
158
~PartialInputStream();
159
/// Destroys the PartialInputStream.
163
class Zip_API PartialOutputStream: public PartialIOS, public std::ostream
164
/// This stream copies all characters written to it
165
/// to one or multiple output streams.
168
PartialOutputStream(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream = true);
169
/// Creates the PartialOutputStream and connects it
170
/// to the given output stream. Bytes written are guaranteed to be in the range [start, realEnd - end].
171
/// If initStream is true the status of the stream will be cleared on the first access.
172
/// start and end acts as offset values for the written content. A start value greater than zero,
173
/// means that the first bytes are not written but discarded instead,
174
/// an end value not equal to zero means that the last end bytes are not written!
176
/// start = 3; end = 1
177
/// write("hello", 5) -> "l"
179
/// start = 3; end = 0
180
/// write("hello", 5) -> "lo"
182
~PartialOutputStream();
183
/// Destroys the PartialOutputStream.
186
/// must be called for the stream to properly terminate it
188
Poco::UInt64 bytesWritten() const;
189
/// Returns the number of bytes actually forwarded to the inner ostream
193
inline void PartialOutputStream::close()
200
inline Poco::UInt64 PartialOutputStream::bytesWritten() const
202
return _buf.bytesWritten();
206
} } // namespace Poco::Zip
209
#endif // Zip_PartialStream_INCLUDED