1 // Copyright 2020 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 17 #ifndef TINK_PYTHON_TINK_CC_INPUT_STREAM_ADAPTER_H_ 18 #define TINK_PYTHON_TINK_CC_INPUT_STREAM_ADAPTER_H_ 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 24 #include "tink/input_stream.h" 25 #include "tink/util/statusor.h" 26 27 namespace crypto { 28 namespace tink { 29 30 // Adapts an InputStream for use in Python. 31 class InputStreamAdapter { 32 public: InputStreamAdapter(std::unique_ptr<InputStream> stream)33 explicit InputStreamAdapter(std::unique_ptr<InputStream> stream) 34 : stream_(std::move(stream)) {} 35 36 // Reads at most 'size' bytes from the underlying InputStream using only one 37 // call to Next(). 38 // If size is negative, all bytes that Next() gives are returned. 39 // Returns OUT_OF_RANGE status if the stream is already at EOF. 40 util::StatusOr<std::string> Read(int64_t size); 41 42 private: 43 std::unique_ptr<InputStream> stream_; 44 }; 45 46 } // namespace tink 47 } // namespace crypto 48 49 #endif // TINK_PYTHON_TINK_CC_INPUT_STREAM_ADAPTER_H_ 50