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 #include "tink/cc/input_stream_adapter.h" 18 19 #include <algorithm> 20 #include <string> 21 22 #include "absl/strings/str_cat.h" 23 #include "absl/strings/string_view.h" 24 #include "tink/util/status.h" 25 #include "tink/util/statusor.h" 26 27 namespace crypto { 28 namespace tink { 29 Read(int64_t size)30util::StatusOr<std::string> InputStreamAdapter::Read(int64_t size) { 31 const void* buffer; 32 auto next_result = stream_->Next(&buffer); 33 if (!next_result.ok()) return next_result.status(); 34 int available = next_result.value(); 35 int read_count = 36 size < 0 ? available : std::min(static_cast<int64_t>(available), size); 37 if (read_count < available) stream_->BackUp(available - read_count); 38 return std::string( 39 absl::string_view(static_cast<const char*>(buffer), read_count)); 40 } 41 42 } // namespace tink 43 } // namespace crypto 44