Skip to content

Commit cec7541

Browse files
shiro615kou
authored andcommitted
ARROW-4199: [GLib] Add garrow_seekable_input_stream_peek()
Author: Yosuke Shiro <yosuke.shiro615@gmail.com> Author: Kouhei Sutou <kou@clear-code.com> Closes #3351 from shiro615/glib-support-peek and squashes the following commits: 1f44576 <Kouhei Sutou> Improve document a5f0fdf <Yosuke Shiro> Add GARROW_AVAILABLE_IN_0_12 b27c0a0 <Yosuke Shiro> Use g_bytes_new_static to avoid copying the data f9d9f23 <Yosuke Shiro> Add support for Peek to InputStream
1 parent bfe6865 commit cec7541

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

c_glib/arrow-glib/input-stream.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,30 @@ garrow_seekable_input_stream_read_at(GArrowSeekableInputStream *input_stream,
325325
}
326326

327327

328+
/**
329+
* garrow_seekable_input_stream_peek:
330+
* @input_stream: A #GArrowSeekableInputStream.
331+
* @n_bytes: The number of bytes to be peeked.
332+
*
333+
* Returns: (transfer full): The data of the buffer, up to the
334+
* indicated number. The data becomes invalid after any operation on
335+
* the stream. If the stream is unbuffered, the data is empty.
336+
*
337+
* It should be freed with g_bytes_unref() when no longer needed.
338+
*
339+
* Since: 0.12.0
340+
*/
341+
GBytes *
342+
garrow_seekable_input_stream_peek(GArrowSeekableInputStream *input_stream,
343+
gint64 n_bytes)
344+
{
345+
auto arrow_random_access_file =
346+
garrow_seekable_input_stream_get_raw(input_stream);
347+
auto string_view = arrow_random_access_file->Peek(n_bytes);
348+
return g_bytes_new_static(string_view.data(), string_view.size());
349+
}
350+
351+
328352
typedef struct GArrowBufferInputStreamPrivate_ {
329353
GArrowBuffer *buffer;
330354
} GArrowBufferInputStreamPrivate;

c_glib/arrow-glib/input-stream.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ GArrowBuffer *garrow_seekable_input_stream_read_at(GArrowSeekableInputStream *in
6666
gint64 position,
6767
gint64 n_bytes,
6868
GError **error);
69+
GARROW_AVAILABLE_IN_0_12
70+
GBytes *garrow_seekable_input_stream_peek(GArrowSeekableInputStream *input_stream,
71+
gint64 n_bytes);
6972

7073

7174
#define GARROW_TYPE_BUFFER_INPUT_STREAM \

c_glib/test/test-buffer-input-stream.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,12 @@ def test_align
3939
read_buffer = buffer_input_stream.read(3)
4040
assert_equal("rld", read_buffer.data.to_s)
4141
end
42+
43+
def test_peek
44+
buffer = Arrow::Buffer.new("Hello World")
45+
buffer_input_stream = Arrow::BufferInputStream.new(buffer)
46+
peeked_data = buffer_input_stream.peek(5)
47+
assert_equal(buffer_input_stream.read(5).data.to_s,
48+
peeked_data.to_s)
49+
end
4250
end

0 commit comments

Comments
 (0)