1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License.  You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using Apache.Arrow.Memory;
17 using System;
18 using System.IO;
19 using System.Threading;
20 using System.Threading.Tasks;
21 
22 namespace Apache.Arrow.Ipc
23 {
24     /// <summary>
25     /// Represents a reader that can read Arrow streams.
26     /// </summary>
27     public class ArrowStreamReader : IArrowReader, IDisposable
28     {
29         private protected readonly ArrowReaderImplementation _implementation;
30 
31         public Schema Schema => _implementation.Schema;
32 
ArrowStreamReader(Stream stream)33         public ArrowStreamReader(Stream stream)
34             : this(stream, allocator: null, leaveOpen: false)
35         {
36         }
37 
ArrowStreamReader(Stream stream, MemoryAllocator allocator)38         public ArrowStreamReader(Stream stream, MemoryAllocator allocator)
39             : this(stream, allocator, leaveOpen: false)
40         {
41         }
42 
ArrowStreamReader(Stream stream, bool leaveOpen)43         public ArrowStreamReader(Stream stream, bool leaveOpen)
44             : this(stream, allocator: null, leaveOpen)
45         {
46         }
47 
ArrowStreamReader(Stream stream, MemoryAllocator allocator, bool leaveOpen)48         public ArrowStreamReader(Stream stream, MemoryAllocator allocator, bool leaveOpen)
49         {
50             if (stream == null)
51                 throw new ArgumentNullException(nameof(stream));
52 
53             _implementation = new ArrowStreamReaderImplementation(stream, allocator, leaveOpen);
54         }
55 
ArrowStreamReader(ReadOnlyMemory<byte> buffer)56         public ArrowStreamReader(ReadOnlyMemory<byte> buffer)
57         {
58             _implementation = new ArrowMemoryReaderImplementation(buffer);
59         }
60 
ArrowStreamReader(ArrowReaderImplementation implementation)61         private protected ArrowStreamReader(ArrowReaderImplementation implementation)
62         {
63             _implementation = implementation;
64         }
65 
Dispose()66         public void Dispose()
67         {
68             Dispose(true);
69             GC.SuppressFinalize(this);
70         }
71 
Dispose(bool disposing)72         protected virtual void Dispose(bool disposing)
73         {
74             if (disposing)
75             {
76                 _implementation.Dispose();
77             }
78         }
79 
ReadNextRecordBatchAsync(CancellationToken cancellationToken = default)80         public ValueTask<RecordBatch> ReadNextRecordBatchAsync(CancellationToken cancellationToken = default)
81         {
82             return _implementation.ReadNextRecordBatchAsync(cancellationToken);
83         }
84 
ReadNextRecordBatch()85         public RecordBatch ReadNextRecordBatch()
86         {
87             return _implementation.ReadNextRecordBatch();
88         }
89     }
90 }
91