|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include <cstdint> |
| 23 | +#include <memory> |
| 24 | +#include <string> |
| 25 | + |
| 26 | +#include "paimon/io/byte_order.h" |
| 27 | +#include "paimon/result.h" |
| 28 | +#include "paimon/status.h" |
| 29 | +#include "paimon/visibility.h" |
| 30 | + |
| 31 | +namespace paimon { |
| 32 | +class Bytes; |
| 33 | +class InputStream; |
| 34 | + |
| 35 | +/// `DataInputStream` provides a convenient wrapper around `InputStream` for reading typed data. |
| 36 | +/// @note The default byte order is big-endian to maintain compatibility with the Java |
| 37 | +/// implementation. |
| 38 | +class PAIMON_EXPORT DataInputStream { |
| 39 | + public: |
| 40 | + /// Constructs a `DataInputStream` wrapping the given `InputStream`. |
| 41 | + /// @param input_stream The underlying input stream to read from. |
| 42 | + explicit DataInputStream(const std::shared_ptr<InputStream>& input_stream); |
| 43 | + |
| 44 | + /// Seek to a specific position in the underlying input stream. |
| 45 | + /// @param offset The absolute byte offset to seek to. |
| 46 | + Status Seek(int64_t offset) const; |
| 47 | + |
| 48 | + /// Read a typed value from the stream. |
| 49 | + /// @return Result containing the read value or an error status. |
| 50 | + template <typename T> |
| 51 | + Result<T> ReadValue() const; |
| 52 | + |
| 53 | + /// Read some bytes to a `Bytes` object from the stream. The length of bytes is the number of |
| 54 | + /// bytes read from the stream. |
| 55 | + /// @param bytes Buffer to store the read bytes. |
| 56 | + Status ReadBytes(Bytes* bytes) const; |
| 57 | + |
| 58 | + /// Read raw data of specified size from the stream. |
| 59 | + /// @param data Buffer to store the read data. |
| 60 | + /// @param size Number of bytes to read. |
| 61 | + Status Read(char* data, uint32_t size) const; |
| 62 | + |
| 63 | + /// Read string from the stream. |
| 64 | + /// @note First read length (int16), then read string bytes. |
| 65 | + Result<std::string> ReadString() const; |
| 66 | + |
| 67 | + /// Get the current position in the underlying input stream. |
| 68 | + Result<int64_t> GetPos() const; |
| 69 | + |
| 70 | + /// Get the total length of the underlying input stream. |
| 71 | + Result<uint64_t> Length() const; |
| 72 | + |
| 73 | + /// Set the byte order for endianness conversion. |
| 74 | + /// @param order The byte order to use `PAIMON_BIG_ENDIAN` or `PAIMON_LITTLE_ENDIAN`. |
| 75 | + void SetOrder(ByteOrder order) { |
| 76 | + byte_order_ = order; |
| 77 | + } |
| 78 | + |
| 79 | + private: |
| 80 | + /// Validate that the expected number of bytes were read. |
| 81 | + /// @param read_length Expected number of bytes to read. |
| 82 | + /// @param actual_read_length Actual number of bytes read. |
| 83 | + Status AssertReadLength(int32_t read_length, int32_t actual_read_length) const; |
| 84 | + |
| 85 | + /// Check if there are enough bytes available to read. |
| 86 | + /// @param need_length Number of bytes needed. |
| 87 | + Status AssertBoundary(int32_t need_length) const; |
| 88 | + |
| 89 | + /// Determine if byte swapping is needed based on current byte order and system endianness. |
| 90 | + /// @return `true` if byte swapping is required, `false` otherwise. |
| 91 | + bool NeedSwap() const; |
| 92 | + |
| 93 | + private: |
| 94 | + std::shared_ptr<InputStream> input_stream_; |
| 95 | + ByteOrder byte_order_ = ByteOrder::PAIMON_BIG_ENDIAN; |
| 96 | +}; |
| 97 | +} // namespace paimon |
0 commit comments