Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
io-util.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_UTIL_IO_UTIL_H
19 #define ARROW_UTIL_IO_UTIL_H
20 
21 #include <iostream>
22 
23 #include "arrow/buffer.h"
24 #include "arrow/io/interfaces.h"
25 #include "arrow/status.h"
26 
27 namespace arrow {
28 namespace io {
29 
30 // Output stream that just writes to stdout.
31 class StdoutStream : public OutputStream {
32  public:
34  virtual ~StdoutStream() {}
35 
36  Status Close() override { return Status::OK(); }
37 
38  Status Tell(int64_t* position) const override {
39  *position = pos_;
40  return Status::OK();
41  }
42 
43  Status Write(const uint8_t* data, int64_t nbytes) override {
44  pos_ += nbytes;
45  std::cout.write(reinterpret_cast<const char*>(data), nbytes);
46  return Status::OK();
47  }
48 
49  private:
50  int64_t pos_;
51 };
52 
53 // Input stream that just reads from stdin.
54 class StdinStream : public InputStream {
55  public:
57  virtual ~StdinStream() {}
58 
59  Status Close() override { return Status::OK(); }
60 
61  Status Tell(int64_t* position) const override {
62  *position = pos_;
63  return Status::OK();
64  }
65 
66  Status Read(int64_t nbytes, int64_t* bytes_read, uint8_t* out) override {
67  std::cin.read(reinterpret_cast<char*>(out), nbytes);
68  if (std::cin) {
69  *bytes_read = nbytes;
70  pos_ += nbytes;
71  } else {
72  *bytes_read = 0;
73  }
74  return Status::OK();
75  }
76 
77  Status Read(int64_t nbytes, std::shared_ptr<Buffer>* out) override {
78  auto buffer = std::make_shared<PoolBuffer>(NULLPTR);
79  RETURN_NOT_OK(buffer->Resize(nbytes));
80  int64_t bytes_read;
81  RETURN_NOT_OK(Read(nbytes, &bytes_read, buffer->mutable_data()));
82  RETURN_NOT_OK(buffer->Resize(bytes_read, false));
83  *out = buffer;
84  return Status::OK();
85  }
86 
87  private:
88  int64_t pos_;
89 };
90 
91 } // namespace io
92 } // namespace arrow
93 
94 #endif // ARROW_UTIL_IO_UTIL_H
Status Read(int64_t nbytes, std::shared_ptr< Buffer > *out) override
Definition: io-util.h:77
Status Write(const uint8_t *data, int64_t nbytes) override
Definition: io-util.h:43
Definition: interfaces.h:111
virtual ~StdinStream()
Definition: io-util.h:57
Definition: interfaces.h:116
virtual ~StdoutStream()
Definition: io-util.h:34
#define NULLPTR
Definition: macros.h:69
Status Tell(int64_t *position) const override
Definition: io-util.h:38
Status Close() override
Definition: io-util.h:59
StdinStream()
Definition: io-util.h:56
Definition: interfaces.h:37
Definition: status.h:106
Definition: io-util.h:54
#define RETURN_NOT_OK(s)
Definition: status.h:66
static Status OK()
Definition: status.h:119
Status Tell(int64_t *position) const override
Definition: io-util.h:61
StdoutStream()
Definition: io-util.h:33
void set_mode(FileMode::type mode)
Definition: interfaces.h:77
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29
Status Close() override
Definition: io-util.h:36
Definition: io-util.h:31
Status Read(int64_t nbytes, int64_t *bytes_read, uint8_t *out) override
Definition: io-util.h:66
Definition: interfaces.h:37