Read an array of metadata blocks.
@return  The array of metadata blocks
@throws IOException  On read error
514406
public Metadata[] readMetadata() throws IOException {
        readStreamSync();
        ArrayList<Metadata> metadataList = new ArrayList<Metadata>();
        Metadata metadata;
        do {
            metadata = readNextMetadata();
            metadataList.add(metadata);
        } while (!metadata.isLast());
        return (Metadata[])metadataList.toArray(new Metadata[0]);
    }
414980
protected function fetchMetadataBlocks()
    {
        $isLastMetadataBlock = false;
        $this->audioMd5;

        while (!$isLastMetadataBlock && !feof($this->fileHandle)) {
            $metadataBlockHeader = unpack('nlast_type/X/X/Nlength', $this->read(4));
            $isLastMetadataBlock = (bool)($metadataBlockHeader['last_type'] >> 15); // the first bit defines if this is the last meta block
            $metadataBlockType = $metadataBlockHeader['last_type'] >> 8 & 127; // bits 2-8 (7bit) of 16 define the block type
            $metadataBlockLength = $metadataBlockHeader['length'] & 16777215; // bits 9-32 (24bit) of 32 define block length

            // Streaminfo
            if ($metadataBlockType == self::METADATA_BLOCK_STREAMINFO) {

                if (array_sum($this->metadataBlockCounts) > 0) {
                    throw new UnexpectedValueException('METADATA_BLOCK_STREAMINFO must be the first metadata block', self::E_METADATA_BLOCK);
                }

                if ($this->metadataBlockCounts[self::METADATA_BLOCK_STREAMINFO] > 0) {
                    throw new UnexpectedValueException('METADATA_BLOCK_STREAMINFO must occur only once', self::E_METADATA_BLOCK);
                }

                $metadataBlockData = unpack(
                    'nminBlockSize/nmaxBlockSize/NminFrameSize/X/NmaxFrameSize/X/N2samplerate_channels_bitrate_samples/H32md5',
                    $this->read($metadataBlockLength)
                );
                $metadataBlockData['samplerate_channels_bitrate_samples'] = $metadataBlockData['samplerate_channels_bitrate_samples1'] << 32 | $metadataBlockData['samplerate_channels_bitrate_samples2'];
                $sampleRate = $metadataBlockData['samplerate_channels_bitrate_samples'] >> 44;

                if ($metadataBlockData['minBlockSize'] < self::BLOCK_SIZE_MIN) {
                    throw new UnexpectedValueException(
                        sprintf('Minimum block size of %d is less than the allowed minimum of %d', $metadataBlockData['minBlockSize'], self::BLOCK_SIZE_MIN),
                        self::E_METADATA_BLOCK_DATA
                    );
                }

                if ($metadataBlockData['maxBlockSize'] > self::BLOCK_SIZE_MAX) {
                    throw new UnexpectedValueException(
                        sprintf('Maximum block size of %d is more than the allowed maximum of %d', $metadataBlockData['maxBlockSize'], self::BLOCK_SIZE_MAX),
                        self::E_METADATA_BLOCK_DATA
                    );
                }

                if ($metadataBlockData['minBlockSize'] > $metadataBlockData['maxBlockSize']) {
                    throw new UnexpectedValueException(
                        sprintf('Minimum block size of %d must not be more than maximum block size of %d', $metadataBlockData['minBlockSize'], $metadataBlockData['maxBlockSize']),
                        self::E_METADATA_BLOCK_DATA
                    );
                }

                if ($sampleRate < self::SAMPLE_RATE_MIN || $sampleRate > self::SAMPLE_RATE_MAX) {
                    throw new UnexpectedValueException(
                        sprintf('Sample rate of %d is invalid. It must be within the range of %d-%d.', $sampleRate, self::SAMPLE_RATE_MIN, self::SAMPLE_RATE_MAX),
                        self::E_METADATA_BLOCK_DATA
                    );
                }

                if (!preg_match('/^[0-9a-f]{32}$/', $metadataBlockData['md5'])) {
                    throw new UnexpectedValueException('Invalid MD5 hash', self::E_METADATA_BLOCK_DATA);
                }

                $this->metadataBlockCounts[self::METADATA_BLOCK_STREAMINFO]++;
                $this->streamBlockSizeMin = $metadataBlockData['minBlockSize'];
                $this->streamBlockSizeMax = $metadataBlockData['maxBlockSize'];
                $this->streamFrameSizeMin = $metadataBlockData['minFrameSize'] >> 8;
                $this->streamFrameSizeMax = $metadataBlockData['maxFrameSize'] >> 8;
                $this->sampleRate = $sampleRate;
                $this->channels = ($metadataBlockData['samplerate_channels_bitrate_samples'] >> 41 & 7) + 1;
                $this->bitsPerSample = ($metadataBlockData['samplerate_channels_bitrate_samples'] >> 36 & 31) + 1;
                $this->totalSamples = $metadataBlockData['samplerate_channels_bitrate_samples'] & 68719476735;
                $this->duration = $this->totalSamples / $this->sampleRate;
                $this->audioMd5 = $metadataBlockData['md5'];
            }

            // Padding
            elseif ($metadataBlockType == self::METADATA_BLOCK_PADDING) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_PADDING]++;
                fseek($this->fileHandle, $metadataBlockLength, SEEK_CUR);
            }

            // Application
            elseif ($metadataBlockType == self::METADATA_BLOCK_APPLICATION) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_APPLICATION]++;
                fseek($this->fileHandle, $metadataBlockLength, SEEK_CUR);
            }

            // Seektable
            elseif ($metadataBlockType == self::METADATA_BLOCK_SEEKTABLE) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_SEEKTABLE]++;
                fseek($this->fileHandle, $metadataBlockLength, SEEK_CUR);
            }

            // Vorbis Comment
            elseif ($metadataBlockType == self::METADATA_BLOCK_VORBIS_COMMENT) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_VORBIS_COMMENT]++;
                $this->vorbisComment = [];

                $metadataBlockRaw = $this->read($metadataBlockLength);
                $rawPosition = 0;

                $metadataBlockData = unpack('V', substr($metadataBlockRaw, $rawPosition, 4));
                $this->vorbisComment['vendorLength'] = $metadataBlockData[1];
                $rawPosition += 4;

                $this->vorbisComment['vendorString'] = substr($metadataBlockRaw, $rawPosition, $this->vorbisComment['vendorLength']);
                $rawPosition += $this->vorbisComment['vendorLength'];

                $metadataBlockData = unpack('V', substr($metadataBlockRaw, $rawPosition, 4));
                $commentsLength = $metadataBlockData[1];
                $rawPosition += 4;

                for ($i = 0; $i < $commentsLength; $i++) {
                    $metadataBlockData = unpack('V', substr($metadataBlockRaw, $rawPosition, 4));
                    $commentSize = $metadataBlockData[1];
                    $rawPosition += 4;

                    $comment = substr($metadataBlockRaw, $rawPosition, $commentSize);
                    $rawPosition += $commentSize;

                    if (!$delimiterPosition = strpos($comment, '=')) {
                        throw new UnexpectedValueException('Vorbis comment must contain "=" as delimiter', self::E_METADATA_BLOCK_DATA);
                    }

                    $field = strtolower(substr($comment, 0, $delimiterPosition));
                    $value = substr($comment, $delimiterPosition + 1);

                    $this->vorbisComment['comments'][$field][] = $value;
                }
            }

            // Cuesheet
            elseif ($metadataBlockType == self::METADATA_BLOCK_CUESHEET) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_CUESHEET] += 1;
                fseek($this->fileHandle, $metadataBlockLength, SEEK_CUR);
            }

            // Picture
            elseif ($metadataBlockType == self::METADATA_BLOCK_PICTURE) {
                $this->metadataBlockCounts[self::METADATA_BLOCK_PICTURE] += 1;
                fseek($this->fileHandle, $metadataBlockLength, SEEK_CUR);
            }

            elseif ($metadataBlockType > 126) {
                throw new UnexpectedValueException(
                    sprintf('Invalid metadata block type: %d', $metadataBlockType),
                    self::E_METADATA_BLOCK
                );
            }

        }

    }
894990
private boolean readMetaData() throws InvalidDataException {
		final ByteBuffer buf = storage.get(0);
		int magic1, magic2, t_b_order_leaf, t_b_order_internal, t_blockSize; // sanity
		boolean isClean = false;
		magic1 = buf.getInt();
		if (magic1 != MAGIC_1) {
			throw new InvalidDataException("Invalid metadata (MAGIC1)");
		}
		t_blockSize = buf.getInt();
		if (t_blockSize != blockSize) {
			throw new InvalidDataException("Invalid metadata (blockSize) " + t_blockSize + " != " + blockSize);
		}
		t_b_order_leaf = buf.getInt();
		t_b_order_internal = buf.getInt();
		if (t_b_order_leaf != b_order_leaf) {
			throw new InvalidDataException("Invalid metadata (b-order leaf) " + t_b_order_leaf + " != "
					+ b_order_leaf);
		}
		if (t_b_order_internal != b_order_internal) {
			throw new InvalidDataException("Invalid metadata (b-order internal) " + t_b_order_internal
					+ " != " + b_order_internal);
		}
		storageBlock = buf.getInt();
		rootIdx = buf.getInt();
		lowIdx = buf.getInt();
		highIdx = buf.getInt();
		elements = buf.getInt();
		height = buf.getInt();
		maxInternalNodes = buf.getInt();
		maxLeafNodes = buf.getInt();
		isClean = ((buf.get() == ((byte) 0xEA)) ? true : false);
		magic2 = buf.getInt();
		if (magic2 != MAGIC_2) {
			throw new InvalidDataException("Invalid metadata (MAGIC2)");
		}
		if (log.isDebugEnabled()) {
			log.debug(this.getClass().getName() + "::readMetaData() elements=" + elements + " rootIdx="
					+ rootIdx);
		}
		storage.release(buf);
		// Clear Caches
		clearReadCaches();
		clearWriteCaches();
		if (isClean && fileFreeBlocks.exists()) {
			try {
				final SimpleBitSet newFreeBlocks = SimpleBitSet.deserializeFromFile(fileFreeBlocks);
				freeBlocks = newFreeBlocks;
			} catch (IOException e) {
				log.error("IOException in readMetaData()", e);
			}
		}
		return isClean;
	}
110684
public byte[] readBytes()
        throws IOException
    {
        int tag = is.read();

        if (tag == 'N')
            return null;

        if (tag != 'B')
            throw expect("bytes", tag);

        int b16 = is.read();
        int b8 = is.read();

        int len = (b16 << 8) + b8;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        for (int i = 0; i < len; i++)
            bos.write(is.read());

        return bos.toByteArray();
    }
1311060
protected int readBlock() throws IOException {
        blockSize = in.read();
        if (blockSize <= 0) {
            return blockSize = 0;
        }
        for (int k = 0; k < blockSize; ++k) {
            int v = in.read();
            if (v < 0) {
                return blockSize = k;
            }
            block[k] = (byte)v;
        }
        return blockSize;
    }
108920
private DataBlock getWriteDataBlock() throws IOException {
        if (writableBlocks.size() != 0) {
            return writableBlocks.get(0);
        }

        lock.lock();
        try {
            if (writableBlocks.size() != 0) {
                return writableBlocks.get(0);
            }
            DataBlock dataBlock = new DataBlock(storeConfig);
            NAME_BLOCK_MAP.put(dataBlock.getFileId(), dataBlock);
            writableBlocks.add(dataBlock);
            return dataBlock;
        } finally {
            lock.unlock();
        }
    }
837868
public byte[] getBytes() throws IOException {
        if(cachedData == null){
            if(getReader().available() > 0) {
                cachedData = getReader().read();
            }
            else{
                cachedData = new byte[0];
            }
        }
        return cachedData;
    }
585305
private static int readDataBlock(String text, int offset, int length, List<byte[]> blocks)
   {
      int bytes = length / 2;
      byte[] data = new byte[bytes];

      for (int index = 0; index < bytes; index++)
      {
         data[index] = (byte) Integer.parseInt(text.substring(offset, offset + 2), 16);
         offset += 2;
      }

      blocks.add(data);
      return (offset);
   }
1113279
public BlocksWithLocations getBlocks(DatanodeInfo datanode, long size)
  throws IOException {
    if(size <= 0) {
      throw new IllegalArgumentException(
        "Unexpected not positive size: "+size);
    }

    return namesystem.getBlocks(datanode, size);
  }
371090
protected InputStream readFiles() throws IOException {
    BufferedInputStream input = new BufferedInputStream(openFile(), BUFFER_SIZE);
    input.mark(100);
    try {
      return new XZInputStream(input);
    } catch (IOException e) {
      input.reset();
    }
    try {
      return new CompressorStreamFactory().createCompressorInputStream(input);
    } catch (CompressorException e) {
      input.reset();
    }
    try {
      return new ArchiveStreamFactory().createArchiveInputStream(input);
    } catch (ArchiveException e) {
      input.reset();
    }
    return input;
  }
965069
public byte[] readToBytes() {
        try {
            try (InputStream in = body()) {
                return InputStreams.readAll(in);
            }
        } catch (IOException e) {
            throw new RequestsException(e);
        } finally {
            close();
        }
    }
805032
@Override
  public BlockMeta getVolatileBlockMeta(long blockId) throws BlockDoesNotExistException {
    LOG.debug("getVolatileBlockMeta: blockId={}", blockId);
    try (LockResource r = new LockResource(mMetadataReadLock)) {
      return mMetaManager.getBlockMeta(blockId);
    }
  }
352966
public List<MapRow> read() throws IOException
   {
      List<MapRow> result = new ArrayList<MapRow>();
      int fileCount = m_stream.readInt();
      if (fileCount != 0)
      {
         for (int index = 0; index < fileCount; index++)
         {
            // We use a LinkedHashMap to preserve insertion order in iteration
            // Useful when debugging the file format.
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            readBlock(map);
            result.add(new MapRow(map));
         }
      }
      return result;
   }
377599
public String readAll() throws IOException {

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = readLine()) != null) {
            sb.append(line);
        }

        return sb.toString();
    }
963933
private static BlockMetadataHeader readHeader(short version, DataInputStream in) 
                                   throws IOException {
    DataChecksum checksum = DataChecksum.newDataChecksum(in);
    return new BlockMetadataHeader(version, checksum);
  }
907626
private boolean readMetaData()
    throws IOException
  {
    SegmentExtent metaExtentInit = new SegmentExtent(0, 0, META_SEGMENT_SIZE);
    
    try (InSegment reader = openRead(metaExtentInit)) {
      ReadStream is = reader.in();
      
      if (! readMetaDataHeader(is)) {
        return false;
      }
      
      _segmentId = 1;
    }
    
    int metaLength = _segmentMeta[0].size();
    
    SegmentExtent metaExtent = new SegmentExtent(0, 0, metaLength);
    
    _metaExtents.clear();
    _metaExtents.add(metaExtent);
    
    _metaAddress = 0;
    _metaOffset = META_OFFSET;
    _metaTail = _metaOffset + metaLength;
    
    while (true) {
      try (InSegment reader = openRead(metaExtent)) {
        ReadStream is = reader.in();
        
        if (metaExtent.address() == 0) {
          is.position(META_OFFSET);
        }
        
        long metaAddress = _metaAddress;

        while (readMetaDataEntry(is)) {
        }
        
        if (_metaAddress == metaAddress) {
          return true;
        }
        
        metaExtent = new SegmentExtent(0, _metaAddress, metaLength);
      }
    }
  }
869351
public boolean readMetaDataIfNeeded() throws IOException {
    if (eofReached) {
      return false;
    }
    if (rawBlockOffset == 0) {
      try {
        metaDataConsumer.readMetaData(in, metaDataBlockSize);
        rawBlockOffset += metaDataBlockSize;
      } catch (EOFException e) {
        eofReached = true;
        return false;
      }
    }
    return true;
  }