• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/lib/io/random_inputstream.h"
17 #include <memory>
18 
19 namespace tensorflow {
20 namespace io {
21 
RandomAccessInputStream(RandomAccessFile * file,bool owns_file)22 RandomAccessInputStream::RandomAccessInputStream(RandomAccessFile* file,
23                                                  bool owns_file)
24     : file_(file), owns_file_(owns_file) {}
25 
~RandomAccessInputStream()26 RandomAccessInputStream::~RandomAccessInputStream() {
27   if (owns_file_) {
28     delete file_;
29   }
30 }
31 
ReadNBytes(int64 bytes_to_read,string * result)32 Status RandomAccessInputStream::ReadNBytes(int64 bytes_to_read,
33                                            string* result) {
34   if (bytes_to_read < 0) {
35     return errors::InvalidArgument("Cannot read negative number of bytes");
36   }
37   result->clear();
38   result->resize(bytes_to_read);
39   char* result_buffer = &(*result)[0];
40   StringPiece data;
41   Status s = file_->Read(pos_, bytes_to_read, &data, result_buffer);
42   if (data.data() != result_buffer) {
43     memmove(result_buffer, data.data(), data.size());
44   }
45   result->resize(data.size());
46   if (s.ok() || errors::IsOutOfRange(s)) {
47     pos_ += data.size();
48   }
49   return s;
50 }
51 
52 // To limit memory usage, the default implementation of SkipNBytes() only reads
53 // 8MB at a time.
54 static constexpr int64 kMaxSkipSize = 8 * 1024 * 1024;
55 
SkipNBytes(int64 bytes_to_skip)56 Status RandomAccessInputStream::SkipNBytes(int64 bytes_to_skip) {
57   if (bytes_to_skip < 0) {
58     return errors::InvalidArgument("Can't skip a negative number of bytes");
59   }
60   std::unique_ptr<char[]> scratch(new char[kMaxSkipSize]);
61   // Try to read 1 bytes first, if we could complete the read then EOF is
62   // not reached yet and we could return.
63   if (bytes_to_skip > 0) {
64     StringPiece data;
65     Status s = file_->Read(pos_ + bytes_to_skip - 1, 1, &data, scratch.get());
66     if ((s.ok() || errors::IsOutOfRange(s)) && data.size() == 1) {
67       pos_ += bytes_to_skip;
68       return Status::OK();
69     }
70   }
71   // Read kDefaultSkipSize at a time till bytes_to_skip.
72   while (bytes_to_skip > 0) {
73     int64 bytes_to_read = std::min<int64>(kMaxSkipSize, bytes_to_skip);
74     StringPiece data;
75     Status s = file_->Read(pos_, bytes_to_read, &data, scratch.get());
76     if (s.ok() || errors::IsOutOfRange(s)) {
77       pos_ += data.size();
78     } else {
79       return s;
80     }
81     if (data.size() < bytes_to_read) {
82       return errors::OutOfRange("reached end of file");
83     }
84     bytes_to_skip -= bytes_to_read;
85   }
86   return Status::OK();
87 }
88 
Tell() const89 int64 RandomAccessInputStream::Tell() const { return pos_; }
90 
91 }  // namespace io
92 }  // namespace tensorflow
93