shoutcastmetadata.java
来自「java语言开发的P2P流媒体系统」· Java 代码 · 共 137 行
JAVA
137 行
/*
* 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;
import p2pradio.Metadata;
/**
* Translates Shoutcast Metadata to a {@link Metadata} object and back.
*
* @author Michael Kaufmann
*/
public class ShoutcastMetadata
{
private ShoutcastMetadata()
{
// Keine Instanzen
}
// Beispiel f黵 einen Shoutcast-Metadatenstring:
// "StreamTitle='Beatles - Hello Goodbye';StreamUrl='';"
public static void shoutcastToMetadata(byte[] shoutcastMetadataBytes, int shoutcastMetadataBytesLength, Metadata metadata)
{
// Den Metadatenstring extrahieren
String shoutcastMetadata = new String(shoutcastMetadataBytes, 0, shoutcastMetadataBytesLength);
// Bis zu 15 Null-Bytes k鰊nten vorkommen
shoutcastMetadata = shoutcastMetadata.trim();
// Lied-Titel auslesen
String songTitle = null;
String streamTitle = "streamtitle='"; //$NON-NLS-1$
String streamUrl = "streamurl='"; //$NON-NLS-1$
int titlePos = shoutcastMetadata.toLowerCase().indexOf(streamTitle);
if (titlePos != -1)
{
titlePos += streamTitle.length();
int titleEndPos = shoutcastMetadata.indexOf("';", titlePos); //$NON-NLS-1$
if (titleEndPos != -1)
{
songTitle = shoutcastMetadata.substring(titlePos, titleEndPos);
}
}
if (songTitle == null)
{
songTitle = Metadata.SONG_TITLE_DEFAULT;
}
// Lied-URL auslesen
String songURL = null;
int urlPos = shoutcastMetadata.toLowerCase().indexOf(streamUrl);
if (urlPos != -1)
{
urlPos += streamUrl.length();
int urlEndPos = shoutcastMetadata.indexOf("';", urlPos); //$NON-NLS-1$
if (urlEndPos != -1)
{
songURL = shoutcastMetadata.substring(urlPos, urlEndPos);
}
}
if (songURL == null)
{
songURL = Metadata.SONG_URL_DEFAULT;
}
// In die Metadaten einf黦en
metadata.setSongTitle(songTitle);
metadata.setSongURL(songURL);
}
public static int metadataToShoutcast(Metadata metadata, byte[] shoutcastMetadataBytes)
{
// Den Shoutcast-Metadatenstring erstellen
String shoutcastMetadataString = "StreamTitle='" + metadata.getSongTitle() + "';StreamUrl='" + metadata.getSongURL() + "';"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
// In ein Byte-Array konvertieren
byte[] shoutcastMetadataStringBytes = shoutcastMetadataString.getBytes();
int length = shoutcastMetadataStringBytes.length;
if (length % 16 != 0)
{
// Die L鋘ge durch 16 teilbar machen
int newlength = length - (length % 16) + 16;
// Array kopieren
System.arraycopy(shoutcastMetadataStringBytes, 0, shoutcastMetadataBytes, 0, length);
// Den Rest mit Nullen auff黮len
java.util.Arrays.fill(shoutcastMetadataBytes, length, newlength, (byte)0);
length = newlength;
}
else
{
// Array kopieren
System.arraycopy(shoutcastMetadataStringBytes, 0, shoutcastMetadataBytes, 0, length);
}
return length;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?