Skip to content

Commit

Permalink
Fix warning when bytes are missing in Parquet BinaryBufferReader (#583
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stloyd authored Oct 13, 2023
1 parent c5e6c95 commit 04589b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -240,9 +240,9 @@ public function readInt96() : array
/**
* @return array<int>
*/
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) {
Expand All @@ -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 = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand All @@ -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++;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.');
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 04589b3

Please sign in to comment.