This is an automated email from the ASF dual-hosted git repository.
kou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new cec7541 ARROW-4199: [GLib] Add garrow_seekable_input_stream_peek()
cec7541 is described below
commit cec75410b78b70b30bd57908d920c006d9101b72
Author: Yosuke Shiro <[email protected]>
AuthorDate: Wed Jan 9 13:35:05 2019 +0900
ARROW-4199: [GLib] Add garrow_seekable_input_stream_peek()
Author: Yosuke Shiro <[email protected]>
Author: Kouhei Sutou <[email protected]>
Closes #3351 from shiro615/glib-support-peek and squashes the following
commits:
1f445764 <Kouhei Sutou> Improve document
a5f0fdfd <Yosuke Shiro> Add GARROW_AVAILABLE_IN_0_12
b27c0a04 <Yosuke Shiro> Use g_bytes_new_static to avoid copying the data
f9d9f237 <Yosuke Shiro> Add support for Peek to InputStream
---
c_glib/arrow-glib/input-stream.cpp | 24 ++++++++++++++++++++++++
c_glib/arrow-glib/input-stream.h | 3 +++
c_glib/test/test-buffer-input-stream.rb | 8 ++++++++
3 files changed, 35 insertions(+)
diff --git a/c_glib/arrow-glib/input-stream.cpp
b/c_glib/arrow-glib/input-stream.cpp
index cb36e49..cb1fb3b 100644
--- a/c_glib/arrow-glib/input-stream.cpp
+++ b/c_glib/arrow-glib/input-stream.cpp
@@ -325,6 +325,30 @@
garrow_seekable_input_stream_read_at(GArrowSeekableInputStream *input_stream,
}
+/**
+ * garrow_seekable_input_stream_peek:
+ * @input_stream: A #GArrowSeekableInputStream.
+ * @n_bytes: The number of bytes to be peeked.
+ *
+ * Returns: (transfer full): The data of the buffer, up to the
+ * indicated number. The data becomes invalid after any operation on
+ * the stream. If the stream is unbuffered, the data is empty.
+ *
+ * It should be freed with g_bytes_unref() when no longer needed.
+ *
+ * Since: 0.12.0
+ */
+GBytes *
+garrow_seekable_input_stream_peek(GArrowSeekableInputStream *input_stream,
+ gint64 n_bytes)
+{
+ auto arrow_random_access_file =
+ garrow_seekable_input_stream_get_raw(input_stream);
+ auto string_view = arrow_random_access_file->Peek(n_bytes);
+ return g_bytes_new_static(string_view.data(), string_view.size());
+}
+
+
typedef struct GArrowBufferInputStreamPrivate_ {
GArrowBuffer *buffer;
} GArrowBufferInputStreamPrivate;
diff --git a/c_glib/arrow-glib/input-stream.h b/c_glib/arrow-glib/input-stream.h
index 9deebd7..745b912 100644
--- a/c_glib/arrow-glib/input-stream.h
+++ b/c_glib/arrow-glib/input-stream.h
@@ -66,6 +66,9 @@ GArrowBuffer
*garrow_seekable_input_stream_read_at(GArrowSeekableInputStream *in
gint64 position,
gint64 n_bytes,
GError **error);
+GARROW_AVAILABLE_IN_0_12
+GBytes *garrow_seekable_input_stream_peek(GArrowSeekableInputStream
*input_stream,
+ gint64 n_bytes);
#define GARROW_TYPE_BUFFER_INPUT_STREAM \
diff --git a/c_glib/test/test-buffer-input-stream.rb
b/c_glib/test/test-buffer-input-stream.rb
index f5a0132..cb6a667 100644
--- a/c_glib/test/test-buffer-input-stream.rb
+++ b/c_glib/test/test-buffer-input-stream.rb
@@ -39,4 +39,12 @@ class TestBufferInputStream < Test::Unit::TestCase
read_buffer = buffer_input_stream.read(3)
assert_equal("rld", read_buffer.data.to_s)
end
+
+ def test_peek
+ buffer = Arrow::Buffer.new("Hello World")
+ buffer_input_stream = Arrow::BufferInputStream.new(buffer)
+ peeked_data = buffer_input_stream.peek(5)
+ assert_equal(buffer_input_stream.read(5).data.to_s,
+ peeked_data.to_s)
+ end
end