From 04589b3b7a948e82b755734faf39affe99863e05 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Fri, 13 Oct 2023 22:55:16 +0200 Subject: [PATCH] Fix warning when bytes are missing in Parquet `BinaryBufferReader` (#583) --- .../BinaryReader/BinaryBufferReader.php | 10 +++---- .../BinaryReader/BinaryStreamReader.php | 28 ++++++++++++------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryBufferReader.php b/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryBufferReader.php index a5f23170d..7e568fd77 100644 --- a/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryBufferReader.php +++ b/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryBufferReader.php @@ -58,7 +58,7 @@ public function readBits(int $total) : array $currentBytes = \substr($this->buffer, $bytePosition, $bytesNeeded); for ($i = 0; $i < $bytesNeeded; $i++) { - $byte = \ord($currentBytes[$i]); + $byte = \ord($currentBytes[$i] ?? ''); for ($j = $bitOffset; $j < 8; $j++) { $bits[] = ($byte >> $j) & 1; @@ -240,9 +240,9 @@ public function readInt96() : array /** * @return array */ - public function readInts32(int $count) : array + public function readInts32(int $total) : array { - $intBytes = \array_chunk($this->readBytes(4 * $count)->toArray(), 4); + $intBytes = \array_chunk($this->readBytes(4 * $total)->toArray(), 4); $ints = []; foreach ($intBytes as $bytes) { @@ -256,9 +256,9 @@ public function readInts32(int $count) : array return $ints; } - public function readInts64(int $count) : array + public function readInts64(int $total) : array { - $intBytes = \array_chunk($this->readBytes(8 * $count)->toArray(), 8); + $intBytes = \array_chunk($this->readBytes(8 * $total)->toArray(), 8); $ints = []; diff --git a/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryStreamReader.php b/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryStreamReader.php index 05f102d17..51b6bbeb1 100644 --- a/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryStreamReader.php +++ b/src/lib/parquet/src/Flow/Parquet/BinaryReader/BinaryStreamReader.php @@ -17,6 +17,9 @@ final class BinaryStreamReader implements BinaryReader private int $fileLength; + /** + * @param resource $handle + */ public function __construct(private $handle, private readonly ByteOrder $byteOrder = ByteOrder::LITTLE_ENDIAN) { if (!\is_resource($handle)) { @@ -30,7 +33,7 @@ public function __construct(private $handle, private readonly ByteOrder $byteOrd } \fseek($this->handle, 0, SEEK_END); - $this->fileLength = \ftell($this->handle); + $this->fileLength = \ftell($this->handle) ?: 0; \fseek($this->handle, 0, SEEK_SET); $this->bitPosition = 0; @@ -53,7 +56,7 @@ public function readBit() : int } \fseek($this->handle, \intdiv($this->bitPosition, 8)); - $byte = \ord(\fread($this->handle, 1)); + $byte = \ord(\fread($this->handle, 1) ?: ''); $bit = ($byte >> ($this->bitPosition % 8)) & 1; $this->bitPosition++; @@ -72,7 +75,7 @@ public function readBits(int $total) : array \fseek($this->handle, $bytePosition); while ($total > 0) { - $byte = \ord(\fread($this->handle, 1)); + $byte = \ord(\fread($this->handle, 1) ?: ''); for ($bitOffset = $this->bitPosition % 8; $bitOffset < 8; $bitOffset++) { $bits[] = ($byte >> $bitOffset) & 1; @@ -105,7 +108,7 @@ public function readByte() : int } \fseek($this->handle, $this->bitPosition / 8); - $byte = \ord(\fread($this->handle, 1)); + $byte = \ord(\fread($this->handle, 1) ?: ''); $this->bitPosition += 8; return $byte; @@ -127,10 +130,10 @@ public function readBytes(int $total) : Bytes } \fseek($this->handle, $this->bitPosition / 8); - $bytes = \fread($this->handle, $total); + $bytes = \fread($this->handle, $total) ?: ''; $this->bitPosition += 8 * \strlen($bytes); - return new Bytes(\array_values(\unpack('C*', $bytes))); + return new Bytes(\array_values(\unpack('C*', $bytes) ?: [])); } public function readDouble() : float @@ -193,7 +196,7 @@ public function readInt64() : int } \fseek($this->handle, $this->bitPosition / 8); - $bytes = \array_values(\unpack('C*', \fread($this->handle, 8))); + $bytes = \array_values(\unpack('C*', \fread($this->handle, 8) ?: '') ?: []); $this->bitPosition += 64; return $this->byteOrder === ByteOrder::LITTLE_ENDIAN @@ -203,7 +206,7 @@ public function readInt64() : int | ($bytes[4] << 24) | ($bytes[5] << 16) | ($bytes[6] << 8) | $bytes[7]; } - public function readInt96() : string + public function readInt96() : array { throw new RuntimeException('Not implemented yet.'); } @@ -218,6 +221,11 @@ public function readInts64(int $total) : array throw new RuntimeException('Not implemented yet.'); } + public function readInts96(int $total) : array + { + throw new RuntimeException('Not implemented yet.'); + } + public function readString() : string { // Read the string bytes @@ -236,7 +244,7 @@ public function readUInt32() : int } \fseek($this->handle, $this->bitPosition / 8); - $bytes = \array_values(\unpack('C*', \fread($this->handle, 4))); + $bytes = \array_values(\unpack('C*', \fread($this->handle, 4) ?: '') ?: []); $this->bitPosition += 32; return $this->byteOrder === ByteOrder::LITTLE_ENDIAN @@ -270,7 +278,7 @@ public function readVarInt() : int } \fseek($this->handle, $this->bitPosition / 8); - $byte = \ord(\fread($this->handle, 1)); + $byte = \ord(\fread($this->handle, 1) ?: ''); $this->bitPosition += 8; $result |= ($byte & 0x7F) << $shift;