Untabifying, widening, nixed wacky client version parsing.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2728 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -28,10 +28,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The CDDB class provides access to the information provided by servers
|
* The CDDB class provides access to the information provided by servers compliant with the
|
||||||
* compliant with the
|
* <a href="http://www.freedb.org/software/old/CDDBPROTO">CDDB protocol</a>.
|
||||||
* <a href="http://www.freedb.org/software/old/CDDBPROTO">CDDB
|
|
||||||
* protocol</a>.
|
|
||||||
*/
|
*/
|
||||||
public class CDDB
|
public class CDDB
|
||||||
{
|
{
|
||||||
@@ -48,12 +46,11 @@ public class CDDB
|
|||||||
/**
|
/**
|
||||||
* The client version reported to the CDDB server.
|
* The client version reported to the CDDB server.
|
||||||
*/
|
*/
|
||||||
public static String CLIENT_VERSION; // assigned during static init
|
public static final String CLIENT_VERSION = "1.0";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class encapsulates the information needed to look up a full
|
* This class encapsulates the information needed to look up a full CDDB record for a
|
||||||
* CDDB record for a particular disc. An array of them are returned in
|
* particular disc. An array of them are returned in response to a CDDB query.
|
||||||
* response to a CDDB query.
|
|
||||||
*/
|
*/
|
||||||
public static class Entry
|
public static class Entry
|
||||||
{
|
{
|
||||||
@@ -67,12 +64,10 @@ public class CDDB
|
|||||||
public String title;
|
public String title;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses values for this entry from the supplied source
|
* Parses values for this entry from the supplied source string. The source string should
|
||||||
* string. The source string should contain an entry description
|
* contain an entry description as formatted by the CDDB server.
|
||||||
* as formatted by the CDDB server.
|
|
||||||
*
|
*
|
||||||
* @exception CDDBException Thrown if the entry is not properly
|
* @exception CDDBException Thrown if the entry is not properly formatted.
|
||||||
* formatted.
|
|
||||||
*/
|
*/
|
||||||
public void parse (String source)
|
public void parse (String source)
|
||||||
throws CDDBException
|
throws CDDBException
|
||||||
@@ -81,8 +76,7 @@ public class CDDB
|
|||||||
int sidx2 = source.indexOf(" ", sidx1+1);
|
int sidx2 = source.indexOf(" ", sidx1+1);
|
||||||
|
|
||||||
if (sidx1 == -1 || sidx2 == -1) {
|
if (sidx1 == -1 || sidx2 == -1) {
|
||||||
throw new CDDBException(499, "Malformed entry '" +
|
throw new CDDBException(499, "Malformed entry '" + source + "'");
|
||||||
source + "'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
category = source.substring(0, sidx1);
|
category = source.substring(0, sidx1);
|
||||||
@@ -92,22 +86,18 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects this CDDB instance to the CDDB server running on the
|
* Connects this CDDB instance to the CDDB server running on the supplied host using the
|
||||||
* supplied host using the standard port.
|
* standard port.
|
||||||
*
|
*
|
||||||
* <p> <b>Note well:</b> you must close a CDDB connection once you are
|
* <p> <b>Note well:</b> you must close a CDDB connection once you are done with it, otherwise
|
||||||
* done with it, otherwise the socket connection will remain open and
|
* the socket connection will remain open and pointlessly consume machine resources.
|
||||||
* pointlessly consume machine resources.
|
|
||||||
*
|
*
|
||||||
* @param hostname The host to which to connect.
|
* @param hostname The host to which to connect.
|
||||||
*
|
*
|
||||||
* @exception IOException Thrown if a network error occurs attempting
|
* @exception IOException Thrown if a network error occurs attempting to connect to the host.
|
||||||
* to connect to the host.
|
* @exception CDDBException Thrown if an error occurs after identifying ourselves to the host.
|
||||||
* @exception CDDBException Thrown if an error occurs after
|
|
||||||
* identifying ourselves to the host.
|
|
||||||
*
|
*
|
||||||
* @return The message supplied with the succesful connection
|
* @return The message supplied with the succesful connection response.
|
||||||
* response.
|
|
||||||
*
|
*
|
||||||
* @see #STANDARD_PORT
|
* @see #STANDARD_PORT
|
||||||
* @see #close
|
* @see #close
|
||||||
@@ -119,23 +109,19 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connects this CDDB instance to the CDDB server running on the
|
* Connects this CDDB instance to the CDDB server running on the supplied host using the
|
||||||
* supplied host using the specified port.
|
* specified port.
|
||||||
*
|
*
|
||||||
* <p> <b>Note well:</b> you must close a CDDB connection once you are
|
* <p> <b>Note well:</b> you must close a CDDB connection once you are done with it, otherwise
|
||||||
* done with it, otherwise the socket connection will remain open and
|
* the socket connection will remain open and pointlessly consume machine resources.
|
||||||
* pointlessly consume machine resources.
|
|
||||||
*
|
*
|
||||||
* @param hostname The host to which to connect.
|
* @param hostname The host to which to connect.
|
||||||
* @param port The port number on which to connect to the host.
|
* @param port The port number on which to connect to the host.
|
||||||
*
|
*
|
||||||
* @exception IOException Thrown if a network error occurs attempting
|
* @exception IOException Thrown if a network error occurs attempting to connect to the host.
|
||||||
* to connect to the host.
|
* @exception CDDBException Thrown if an error occurs after identifying ourselves to the host.
|
||||||
* @exception CDDBException Thrown if an error occurs after
|
|
||||||
* identifying ourselves to the host.
|
|
||||||
*
|
*
|
||||||
* @return The message supplied with the succesful connection
|
* @return The message supplied with the succesful connection response.
|
||||||
* response.
|
|
||||||
*
|
*
|
||||||
* @see #close
|
* @see #close
|
||||||
*/
|
*/
|
||||||
@@ -153,10 +139,8 @@ public class CDDB
|
|||||||
// establish our socket connection and IO streams
|
// establish our socket connection and IO streams
|
||||||
InetAddress addr = InetAddress.getByName(hostname);
|
InetAddress addr = InetAddress.getByName(hostname);
|
||||||
_sock = new Socket(addr, port);
|
_sock = new Socket(addr, port);
|
||||||
_in = new BufferedReader(
|
_in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
|
||||||
new InputStreamReader(_sock.getInputStream()));
|
_out = new PrintStream(new BufferedOutputStream(_sock.getOutputStream()));
|
||||||
_out = new PrintStream(
|
|
||||||
new BufferedOutputStream(_sock.getOutputStream()));
|
|
||||||
|
|
||||||
// first read (and discard) the banner string
|
// first read (and discard) the banner string
|
||||||
_in.readLine();
|
_in.readLine();
|
||||||
@@ -180,10 +164,9 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specifies the number of milliseconds that the client should wait
|
* Specifies the number of milliseconds that the client should wait for a response from the
|
||||||
* for a response from the server before aborting. This must be called
|
* server before aborting. This must be called <em>after</em> a successful call to connect,
|
||||||
* <em>after</em> a successful call to connect, otherwise the timeout
|
* otherwise the timeout will not be set.
|
||||||
* will not be set.
|
|
||||||
*/
|
*/
|
||||||
public void setTimeout (int timeout)
|
public void setTimeout (int timeout)
|
||||||
throws SocketException
|
throws SocketException
|
||||||
@@ -202,11 +185,8 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes the connection to the CDDB server previously opened with a
|
* Closes the connection to the CDDB server previously opened with a call to {@link #connect}.
|
||||||
* call to <code>connect()</code>. If the connection was not
|
* If the connection was not previously established, this member function does nothing.
|
||||||
* previously established, this member function does nothing.
|
|
||||||
*
|
|
||||||
* @see #connect
|
|
||||||
*/
|
*/
|
||||||
public void close ()
|
public void close ()
|
||||||
throws IOException
|
throws IOException
|
||||||
@@ -222,20 +202,16 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issues a query to the CDDB server using the supplied CD identifying
|
* Issues a query to the CDDB server using the supplied CD identifying information.
|
||||||
* information.
|
|
||||||
*
|
*
|
||||||
* @param discid The disc identifier (information on how to compute
|
* @param discid The disc identifier (information on how to compute the disc ID is available <a
|
||||||
* the disc ID is available
|
* href="http://www.freedb.org/sections.php?op=viewarticle&artid=6"> here</a>.
|
||||||
* <a href="http://www.freedb.org/sections.php?op=viewarticle&artid=6">
|
* @param frameOffsets The frame offset of each track. The length of this array is assumed to
|
||||||
* here</a>.
|
* be the number of tracks on the CD and is used in the query.
|
||||||
* @param frameOffsets The frame offset of each track. The length of
|
|
||||||
* this array is assumed to be the number of tracks on the CD and is
|
|
||||||
* used in the query.
|
|
||||||
* @param length The length (in seconds) of the CD.
|
* @param length The length (in seconds) of the CD.
|
||||||
*
|
*
|
||||||
* @return If no entry matches the query, null is returned. Otherwise
|
* @return If no entry matches the query, null is returned. Otherwise one or more entries is
|
||||||
* one or more entries is returned that matched the query parameters.
|
* returned that matched the query parameters.
|
||||||
*/
|
*/
|
||||||
public Entry[] query (String discid, int[] frameOffsets, int length)
|
public Entry[] query (String discid, int[] frameOffsets, int length)
|
||||||
throws IOException, CDDBException
|
throws IOException, CDDBException
|
||||||
@@ -287,8 +263,8 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A detail object contains all of the detailed information about a
|
* A detail object contains all of the detailed information about a particular CD as retrieved
|
||||||
* particular CD as retrieved from the CDDB server.
|
* from the CDDB server.
|
||||||
*/
|
*/
|
||||||
public static class Detail
|
public static class Detail
|
||||||
{
|
{
|
||||||
@@ -312,11 +288,10 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Requests the detail information for a particular disc in a
|
* Requests the detail information for a particular disc in a particular category from the CDDB
|
||||||
* particular category from the CDDB server.
|
* server.
|
||||||
*
|
*
|
||||||
* @return A detail instance filled with the information for the
|
* @return A detail instance filled with the information for the requested CD.
|
||||||
* requested CD.
|
|
||||||
*/
|
*/
|
||||||
public Detail read (String category, String discid)
|
public Detail read (String category, String discid)
|
||||||
throws IOException, CDDBException
|
throws IOException, CDDBException
|
||||||
@@ -355,7 +330,6 @@ public class CDDB
|
|||||||
// now parse the contents
|
// now parse the contents
|
||||||
String input = _in.readLine();
|
String input = _in.readLine();
|
||||||
for (int lno = 0; !input.equals(CDDBProtocol.TERMINATOR); lno++) {
|
for (int lno = 0; !input.equals(CDDBProtocol.TERMINATOR); lno++) {
|
||||||
|
|
||||||
if (input.startsWith("#")) {
|
if (input.startsWith("#")) {
|
||||||
// skip comments
|
// skip comments
|
||||||
|
|
||||||
@@ -374,12 +348,10 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (input.startsWith("TTITLE")) {
|
} else if (input.startsWith("TTITLE")) {
|
||||||
append(tnames, index(input, "TTITLE", lno),
|
append(tnames, index(input, "TTITLE", lno), contents(input, lno));
|
||||||
contents(input, lno));
|
|
||||||
|
|
||||||
} else if (input.startsWith("EXTT")) {
|
} else if (input.startsWith("EXTT")) {
|
||||||
append(texts, index(input, "EXTT", lno),
|
append(texts, index(input, "EXTT", lno), contents(input, lno));
|
||||||
contents(input, lno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read in the next line of input
|
// read in the next line of input
|
||||||
@@ -396,48 +368,43 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the track index of the supplied line (the number
|
* Extracts the track index of the supplied line (the number immediately following the supplied
|
||||||
* immediately following the supplied prefix and preceding the equals
|
* prefix and preceding the equals sign) or throws a CDDBException if the line contains no
|
||||||
* sign) or throws a CDDBException if the line contains no equals sign
|
* equals sign or track index.
|
||||||
* or track index.
|
|
||||||
*/
|
*/
|
||||||
protected final int index (String source, String prefix, int lineno)
|
protected final int index (String source, String prefix, int lineno)
|
||||||
throws CDDBException
|
throws CDDBException
|
||||||
{
|
{
|
||||||
int eidx = source.indexOf("=", prefix.length());
|
int eidx = source.indexOf("=", prefix.length());
|
||||||
if (eidx == -1) {
|
if (eidx == -1) {
|
||||||
throw new CDDBException(500, "Malformed line '" + source +
|
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
|
||||||
"' number " + lineno);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return Integer.parseInt(source.substring(prefix.length(), eidx));
|
return Integer.parseInt(source.substring(prefix.length(), eidx));
|
||||||
} catch (NumberFormatException nfe) {
|
} catch (NumberFormatException nfe) {
|
||||||
throw new CDDBException(500, "Malformed line '" + source +
|
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
|
||||||
"' number " + lineno);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts the contents of the supplied line (everything after the
|
* Extracts the contents of the supplied line (everything after the equals sign) or throws a
|
||||||
* equals sign) or throws a CDDBException if the line contains no
|
* CDDBException if the line contains no equals sign.
|
||||||
* equals sign.
|
|
||||||
*/
|
*/
|
||||||
protected final String contents (String source, int lineno)
|
protected final String contents (String source, int lineno)
|
||||||
throws CDDBException
|
throws CDDBException
|
||||||
{
|
{
|
||||||
int eidx = source.indexOf("=");
|
int eidx = source.indexOf("=");
|
||||||
if (eidx == -1) {
|
if (eidx == -1) {
|
||||||
throw new CDDBException(500, "Malformed line '" + source +
|
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
|
||||||
"' number " + lineno);
|
|
||||||
}
|
}
|
||||||
return source.substring(eidx+1);
|
return source.substring(eidx+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Appends the supplied string to the contents of the list at the
|
* Appends the supplied string to the contents of the list at the supplied index. If the list
|
||||||
* supplied index. If the list has no contents at the supplied
|
* has no contents at the supplied index, the supplied value becomes the contents at that
|
||||||
* index, the supplied value becomes the contents at that index.
|
* index.
|
||||||
*/
|
*/
|
||||||
protected final void append (
|
protected final void append (
|
||||||
ArrayList<String> list, int index, String value)
|
ArrayList<String> list, int index, String value)
|
||||||
@@ -449,15 +416,6 @@ public class CDDB
|
|||||||
list.set(index, list.get(index) + value);
|
list.set(index, list.get(index) + value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple class to encapsulate the response from the CDDB server.
|
|
||||||
*/
|
|
||||||
protected class Response
|
|
||||||
{
|
|
||||||
public int code;
|
|
||||||
public String message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Issues a request to the CDDB server and parses the response.
|
* Issues a request to the CDDB server and parses the response.
|
||||||
*/
|
*/
|
||||||
@@ -500,15 +458,12 @@ public class CDDB
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The client version number is extracted from the version control
|
* A simple class to encapsulate the response from the CDDB server.
|
||||||
* revision of this file from a string that is managed by the version
|
|
||||||
* control system.
|
|
||||||
*/
|
*/
|
||||||
static
|
protected class Response
|
||||||
{
|
{
|
||||||
StringTokenizer tok = new StringTokenizer("$Revision: 1.8 $");
|
public int code;
|
||||||
tok.nextToken();
|
public String message;
|
||||||
CLIENT_VERSION = tok.nextToken();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Socket _sock;
|
protected Socket _sock;
|
||||||
|
|||||||
Reference in New Issue
Block a user