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,29 +216,28 @@ QPDF_Stream::getStreamJSON( | ||
| 216 | auto dict = this->stream_dict; | 216 | auto dict = this->stream_dict; |
| 217 | JSON result = JSON::makeDictionary(); | 217 | JSON result = JSON::makeDictionary(); |
| 218 | if (json_data != qpdf_sj_none) { | 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 | bool filtered = false; | 223 | bool filtered = false; |
| 221 | bool filter = (decode_level != qpdf_dl_none); | 224 | bool filter = (decode_level != qpdf_dl_none); |
| 222 | for (int attempt = 1; attempt <= 2; ++attempt) { | 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 | if (json_data == qpdf_sj_file) { | 227 | if (json_data == qpdf_sj_file) { |
| 227 | // We need to capture the data to write | 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 | bool succeeded = | 231 | bool succeeded = |
| 234 | pipeStreamData(data_pipeline, &filtered, 0, decode_level, false, (attempt == 1)); | 232 | pipeStreamData(data_pipeline, &filtered, 0, decode_level, false, (attempt == 1)); |
| 235 | - if ((!succeeded) || (filter && (!filtered))) { | 233 | + if (!succeeded || (filter && !filtered)) { |
| 236 | // Try again | 234 | // Try again |
| 237 | filter = false; | 235 | filter = false; |
| 238 | decode_level = qpdf_dl_none; | 236 | decode_level = qpdf_dl_none; |
| 237 | + buf_pl.getString(); // reset buf_pl | ||
| 239 | } else { | 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 | break; | 242 | break; |
| 244 | } | 243 | } |
| @@ -252,10 +251,10 @@ QPDF_Stream::getStreamJSON( | @@ -252,10 +251,10 @@ QPDF_Stream::getStreamJSON( | ||
| 252 | } | 251 | } |
| 253 | if (json_data == qpdf_sj_file) { | 252 | if (json_data == qpdf_sj_file) { |
| 254 | result.addDictionaryMember("datafile", JSON::makeString(data_filename)); | 253 | result.addDictionaryMember("datafile", JSON::makeString(data_filename)); |
| 255 | - if (!buf.get()) { | 254 | + if (!buf_pl_ready) { |
| 256 | throw std::logic_error("QPDF_Stream: failed to get stream data in json file mode"); | 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 | } else if (json_data == qpdf_sj_inline) { | 258 | } else if (json_data == qpdf_sj_inline) { |
| 260 | result.addDictionaryMember( | 259 | result.addDictionaryMember( |
| 261 | "data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); | 260 | "data", JSON::makeBlob(StreamBlobProvider(this, decode_level))); |