1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 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 #ifndef TENSORFLOW_CORE_LIB_IO_RANDOM_INPUTSTREAM_H_ 17 #define TENSORFLOW_CORE_LIB_IO_RANDOM_INPUTSTREAM_H_ 18 19 #include "tensorflow/core/lib/io/inputstream_interface.h" 20 #include "tensorflow/core/platform/cord.h" 21 #include "tensorflow/core/platform/file_system.h" 22 23 namespace tensorflow { 24 namespace io { 25 26 // Wraps a RandomAccessFile in an InputStreamInterface. A given instance of 27 // RandomAccessInputStream is NOT safe for concurrent use by multiple threads. 28 class RandomAccessInputStream : public InputStreamInterface { 29 public: 30 // Does not take ownership of 'file' unless owns_file is set to true. 'file' 31 // must outlive *this. 32 RandomAccessInputStream(RandomAccessFile* file, bool owns_file = false); 33 34 ~RandomAccessInputStream(); 35 36 Status ReadNBytes(int64_t bytes_to_read, tstring* result) override; 37 38 #if defined(TF_CORD_SUPPORT) 39 Status ReadNBytes(int64_t bytes_to_read, absl::Cord* result) override; 40 #endif 41 42 Status SkipNBytes(int64_t bytes_to_skip) override; 43 44 int64_t Tell() const override; 45 Seek(int64_t position)46 Status Seek(int64_t position) { 47 pos_ = position; 48 return OkStatus(); 49 } 50 Reset()51 Status Reset() override { return Seek(0); } 52 53 private: 54 RandomAccessFile* file_; // Not owned. 55 int64_t pos_ = 0; // Tracks where we are in the file. 56 bool owns_file_ = false; 57 }; 58 59 } // namespace io 60 } // namespace tensorflow 61 62 #endif // TENSORFLOW_CORE_LIB_IO_RANDOM_INPUTSTREAM_H_ 63