Home | History | Annotate | Download | only in io
      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_LIB_IO_RANDOM_INPUTSTREAM_H_
     17 #define TENSORFLOW_LIB_IO_RANDOM_INPUTSTREAM_H_
     18 
     19 #include "tensorflow/core/lib/io/inputstream_interface.h"
     20 #include "tensorflow/core/platform/file_system.h"
     21 
     22 namespace tensorflow {
     23 namespace io {
     24 
     25 // Wraps a RandomAccessFile in an InputStreamInterface. A given instance of
     26 // RandomAccessInputStream is NOT safe for concurrent use by multiple threads.
     27 class RandomAccessInputStream : public InputStreamInterface {
     28  public:
     29   // Does not take ownership of 'file' unless owns_file is set to true. 'file'
     30   // must outlive *this.
     31   RandomAccessInputStream(RandomAccessFile* file, bool owns_file = false);
     32 
     33   ~RandomAccessInputStream();
     34 
     35   Status ReadNBytes(int64 bytes_to_read, string* result) override;
     36 
     37   Status SkipNBytes(int64 bytes_to_skip) override;
     38 
     39   int64 Tell() const override;
     40 
     41   Status Seek(int64 position) {
     42     pos_ = position;
     43     return Status::OK();
     44   }
     45 
     46   Status Reset() override { return Seek(0); }
     47 
     48  private:
     49   RandomAccessFile* file_;  // Not owned.
     50   int64 pos_ = 0;           // Tracks where we are in the file.
     51   bool owns_file_ = false;
     52 };
     53 
     54 }  // namespace io
     55 }  // namespace tensorflow
     56 
     57 #endif  // TENSORFLOW_LIB_IO_RANDOM_INPUTSTREAM_H_
     58