oggpage.java

来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 173 行

JAVA
173
字号
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------------
 */

package p2pradio.sources.ogg;

import p2pradio.Messages;
import java.io.*;
import java.util.*;

public class OggPage
{
	protected byte[] data;
	
	public static final int HEADER_SIZE = 27;
	public static final int MAX_PAGE_SIZE = 65307;
	
	protected LinkedList oggPackets = new LinkedList();
	
	public OggPage(InputStream inputStream) throws IOException, OggException
	{
		super();
		
		DataInputStream input = new DataInputStream(inputStream);
		
		data = new byte[HEADER_SIZE];
		
		// Search signature "OggS"
		input.readFully(data, 0, 4);
		
		boolean signatureFound = false;
		int searchPos = 0;
		
		while (!signatureFound)
		{
			if ((data[0] == 'O') && (data[1] == 'g') && (data[2] == 'g') && (data[3] == 'S')) 
			{
				signatureFound = true;
				break;
			}
			
			// Signature not found - search it
			data[0] = data[1];
			data[1] = data[2];
			data[2] = data[3];
			data[3] = input.readByte();
			
			searchPos++;
			if (searchPos > MAX_PAGE_SIZE)
			{
				throw new OggException(Messages.getString("OggPage.SIGNATURE_NOT_FOUND")); //$NON-NLS-1$
			}
		}
		
		// Read stream structure version
		data[4] = input.readByte();
		
		if (data[4] != 0x00)
		{
			throw new OggException(Messages.getString("OggPage.WRONG_STREAM_STRUCTURE_VERSION")); //$NON-NLS-1$
		}
		
		// Read the rest of the header
		input.readFully(data, 5, 27 - 5);
		
		// Number of page segments
		byte[] segmentTable = new byte[getNumberOfSegments()];
		
		// Read the segment table
		input.readFully(segmentTable);
		
		int segmentsLength = 0;
		
		// Calculate the length of the segments
		for (int i=0; i < segmentTable.length; i++)
		{
			segmentsLength += segmentTable[i] & 0xFF;			
		}
		
		// Assemble the whole packet
		byte[] wholePacket = new byte[HEADER_SIZE + segmentTable.length + segmentsLength];
		System.arraycopy(data, 0, wholePacket, 0, HEADER_SIZE);
		System.arraycopy(segmentTable, 0, wholePacket, HEADER_SIZE, segmentTable.length);
		
		data = wholePacket;
		
		// Read the segments of the packet
		input.readFully(data, HEADER_SIZE + segmentTable.length, segmentsLength);
		
		createOggPackets();
	}
	
	public final boolean isContinuedPage()
	{
		return (data[5] & 0x01) != 0;
	}
	
	public final boolean isBeginOfLogicalBitstream()
	{
		return (data[5] & 0x02) != 0;
	}
	
	public final boolean isEndOfLogicalBitstream()
	{
		return (data[5] & 0x04) != 0;
	}
	
	public final int getNumberOfSegments()
	{
		return (data[26] & 0xFF);
	}
	
	public final int getDataStartPos()
	{
		return HEADER_SIZE + getNumberOfSegments();
	}
	
	private void createOggPackets()
	{
		int packetStartPos = getDataStartPos();
		int packetLength = 0;
				
		for (int i=0; i < getNumberOfSegments(); i++)
		{
			int segmentLength = data[HEADER_SIZE + i] & 0xFF;
			
			packetLength += segmentLength;
			
			if (segmentLength < 255)
			{
				oggPackets.addLast(new OggPacket(this, packetStartPos, packetLength, false));
				
				packetStartPos = packetStartPos + packetLength;
				packetLength = 0;
			}
		}
		
		// Is the last packet continued in the next Ogg page?
		if (packetLength != 0)
		{
			oggPackets.addLast(new OggPacket(this, packetStartPos, packetLength, true));
		}
	}
	
	public byte[] getPageData()
	{
		return data;
	}
	
	public String toString()
	{
		return "Ogg Page: " + data.length + " bytes, " + oggPackets.size() + " packets";  //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?