Line data Source code
1 : /****************************************************************************** 2 : * 3 : * Project: Arrow generic code 4 : * Purpose: Arrow generic code 5 : * Author: Even Rouault, <even.rouault at spatialys.com> 6 : * 7 : ****************************************************************************** 8 : * Copyright (c) 2022, Planet Labs 9 : * 10 : * Permission is hereby granted, free of charge, to any person obtaining a 11 : * copy of this software and associated documentation files (the "Software"), 12 : * to deal in the Software without restriction, including without limitation 13 : * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 : * and/or sell copies of the Software, and to permit persons to whom the 15 : * Software is furnished to do so, subject to the following conditions: 16 : * 17 : * The above copyright notice and this permission notice shall be included 18 : * in all copies or substantial portions of the Software. 19 : * 20 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 21 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 : * DEALINGS IN THE SOFTWARE. 27 : ****************************************************************************/ 28 : 29 : #ifndef OGR_ARROW_WRITABLE_FILE_H 30 : #define OGR_ARROW_WRITABLE_FILE_H 31 : 32 : #include "cpl_vsi.h" 33 : 34 : #include "arrow/buffer.h" 35 : #include "arrow/io/file.h" 36 : #include "arrow/io/interfaces.h" 37 : 38 : /************************************************************************/ 39 : /* OGRArrowWritableFile */ 40 : /************************************************************************/ 41 : 42 : class OGRArrowWritableFile final : public arrow::io::OutputStream 43 : { 44 : VSILFILE *m_fp; 45 : 46 : OGRArrowWritableFile(const OGRArrowWritableFile &) = delete; 47 : OGRArrowWritableFile &operator=(const OGRArrowWritableFile &) = delete; 48 : 49 : public: 50 336 : explicit OGRArrowWritableFile(VSILFILE *fp) : m_fp(fp) 51 : { 52 336 : } 53 : 54 336 : ~OGRArrowWritableFile() override 55 336 : { 56 336 : if (m_fp) 57 336 : VSIFCloseL(m_fp); 58 336 : } 59 : 60 0 : arrow::Status Close() override 61 : { 62 0 : int ret = VSIFCloseL(m_fp); 63 0 : m_fp = nullptr; 64 : return ret == 0 ? arrow::Status::OK() 65 0 : : arrow::Status::IOError("Error while closing"); 66 : } 67 : 68 4624 : arrow::Result<int64_t> Tell() const override 69 : { 70 4624 : return static_cast<int64_t>(VSIFTellL(m_fp)); 71 : } 72 : 73 0 : bool closed() const override 74 : { 75 0 : return m_fp == nullptr; 76 : } 77 : 78 16524 : arrow::Status Write(const void *data, int64_t nbytes) override 79 : { 80 : CPLAssert(static_cast<int64_t>(static_cast<size_t>(nbytes)) == nbytes); 81 16524 : if (VSIFWriteL(data, 1, static_cast<size_t>(nbytes), m_fp) == 82 16524 : static_cast<size_t>(nbytes)) 83 16524 : return arrow::Status::OK(); 84 0 : return arrow::Status::IOError("Error while writing"); 85 : } 86 : 87 4493 : arrow::Status Write(const std::shared_ptr<arrow::Buffer> &data) override 88 : { 89 4493 : return Write(data->data(), data->size()); 90 : } 91 : }; 92 : 93 : #endif // OGR_ARROW_WRITABLE_FILE_H