Example usage for java.io DataInputStream markSupported

List of usage examples for java.io DataInputStream markSupported

Introduction

In this page you can find the example usage for java.io DataInputStream markSupported.

Prototype

public boolean markSupported() 

Source Link

Document

Tests if this input stream supports the mark and reset methods.

Usage

From source file:org.apache.hadoop.hbase.HRegionInfo.java

/**
 * Parses an HRegionInfo instance from the passed in stream.  Presumes the HRegionInfo was
 * serialized to the stream with {@link #toDelimitedByteArray()}
 * @param in/*from w w  w.  j  av a  2  s  .  co m*/
 * @return An instance of HRegionInfo.
 * @throws IOException
 */
public static HRegionInfo parseFrom(final DataInputStream in) throws IOException {
    // I need to be able to move back in the stream if this is not a pb serialization so I can
    // do the Writable decoding instead.
    int pblen = ProtobufUtil.lengthOfPBMagic();
    byte[] pbuf = new byte[pblen];
    if (in.markSupported()) { //read it with mark()
        in.mark(pblen);
    }
    int read = in.read(pbuf); //assumption: if Writable serialization, it should be longer than pblen.
    if (read != pblen)
        throw new IOException("read=" + read + ", wanted=" + pblen);
    if (ProtobufUtil.isPBMagicPrefix(pbuf)) {
        return convert(HBaseProtos.RegionInfo.parseDelimitedFrom(in));
    } else {
        // Presume Writables.  Need to reset the stream since it didn't start w/ pb.
        if (in.markSupported()) {
            in.reset();
            HRegionInfo hri = new HRegionInfo();
            hri.readFields(in);
            return hri;
        } else {
            //we cannot use BufferedInputStream, it consumes more than we read from the underlying IS
            ByteArrayInputStream bais = new ByteArrayInputStream(pbuf);
            SequenceInputStream sis = new SequenceInputStream(bais, in); //concatenate input streams
            HRegionInfo hri = new HRegionInfo();
            hri.readFields(new DataInputStream(sis));
            return hri;
        }
    }
}