📄 playerinputstream.java
字号:
/*
* 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.players;
import p2pradio.Messages;
import p2pradio.event.*;
import p2pradio.logging.Logger;
import java.io.InputStream;
import java.io.IOException;
/**
* Takes the data that a {@link ListenBuffer} provides and allows it
* to be accessed as a usual {@link java.io.InputStream}.
*
* @author Michael Kaufmann
*/
public class PlayerInputStream extends InputStream implements DataLeapListener
{
private ListenBuffer buffer;
// Das "angebrauchte" Datenpaket
private byte[] currentData;
private int dataOffset;
private int dataLength;
// Wieviel wurde vom "angebrauchten" Datenpaket schon gelesen?
private int alreadyRead = 0;
// Ist es das erste Mal, dass Daten abgeholt werden?
private boolean firstTime = true;
private boolean continueIfDataLeap;
private boolean dataLeap = false;
/**
* Creates a Player Input Stream with <code>player</code> as source.
* If the consumer of this input stream is too slow <code>continueIfTooSlow</code> determines
* if the input stream should skip some stream data and continue, or if the stream should end.
*
*/
public PlayerInputStream(ListenBuffer buffer, boolean continueIfDataLeap)
{
super();
this.buffer = buffer;
this.continueIfDataLeap = continueIfDataLeap;
buffer.addDataLeapListener(this);
}
public void dataLeap(DataLeapEvent e)
{
dataLeap = true;
}
public int available() throws IOException
{
// Die Anzahl restliche Bytes im Datenpaket zur點kgeben
return dataLength - alreadyRead;
}
public int read() throws IOException
{
if (currentData == null)
{
try
{
getNextData();
}
catch (IOException e)
{
// Player ist zu langsam
return -1;
}
}
int value = (currentData[dataOffset + alreadyRead] & 0xff);
alreadyRead++;
if (alreadyRead == dataLength)
{
// N鋍hstes Mal m黶sen neue Daten besorgt werden
currentData = null;
}
return value;
}
public int read(byte b[], int off, int len) throws IOException
{
if (b == null)
{
throw new NullPointerException();
}
else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0))
{
throw new IndexOutOfBoundsException();
}
else if (len == 0)
{
return 0;
}
if (currentData == null)
{
try
{
getNextData();
}
catch (IOException e)
{
// Player ist zu langsam
return -1;
}
}
// Restliche Bytes des Datenpakets zur點kgeben
// (kein neues Paket anfordern, sonst k鰊nte die Methode blockieren)
int bytesToRead = Math.min(available(), len);
System.arraycopy(currentData, dataOffset + alreadyRead, b, off, bytesToRead);
alreadyRead += bytesToRead;
if (alreadyRead == dataLength)
{
// N鋍hstes Mal m黶sen neue Daten besorgt werden
currentData = null;
}
return bytesToRead;
}
private void getNextData() throws IOException
{
// Die Daten abholen
if (firstTime)
{
firstTime = false;
}
else
{
buffer.nextDataPacket();
}
if (dataLeap)
{
Logger.finer("PlayerInputStream", "PlayerInputStream.DATA_LEAP"); //$NON-NLS-1$ //$NON-NLS-2$
if (!continueIfDataLeap)
{
throw new IOException(Messages.getString("PlayerInputStream.DATA_LEAP")); //$NON-NLS-1$
}
}
currentData = buffer.getData();
dataOffset = buffer.getDataOffset();
dataLength = buffer.getDataLength();
alreadyRead = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -