aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/io/buffered_inputstream_test.cc
diff options
context:
space:
mode:
authorGravatar Vijay Vasudevan <vrv@google.com>2016-09-28 08:23:29 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2016-09-28 09:33:27 -0700
commit63a7a30e6bd091f87be1de2305c6d882d68ba6a8 (patch)
tree3d7349ec98338098941c63567081746a4f6a9851 /tensorflow/core/lib/io/buffered_inputstream_test.cc
parent419d5d072375ee0044fecb94e4bfe21a7b3b0b9e (diff)
Add seek to FileIO via RandomAccessInputStream and BufferedInputStream,
by introducing a Seek() API to those implementations. Seek is different than SkipNBytes in that you can Seek backwards, whereas on a stream, you can only move forwards. While Seek() on RandomAccessInputStream sort of makes sense, Seek() on BufferedInputStream makes a little less sense. I avoided adding Seek() to the InputStreamInterface because I didn't want all implementors of InputStreamInterface to have to implement Seek(), only those for which it made some sense. However, I did have to add a Reset() API to InputStreamInterface to allow reseting + skipping as the method of seeking backwards. Adds read(n) to FileIO. Defaults to 'read to the end of the file' as usual, and allows reading n bytes. This change is to help enable FileIO to be used as a fileobj for gzipfile, among other things. Change: 134542845
Diffstat (limited to 'tensorflow/core/lib/io/buffered_inputstream_test.cc')
-rw-r--r--tensorflow/core/lib/io/buffered_inputstream_test.cc30
1 files changed, 30 insertions, 0 deletions
diff --git a/tensorflow/core/lib/io/buffered_inputstream_test.cc b/tensorflow/core/lib/io/buffered_inputstream_test.cc
index 1263f0f9aa..4488637cad 100644
--- a/tensorflow/core/lib/io/buffered_inputstream_test.cc
+++ b/tensorflow/core/lib/io/buffered_inputstream_test.cc
@@ -293,6 +293,36 @@ TEST(BufferedInputStream, SkipNBytesRandomAccessFile) {
}
}
+TEST(BufferedInputStream, Seek) {
+ Env* env = Env::Default();
+ string fname = testing::TmpDir() + "/buffered_inputstream_test";
+ WriteStringToFile(env, fname, "0123456789");
+ std::unique_ptr<RandomAccessFile> file;
+ TF_ASSERT_OK(env->NewRandomAccessFile(fname, &file));
+
+ for (auto buf_size : BufferSizes()) {
+ std::unique_ptr<RandomAccessInputStream> input_stream(
+ new RandomAccessInputStream(file.get()));
+ string read;
+ BufferedInputStream in(input_stream.get(), buf_size);
+
+ // Seek forward
+ TF_ASSERT_OK(in.Seek(3));
+ EXPECT_EQ(3, in.Tell());
+
+ // Read 4 bytes
+ TF_ASSERT_OK(in.ReadNBytes(4, &read));
+ EXPECT_EQ(read, "3456");
+ EXPECT_EQ(7, in.Tell());
+
+ // Seek backwards
+ TF_ASSERT_OK(in.Seek(1));
+ TF_ASSERT_OK(in.ReadNBytes(4, &read));
+ EXPECT_EQ(read, "1234");
+ EXPECT_EQ(5, in.Tell());
+ }
+}
+
} // anonymous namespace
} // namespace io
} // namespace tensorflow