diff --git a/src/java/com/samskivert/net/cddb/CDDB.java b/src/java/com/samskivert/net/cddb/CDDB.java index a3002355..6bc7fa4b 100644 --- a/src/java/com/samskivert/net/cddb/CDDB.java +++ b/src/java/com/samskivert/net/cddb/CDDB.java @@ -28,10 +28,8 @@ import java.util.ArrayList; import java.util.StringTokenizer; /** - * The CDDB class provides access to the information provided by servers - * compliant with the - * CDDB - * protocol. + * The CDDB class provides access to the information provided by servers compliant with the + * CDDB protocol. */ public class CDDB { @@ -48,149 +46,134 @@ public class CDDB /** * 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 - * CDDB record for a particular disc. An array of them are returned in - * response to a CDDB query. + * This class encapsulates the information needed to look up a full CDDB record for a + * particular disc. An array of them are returned in response to a CDDB query. */ public static class Entry { - /** The category to which this entry belongs. */ - public String category; + /** The category to which this entry belongs. */ + public String category; - /** The unique identifier for this entry. */ - public String cdid; + /** The unique identifier for this entry. */ + public String cdid; - /** The human readable title of this entry. */ - public String title; + /** The human readable title of this entry. */ + public String title; - /** - * Parses values for this entry from the supplied source - * string. The source string should contain an entry description - * as formatted by the CDDB server. - * - * @exception CDDBException Thrown if the entry is not properly - * formatted. - */ - public void parse (String source) - throws CDDBException - { - int sidx1 = source.indexOf(" "); - int sidx2 = source.indexOf(" ", sidx1+1); + /** + * Parses values for this entry from the supplied source string. The source string should + * contain an entry description as formatted by the CDDB server. + * + * @exception CDDBException Thrown if the entry is not properly formatted. + */ + public void parse (String source) + throws CDDBException + { + int sidx1 = source.indexOf(" "); + int sidx2 = source.indexOf(" ", sidx1+1); - if (sidx1 == -1 || sidx2 == -1) { - throw new CDDBException(499, "Malformed entry '" + - source + "'"); - } + if (sidx1 == -1 || sidx2 == -1) { + throw new CDDBException(499, "Malformed entry '" + source + "'"); + } - category = source.substring(0, sidx1); - cdid = source.substring(sidx1+1, sidx2); - title = source.substring(sidx2+1); - } + category = source.substring(0, sidx1); + cdid = source.substring(sidx1+1, sidx2); + title = source.substring(sidx2+1); + } } /** - * Connects this CDDB instance to the CDDB server running on the - * supplied host using the standard port. + * Connects this CDDB instance to the CDDB server running on the supplied host using the + * standard port. * - *
Note well: you must close a CDDB connection once you are - * done with it, otherwise the socket connection will remain open and - * pointlessly consume machine resources. + *
Note well: you must close a CDDB connection once you are done with it, otherwise + * the socket connection will remain open and pointlessly consume machine resources. * * @param hostname The host to which to connect. * - * @exception IOException Thrown if a network error occurs attempting - * to connect to the host. - * @exception CDDBException Thrown if an error occurs after - * identifying ourselves to the host. + * @exception IOException Thrown if a network error occurs attempting to connect to the host. + * @exception CDDBException Thrown if an error occurs after identifying ourselves to the host. * - * @return The message supplied with the succesful connection - * response. + * @return The message supplied with the succesful connection response. * * @see #STANDARD_PORT * @see #close */ public String connect (String hostname) - throws IOException, CDDBException + throws IOException, CDDBException { - return connect(hostname, STANDARD_PORT); + return connect(hostname, STANDARD_PORT); } /** - * Connects this CDDB instance to the CDDB server running on the - * supplied host using the specified port. + * Connects this CDDB instance to the CDDB server running on the supplied host using the + * specified port. * - *
Note well: you must close a CDDB connection once you are - * done with it, otherwise the socket connection will remain open and - * pointlessly consume machine resources. + *
Note well: you must close a CDDB connection once you are done with it, otherwise
+ * the socket connection will remain open and pointlessly consume machine resources.
*
* @param hostname The host to which to connect.
* @param port The port number on which to connect to the host.
*
- * @exception IOException Thrown if a network error occurs attempting
- * to connect to the host.
- * @exception CDDBException Thrown if an error occurs after
- * identifying ourselves to the host.
+ * @exception IOException Thrown if a network error occurs attempting to connect to the host.
+ * @exception CDDBException Thrown if an error occurs after identifying ourselves to the host.
*
- * @return The message supplied with the succesful connection
- * response.
+ * @return The message supplied with the succesful connection response.
*
* @see #close
*/
public String connect (String hostname, int port)
- throws IOException, CDDBException
+ throws IOException, CDDBException
{
- // obtain the necessary information we'll need to identify
- // ourselves to the CDDB server
- String localhost = InetAddress.getLocalHost().getHostName();
- String username = System.getProperty("user.name");
- if (username == null) {
- username = "anonymous";
- }
+ // obtain the necessary information we'll need to identify
+ // ourselves to the CDDB server
+ String localhost = InetAddress.getLocalHost().getHostName();
+ String username = System.getProperty("user.name");
+ if (username == null) {
+ username = "anonymous";
+ }
- // establish our socket connection and IO streams
- InetAddress addr = InetAddress.getByName(hostname);
- _sock = new Socket(addr, port);
- _in = new BufferedReader(
- new InputStreamReader(_sock.getInputStream()));
- _out = new PrintStream(
- new BufferedOutputStream(_sock.getOutputStream()));
+ // establish our socket connection and IO streams
+ InetAddress addr = InetAddress.getByName(hostname);
+ _sock = new Socket(addr, port);
+ _in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
+ _out = new PrintStream(new BufferedOutputStream(_sock.getOutputStream()));
- // first read (and discard) the banner string
- _in.readLine();
+ // first read (and discard) the banner string
+ _in.readLine();
- // send a hello request
- StringBuilder req = new StringBuilder("cddb hello ");
- req.append(username).append(" ");
- req.append(localhost).append(" ");
- req.append(CLIENT_NAME).append(" ");
- req.append(CLIENT_VERSION);
+ // send a hello request
+ StringBuilder req = new StringBuilder("cddb hello ");
+ req.append(username).append(" ");
+ req.append(localhost).append(" ");
+ req.append(CLIENT_NAME).append(" ");
+ req.append(CLIENT_VERSION);
- Response rsp = request(req.toString());
+ Response rsp = request(req.toString());
- // confirm that the response was a successful one
- if (CDDBProtocol.codeFamily(rsp.code) != CDDBProtocol.OK &&
- rsp.code != 402 /* already shook hands */) {
- throw new CDDBException(rsp.code, rsp.message);
- }
+ // confirm that the response was a successful one
+ if (CDDBProtocol.codeFamily(rsp.code) != CDDBProtocol.OK &&
+ rsp.code != 402 /* already shook hands */) {
+ throw new CDDBException(rsp.code, rsp.message);
+ }
- return rsp.message;
+ return rsp.message;
}
/**
- * Specifies the number of milliseconds that the client should wait
- * for a response from the server before aborting. This must be called
- * after a successful call to connect, otherwise the timeout
- * will not be set.
+ * Specifies the number of milliseconds that the client should wait for a response from the
+ * server before aborting. This must be called after a successful call to connect,
+ * otherwise the timeout will not be set.
*/
public void setTimeout (int timeout)
- throws SocketException
+ throws SocketException
{
- if (_sock != null) {
- _sock.setSoTimeout(timeout);
- }
+ if (_sock != null) {
+ _sock.setSoTimeout(timeout);
+ }
}
/**
@@ -198,255 +181,280 @@ public class CDDB
*/
public boolean connected ()
{
- return _sock != null;
+ return _sock != null;
}
/**
- * Closes the connection to the CDDB server previously opened with a
- * call to connect(). If the connection was not
- * previously established, this member function does nothing.
- *
- * @see #connect
+ * Closes the connection to the CDDB server previously opened with a call to {@link #connect}.
+ * If the connection was not previously established, this member function does nothing.
*/
public void close ()
- throws IOException
+ throws IOException
{
- if (_sock != null) {
- _sock.close();
+ if (_sock != null) {
+ _sock.close();
- // clear out our references
- _sock = null;
- _in = null;
- _out = null;
- }
+ // clear out our references
+ _sock = null;
+ _in = null;
+ _out = null;
+ }
}
/**
- * Issues a query to the CDDB server using the supplied CD identifying
- * information.
+ * Issues a query to the CDDB server using the supplied CD identifying information.
*
- * @param discid The disc identifier (information on how to compute
- * the disc ID is available
- *
- * here.
- * @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 discid The disc identifier (information on how to compute the disc ID is available here.
+ * @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.
*
- * @return If no entry matches the query, null is returned. Otherwise
- * one or more entries is returned that matched the query parameters.
+ * @return If no entry matches the query, null is returned. Otherwise one or more entries is
+ * returned that matched the query parameters.
*/
public Entry[] query (String discid, int[] frameOffsets, int length)
- throws IOException, CDDBException
+ throws IOException, CDDBException
{
- // sanity check
- if (_sock == null) {
- throw new CDDBException(500, "Not connected");
- }
+ // sanity check
+ if (_sock == null) {
+ throw new CDDBException(500, "Not connected");
+ }
- // construct the query parameter
- StringBuilder req = new StringBuilder("cddb query ");
- req.append(discid).append(" ");
- req.append(frameOffsets.length).append(" ");
- for (int i = 0; i < frameOffsets.length; i++) {
- req.append(frameOffsets[i]).append(" ");
- }
- req.append(length);
+ // construct the query parameter
+ StringBuilder req = new StringBuilder("cddb query ");
+ req.append(discid).append(" ");
+ req.append(frameOffsets.length).append(" ");
+ for (int i = 0; i < frameOffsets.length; i++) {
+ req.append(frameOffsets[i]).append(" ");
+ }
+ req.append(length);
- // make the request
- Response rsp = request(req.toString());
- Entry[] entries = null;
+ // make the request
+ Response rsp = request(req.toString());
+ Entry[] entries = null;
- // if this is an exact match, parse the entry and return it
- if (rsp.code == 200 /* exact match */) {
- entries = new Entry[1];
- entries[0] = new Entry();
- entries[0].parse(rsp.message);
+ // if this is an exact match, parse the entry and return it
+ if (rsp.code == 200 /* exact match */) {
+ entries = new Entry[1];
+ entries[0] = new Entry();
+ entries[0].parse(rsp.message);
- } else if (rsp.code == 211 /* inexact matches */) {
- // read the matches from the server
- ArrayList