Util.php 702 B

1234567891011121314151617181920212223242526
  1. <?php
  2. namespace MaxMind\Db\Reader;
  3. class Util
  4. {
  5. public static function read($stream, $offset, $numberOfBytes)
  6. {
  7. if ($numberOfBytes === 0) {
  8. return '';
  9. }
  10. if (fseek($stream, $offset) === 0) {
  11. $value = fread($stream, $numberOfBytes);
  12. // We check that the number of bytes read is equal to the number
  13. // asked for. We use ftell as getting the length of $value is
  14. // much slower.
  15. if (ftell($stream) - $offset === $numberOfBytes) {
  16. return $value;
  17. }
  18. }
  19. throw new InvalidDatabaseException(
  20. 'The MaxMind DB file contains bad data'
  21. );
  22. }
  23. }