FileDocCategorySizeDatePackage
OggPageHeader.javaAPI DocJaudiotagger 2.0.413646Thu Apr 28 13:34:48 BST 2011org.jaudiotagger.audio.ogg.util

OggPageHeader

public class OggPageHeader extends Object
$Id: OggPageHeader.java 965 2011-04-28 11:34:43Z paultaylor $

reference:http://xiph.org/ogg/doc/framing.html

author
Raphael Slinckx (KiKiDonK)
version
16 d�cembre 2003

Fields Summary
public static Logger
logger
public static final byte[]
CAPTURE_PATTERN
public static final int
OGG_PAGE_HEADER_FIXED_LENGTH
public static final int
MAXIMUM_NO_OF_SEGMENT_SIZE
public static final int
MAXIMUM_SEGMENT_SIZE
public static final int
MAXIMUM_PAGE_HEADER_SIZE
public static final int
MAXIMUM_PAGE_DATA_SIZE
public static final int
MAXIMUM_PAGE_SIZE
public static final int
FIELD_CAPTURE_PATTERN_POS
public static final int
FIELD_STREAM_STRUCTURE_VERSION_POS
public static final int
FIELD_HEADER_TYPE_FLAG_POS
public static final int
FIELD_ABSOLUTE_GRANULE_POS
public static final int
FIELD_STREAM_SERIAL_NO_POS
public static final int
FIELD_PAGE_SEQUENCE_NO_POS
public static final int
FIELD_PAGE_CHECKSUM_POS
public static final int
FIELD_PAGE_SEGMENTS_POS
public static final int
FIELD_SEGMENT_TABLE_POS
public static final int
FIELD_CAPTURE_PATTERN_LENGTH
public static final int
FIELD_STREAM_STRUCTURE_VERSION_LENGTH
public static final int
FIELD_HEADER_TYPE_FLAG_LENGTH
public static final int
FIELD_ABSOLUTE_GRANULE_LENGTH
public static final int
FIELD_STREAM_SERIAL_NO_LENGTH
public static final int
FIELD_PAGE_SEQUENCE_NO_LENGTH
public static final int
FIELD_PAGE_CHECKSUM_LENGTH
public static final int
FIELD_PAGE_SEGMENTS_LENGTH
private byte[]
rawHeaderData
private double
absoluteGranulePosition
private int
checksum
private byte
headerTypeFlag
private boolean
isValid
private int
pageLength
private int
pageSequenceNumber
private int
streamSerialNumber
private byte[]
segmentTable
private List
packetList
private boolean
lastPacketIncomplete
private long
startByte
Constructors Summary
public OggPageHeader(byte[] b)

        this.rawHeaderData = b;
        int streamStructureRevision = b[FIELD_STREAM_STRUCTURE_VERSION_POS];
        headerTypeFlag = b[FIELD_HEADER_TYPE_FLAG_POS];
        if (streamStructureRevision == 0)
        {
            this.absoluteGranulePosition = 0;
            for (int i = 0; i < FIELD_ABSOLUTE_GRANULE_LENGTH; i++)
            {
                this.absoluteGranulePosition += u(b[i + FIELD_ABSOLUTE_GRANULE_POS]) * Math.pow(2, 8 * i);
            }

            streamSerialNumber = Utils.getIntLE(b, FIELD_STREAM_SERIAL_NO_POS, 17);
            pageSequenceNumber = Utils.getIntLE(b, FIELD_PAGE_SEQUENCE_NO_POS, 21);
            checksum = Utils.getIntLE(b, FIELD_PAGE_CHECKSUM_POS, 25);
            int pageSegments = u(b[FIELD_PAGE_SEGMENTS_POS]);

            this.segmentTable = new byte[b.length - OGG_PAGE_HEADER_FIXED_LENGTH];
            int packetLength = 0;
            Integer segmentLength = null;
            for (int i = 0; i < segmentTable.length; i++)
            {
                segmentTable[i] = b[OGG_PAGE_HEADER_FIXED_LENGTH + i];
                segmentLength = u(segmentTable[i]);
                this.pageLength += segmentLength;
                packetLength += segmentLength;

                if (segmentLength < MAXIMUM_SEGMENT_SIZE)
                {
                    packetList.add(new PacketStartAndLength(pageLength - packetLength, packetLength));
                    packetLength = 0;
                }
            }

            //If last segment value is 255 this packet continues onto next page
            //and will not have been added to the packetStartAndEnd list yet
            if(segmentLength!=null)
            {
                if (segmentLength == MAXIMUM_SEGMENT_SIZE)
                {
                    packetList.add(new PacketStartAndLength(pageLength - packetLength, packetLength));
                    lastPacketIncomplete = true;
                }
            }
            isValid = true;
        }

        if(logger.isLoggable(Level.CONFIG))
        {
            logger.config("Constructed OggPage:" + this.toString());
        }
    
Methods Summary
public doublegetAbsoluteGranulePosition()

        logger.fine("Number Of Samples: " + absoluteGranulePosition);
        return this.absoluteGranulePosition;
    
public intgetCheckSum()

        return checksum;
    
public bytegetHeaderType()

        return headerTypeFlag;
    
public java.util.ListgetPacketList()

return
a list of packet start position and size within this page.

        return packetList;
    
public intgetPageLength()

        logger.fine("This page length: " + pageLength);
        return this.pageLength;
    
public intgetPageSequence()

        return pageSequenceNumber;
    
public byte[]getRawHeaderData()

return
the raw header data that this pageheader is derived from

        return rawHeaderData;
    
public byte[]getSegmentTable()

        return this.segmentTable;
    
public intgetSerialNumber()

        return streamSerialNumber;
    
public longgetStartByte()
Startbyte of this pageHeader in the file This is useful for Ogg files that contain unsupported additional data at the start of the file such as ID3 data

        return startByte;
    
public booleanisLastPacketIncomplete()

return
true if the last packet on this page extends to the next page

        return lastPacketIncomplete;
    
public booleanisValid()

        return isValid;
    
public static org.jaudiotagger.audio.ogg.util.OggPageHeaderread(java.nio.ByteBuffer byteBuffer)
Read next PageHeader from Buffer

param
byteBuffer
return
throws
IOException
throws
CannotReadException

                     
           
       
           //byteBuffer
           int start = byteBuffer.position();
           logger.fine("Trying to read OggPage at:" + start);

           byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
           byteBuffer.get(b);
           if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
           {
               throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
           }

           byteBuffer.position(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
           int pageSegments = byteBuffer.get() & 0xFF; //unsigned
           byteBuffer.position(start);

           b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
           byteBuffer.get(b);
           OggPageHeader pageHeader = new OggPageHeader(b);

           //Now just after PageHeader, ready for Packet Data
           return pageHeader;
       
public static org.jaudiotagger.audio.ogg.util.OggPageHeaderread(java.io.RandomAccessFile raf)
Read next PageHeader from file

param
raf
return
throws
IOException
throws
CannotReadException

        long start = raf.getFilePointer();
        logger.fine("Trying to read OggPage at:" + start);

        byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
        raf.read(b);
        if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
        {
            raf.seek(start);
            if(AbstractID3v2Tag.isId3Tag(raf))
            {
                logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
                raf.read(b);
                if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
                {
                    //Go to the end of the ID3 header
                    start=raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
                }
            }
            else
            {
                throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
            }
        }

        raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
        int pageSegments = raf.readByte() & 0xFF; //unsigned
        raf.seek(start);

        b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
        raf.read(b);


        OggPageHeader pageHeader = new OggPageHeader(b);
        pageHeader.setStartByte(start);
        //Now just after PageHeader, ready for Packet Data
        return pageHeader;
    
public voidsetStartByte(long startByte)

        this.startByte = startByte;
    
public java.lang.StringtoString()

        String out = "Ogg Page Header:isValid:" + isValid + ":type:" + headerTypeFlag + ":oggPageHeaderLength:" + rawHeaderData.length + ":length:" + pageLength + ":seqNo:" + getPageSequence() + ":packetIncomplete:" + isLastPacketIncomplete() + ":serNum:" + this.getSerialNumber();

        for (PacketStartAndLength packet : getPacketList())
        {
            out += packet.toString();
        }
        return out;
    
private intu(int i)

        return i & 0xFF;