Commit 4c71c8680a8aed21e356d23d9828ebb37d052342
1 parent
3237ef70
Refactor QPDF_Stream::getStreamJSON
Showing
1 changed file
with
12 additions
and
13 deletions
libqpdf/QPDF_Stream.cc
| ... | ... | @@ -216,29 +216,28 @@ QPDF_Stream::getStreamJSON( |
| 216 | 216 | auto dict = this->stream_dict; |
| 217 | 217 | JSON result = JSON::makeDictionary(); |
| 218 | 218 | if (json_data != qpdf_sj_none) { |
| 219 | - std::shared_ptr<Buffer> buf; | |
| 219 | + Pl_Discard discard; | |
| 220 | + Pl_Buffer buf_pl{"stream data"}; | |
| 221 | + // buf_pl contains valid data and is ready for retrieval of the data. | |
| 222 | + bool buf_pl_ready = false; | |
| 220 | 223 | bool filtered = false; |
| 221 | 224 | bool filter = (decode_level != qpdf_dl_none); |
| 222 | 225 | for (int attempt = 1; attempt <= 2; ++attempt) { |
| 223 | - Pl_Discard discard; | |
| 224 | - std::shared_ptr<Pl_Buffer> buf_pl; | |
| 225 | - Pipeline* data_pipeline = nullptr; | |
| 226 | + Pipeline* data_pipeline = &discard; | |
| 226 | 227 | if (json_data == qpdf_sj_file) { |
| 227 | 228 | // We need to capture the data to write |
| 228 | - buf_pl = std::make_shared<Pl_Buffer>("stream data"); | |
| 229 | - data_pipeline = buf_pl.get(); | |
| 230 | - } else { | |
| 231 | - data_pipeline = &discard; | |
| 229 | + data_pipeline = &buf_pl; | |
| 232 | 230 | } |
| 233 | 231 | bool succeeded = |
| 234 | 232 | pipeStreamData(data_pipeline, &filtered, 0, decode_level, false, (attempt == 1)); |
| 235 | - if ((!succeeded) || (filter && (!filtered))) { | |
| 233 | + if (!succeeded || (filter && !filtered)) { | |
| 236 | 234 | // Try again |
| 237 | 235 | filter = false; |
| 238 | 236 | decode_level = qpdf_dl_none; |
| 237 | + buf_pl.getString(); // reset buf_pl | |
| 239 | 238 | } else { |
| 240 | - if (buf_pl.get()) { | |
| 241 | - buf = buf_pl->getBufferSharedPointer(); | |
| 239 | + if (json_data == qpdf_sj_file) { | |
| 240 | + buf_pl_ready = true; | |
| 242 | 241 | } |
| 243 | 242 | break; |
| 244 | 243 | } |
| ... | ... | @@ -252,10 +251,10 @@ QPDF_Stream::getStreamJSON( |
| 252 | 251 | } |
| 253 | 252 | if (json_data == qpdf_sj_file) { |
| 254 | 253 | result.addDictionaryMember("datafile", JSON::makeString(data_filename)); |
| 255 | - if (!buf.get()) { | |
| 254 | + if (!buf_pl_ready) { | |
| 256 | 255 | throw std::logic_error("QPDF_Stream: failed to get stream data in json file mode"); |
| 257 | 256 | } |
| 258 | - p->write(buf->getBuffer(), buf->getSize()); | |
| 257 | + p->writeString(buf_pl.getString()); | |
| 259 | 258 | } else if (json_data == qpdf_sj_inline) { |
| 260 | 259 | result.addDictionaryMember( |
| 261 | 260 | "data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); | ... | ... |