• 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,tstring * result)32 Status RandomAccessInputStream::ReadNBytes(int64 bytes_to_read,
33                                            tstring* result) {
34   if (bytes_to_read < 0) {
35     return errors::InvalidArgument("Cannot read negative number of bytes");
36   }
37   result->clear();
38   result->resize_uninitialized(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 #if defined(TF_CORD_SUPPORT)
ReadNBytes(int64 bytes_to_read,absl::Cord * result)53 Status RandomAccessInputStream::ReadNBytes(int64 bytes_to_read,
54                                            absl::Cord* result) {
55   if (bytes_to_read < 0) {
56     return errors::InvalidArgument("Cannot read negative number of bytes");
57   }
58   int64 current_size = result->size();
59   Status s = file_->Read(pos_, bytes_to_read, result);
60   if (s.ok() || errors::IsOutOfRange(s)) {
61     pos_ += result->size() - current_size;
62   }
63   return s;
64 }
65 #endif
66 
67 // To limit memory usage, the default implementation of SkipNBytes() only reads
68 // 8MB at a time.
69 static constexpr int64 kMaxSkipSize = 8 * 1024 * 1024;
70 
SkipNBytes(int64 bytes_to_skip)71 Status RandomAccessInputStream::SkipNBytes(int64 bytes_to_skip) {
72   if (bytes_to_skip < 0) {
73     return errors::InvalidArgument("Can't skip a negative number of bytes");
74   }
75   std::unique_ptr<char[]> scratch(new char[kMaxSkipSize]);
76   // Try to read 1 bytes first, if we could complete the read then EOF is
77   // not reached yet and we could return.
78   if (bytes_to_skip > 0) {
79     StringPiece data;
80     Status s = file_->Read(pos_ + bytes_to_skip - 1, 1, &data, scratch.get());
81     if ((s.ok() || errors::IsOutOfRange(s)) && data.size() == 1) {
82       pos_ += bytes_to_skip;
83       return Status::OK();
84     }
85   }
86   // Read kDefaultSkipSize at a time till bytes_to_skip.
87   while (bytes_to_skip > 0) {
88     int64 bytes_to_read = std::min<int64>(kMaxSkipSize, bytes_to_skip);
89     StringPiece data;
90     Status s = file_->Read(pos_, bytes_to_read, &data, scratch.get());
91     if (s.ok() || errors::IsOutOfRange(s)) {
92       pos_ += data.size();
93     } else {
94       return s;
95     }
96     if (data.size() < static_cast<size_t>(bytes_to_read)) {
97       return errors::OutOfRange("reached end of file");
98     }
99     bytes_to_skip -= bytes_to_read;
100   }
101   return Status::OK();
102 }
103 
Tell() const104 int64 RandomAccessInputStream::Tell() const { return pos_; }
105 
106 }  // namespace io
107 }  // namespace tensorflow
108