forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartialStream.cpp
More file actions
255 lines (209 loc) · 5.72 KB
/
PartialStream.cpp
File metadata and controls
255 lines (209 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// PartialStream.cpp
//
// Library: Zip
// Package: Zip
// Module: PartialStream
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Zip/PartialStream.h"
#include "Poco/Exception.h"
#include <cstring>
namespace Poco {
namespace Zip {
PartialStreamBuf::PartialStreamBuf(std::istream& in, std::ios::pos_type start, std::ios::pos_type end, const std::string& pre, const std::string& post, bool initStream):
Poco::BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::in),
_initialized(!initStream),
_start(start),
_numBytes(end-start),
_bytesWritten(0),
_pIstr(&in),
_pOstr(0),
_prefix(pre),
_postfix(post),
_ignoreStart(0),
_buffer(0),
_bufferOffset(0)
{
}
PartialStreamBuf::PartialStreamBuf(std::ostream& out, std::size_t start, std::size_t end, bool initStream):
Poco::BufferedStreamBuf(STREAM_BUFFER_SIZE, std::ios::out),
_initialized(!initStream),
_start(0),
_numBytes(0),
_bytesWritten(0),
_pIstr(0),
_pOstr(&out),
_ignoreStart(start),
_buffer(end),
_bufferOffset(0)
{
}
PartialStreamBuf::~PartialStreamBuf()
{
}
int PartialStreamBuf::readFromDevice(char* buffer, std::streamsize length)
{
if (_pIstr == 0 ||length == 0) return -1;
if (!_initialized)
{
_initialized = true;
_pIstr->clear();
_pIstr->seekg(_start, std::ios_base::beg);
if (_pIstr->fail())
throw Poco::IOException("Failed to seek on input stream");
}
if (!_prefix.empty())
{
std::streamsize tmp = (_prefix.size() > length)? length: static_cast<std::streamsize>(_prefix.size());
std::memcpy(buffer, _prefix.c_str(), tmp);
_prefix = _prefix.substr(tmp);
return static_cast<int>(tmp);
}
if (_numBytes == 0)
{
if (!_postfix.empty())
{
std::streamsize tmp = (_postfix.size() > length)? length: static_cast<std::streamsize>(_postfix.size());
std::memcpy(buffer, _postfix.c_str(), tmp);
_postfix = _postfix.substr(tmp);
return static_cast<int>(tmp);
}
else
return -1;
}
if (!_pIstr->good())
return -1;
if (_numBytes < length)
length = static_cast<std::streamsize>(_numBytes);
_pIstr->read(buffer, length);
std::streamsize bytesRead = _pIstr->gcount();
_numBytes -= bytesRead;
return static_cast<int>(bytesRead);
}
int PartialStreamBuf::writeToDevice(const char* buffer, std::streamsize length)
{
if (_pOstr == 0 || length == 0) return -1;
if (!_initialized)
{
_initialized = true;
_pOstr->clear();
if (_pOstr->fail())
throw Poco::IOException("Failed to clear stream status");
}
if (_ignoreStart > 0)
{
if (_ignoreStart > length)
{
_ignoreStart -= length;
// fake return values
return static_cast<int>(length);
}
else
{
std::streamsize cnt = static_cast<std::streamsize>(length - _ignoreStart - _buffer.size());
if (cnt > 0)
{
_pOstr->write(buffer+_ignoreStart, cnt);
_bytesWritten += cnt;
}
// copy the rest into buffer
cnt += static_cast<std::streamsize>(_ignoreStart);
_ignoreStart = 0;
poco_assert (cnt < length);
_bufferOffset = static_cast<Poco::UInt32>(length - cnt);
std::memcpy(_buffer.begin(), buffer + cnt, _bufferOffset);
return static_cast<int>(length);
}
}
if (_buffer.size() > 0)
{
// always treat each write as the potential last one
// thus first fill the buffer with the last n bytes of the msg
// how much of the already cached data do we need to write?
Poco::Int32 cache = static_cast<Poco::Int32>(_bufferOffset +
static_cast<Poco::Int32>(length) - static_cast<Poco::Int32>(_buffer.size()));
if (cache > 0)
{
if (cache > _bufferOffset)
cache = _bufferOffset;
_pOstr->write(_buffer.begin(), cache);
_bytesWritten += cache;
_bufferOffset -= cache;
if (_bufferOffset > 0)
std::memmove(_buffer.begin(), _buffer.begin()+cache, _bufferOffset);
}
// now fill up _buffer with the last bytes from buffer
Poco::Int32 pos = static_cast<Poco::Int32>(length - static_cast<Poco::Int32>(_buffer.size()) + _bufferOffset);
if (pos <= 0)
{
// all of the message goes to _buffer
std::memcpy(_buffer.begin() + _bufferOffset, buffer, length);
}
else
{
poco_assert (_bufferOffset == 0);
std::memcpy(_buffer.begin(), buffer+pos, _buffer.size());
_bufferOffset = static_cast<Poco::UInt32>(_buffer.size());
// the rest is written
_pOstr->write(buffer, static_cast<std::streamsize>(length - _buffer.size()));
_bytesWritten += (length - _buffer.size());
}
}
else
{
_pOstr->write(buffer, length);
_bytesWritten += length;
}
if (_pOstr->good())
return static_cast<int>(length);
throw Poco::IOException("Failed to write to output stream");
}
void PartialStreamBuf::close()
{
// DON'T write data from _buffer!
}
PartialIOS::PartialIOS(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, const std::string& pre, const std::string& post, bool initStream): _buf(istr, start, end, pre, post, initStream)
{
poco_ios_init(&_buf);
}
PartialIOS::PartialIOS(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream): _buf(ostr, start, end, initStream)
{
poco_ios_init(&_buf);
}
PartialIOS::~PartialIOS()
{
}
PartialStreamBuf* PartialIOS::rdbuf()
{
return &_buf;
}
PartialInputStream::PartialInputStream(std::istream& istr, std::ios::pos_type start, std::ios::pos_type end, bool initStream, const std::string& pre, const std::string& post):
PartialIOS(istr, start, end, pre, post, initStream),
std::istream(&_buf)
{
}
PartialInputStream::~PartialInputStream()
{
}
PartialOutputStream::PartialOutputStream(std::ostream& ostr, std::size_t start, std::size_t end, bool initStream):
PartialIOS(ostr, start, end, initStream),
std::ostream(&_buf)
{
}
PartialOutputStream::~PartialOutputStream()
{
try
{
close();
}
catch (...)
{
poco_unexpected();
}
}
} } // namespace Poco::Zip