001package net.bramp.ffmpeg.nut;
002
003import com.google.common.base.MoreObjects;
004import java.io.IOException;
005
006public class PacketHeader {
007
008  long startcode;
009  long forwardPtr;
010  int checksum; // header checksum
011
012  long end; // End byte of packet
013
014  public void read(NutDataInputStream in, long startcode) throws IOException {
015    this.startcode = startcode;
016    forwardPtr = in.readVarLong();
017    if (forwardPtr > 4096) {
018      long expected = in.getCRC();
019      checksum = in.readInt();
020      if (checksum != expected) {
021        // TODO This code path has never been tested.
022        throw new IOException(
023            String.format("invalid header checksum %X want %X", expected, checksum));
024      }
025    }
026
027    in.resetCRC();
028    end = in.offset() + forwardPtr - 4; // 4 bytes for footer CRC
029  }
030
031  @Override
032  public String toString() {
033    MoreObjects.ToStringHelper helper =
034        MoreObjects.toStringHelper(this)
035            .add("startcode", Packet.Startcode.toString(startcode))
036            .add("forwardPtr", forwardPtr);
037
038    if (forwardPtr > 4096) {
039      helper = helper.add("checksum", checksum);
040    }
041    return helper.toString();
042  }
043}