format/src/main/java/com/github/sadikovi/riff/FileHeader.java
/**
   * Read header from input stream.
   * Stream is not closed after operation is complete.
   * @param in input stream
   * @throws IOException
   */
Read header from input stream.
Stream is not closed after operation is complete.
@param in input stream
@throws IOException
Read header from input stream . Stream is not closed after operation is complete . @param in input stream @throws IOException
public static FileHeader readFrom(FSDataInputStream in) throws IOException {
    long meta = in.readLong();
    int magic = (int) (meta >>> 32);
    if (magic != Riff.MAGIC) throw new IOException("Wrong magic: " + magic + " != " + Riff.MAGIC);
    int len = (int) (meta & 0x7fffffff);
    LOG.debug("Read header content of {} bytes", len);
    ByteBuffer buffer = ByteBuffer.allocate(len);
    in.readFully(buffer.array(), buffer.arrayOffset(), buffer.limit());
    byte[] state = new byte[STATE_LENGTH];
    buffer.get(state);
    ByteBufferStream byteStream = new ByteBufferStream(buffer);
    TypeDescription td = TypeDescription.readFrom(byteStream);
    int propertiesLen = buffer.getInt();
    HashMap<String, String> properties = null;
    if (propertiesLen >= 0) {
      properties = new HashMap<String, String>();
      byte[] bytes = null;
      while (propertiesLen > 0) {
        bytes = new byte[buffer.getInt()];
        buffer.get(bytes);
        String key = new String(bytes);
        bytes = new byte[buffer.getInt()];
        buffer.get(bytes);
        String value = new String(bytes);
        properties.put(key, value);
        --propertiesLen;
      }
    }
    return new FileHeader(state, td, properties);
  }
public static FileHeader readFrom ( FSDataInputStream in ) throws IOException { long meta = in . readLong ( ) ; int magic = ( int ) ( meta >>> 32 ) ; if ( magic != Riff . MAGIC ) throw new IOException ( "Wrong magic: " + magic + " != " + Riff . MAGIC ) ; int len = ( int ) ( meta & 0x7fffffff ) ; LOG . debug ( "Read header content of {} bytes" , len ) ; ByteBuffer buffer = ByteBuffer . allocate ( len ) ; in . readFully ( buffer . array ( ) , buffer . arrayOffset ( ) , buffer . limit ( ) ) ; byte [ ] state = new byte [ STATE_LENGTH ] ; buffer . get ( state ) ; ByteBufferStream byteStream = new ByteBufferStream ( buffer ) ; TypeDescription td = TypeDescription . readFrom ( byteStream ) ; int propertiesLen = buffer . getInt ( ) ; HashMap < String , String > properties = null ; if ( propertiesLen >= 0 ) { properties = new HashMap < String , String > ( ) ; byte [ ] bytes = null ; while ( propertiesLen > 0 ) { bytes = new byte [ buffer . getInt ( ) ] ; buffer . get ( bytes ) ; String key = new String ( bytes ) ; bytes = new byte [ buffer . getInt ( ) ] ; buffer . get ( bytes ) ; String value = new String ( bytes ) ; properties . put ( key , value ) ; -- propertiesLen ; } } return new FileHeader ( state , td , properties ) ; }
Read header from input stream.
Read header from input stream .
// Read first 8 bytes: magic 4 bytes and length of the header 4 bytes // read full header bytes // no flip - we have not reset position // read byte state // read type description // currently type description supports serde from stream, therefore we create stream that // wraps byte buffer, stream updates position of buffer // read properties // read key // read value