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