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:
samskivert
2010-02-09 21:22:40 +00:00
parent 053d4c6196
commit ddd0b0b72a
+59 -104
View File
@@ -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
* <a href="http://www.freedb.org/software/old/CDDBPROTO">CDDB
* protocol</a>.
* The CDDB class provides access to the information provided by servers compliant with the
* <a href="http://www.freedb.org/software/old/CDDBPROTO">CDDB protocol</a>.
*/
public class CDDB
{
@@ -48,12 +46,11 @@ 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
{
@@ -67,12 +64,10 @@ public class CDDB
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.
* 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.
* @exception CDDBException Thrown if the entry is not properly formatted.
*/
public void parse (String source)
throws CDDBException
@@ -81,8 +76,7 @@ public class CDDB
int sidx2 = source.indexOf(" ", sidx1+1);
if (sidx1 == -1 || sidx2 == -1) {
throw new CDDBException(499, "Malformed entry '" +
source + "'");
throw new CDDBException(499, "Malformed entry '" + source + "'");
}
category = source.substring(0, sidx1);
@@ -92,22 +86,18 @@ public class CDDB
}
/**
* 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.
*
* <p> <b>Note well:</b> you must close a CDDB connection once you are
* done with it, otherwise the socket connection will remain open and
* pointlessly consume machine resources.
* <p> <b>Note well:</b> 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
@@ -119,23 +109,19 @@ public class CDDB
}
/**
* 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.
*
* <p> <b>Note well:</b> you must close a CDDB connection once you are
* done with it, otherwise the socket connection will remain open and
* pointlessly consume machine resources.
* <p> <b>Note well:</b> 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
*/
@@ -153,10 +139,8 @@ public class CDDB
// 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()));
_in = new BufferedReader(new InputStreamReader(_sock.getInputStream()));
_out = new PrintStream(new BufferedOutputStream(_sock.getOutputStream()));
// first read (and discard) the banner string
_in.readLine();
@@ -180,10 +164,9 @@ public class CDDB
}
/**
* Specifies the number of milliseconds that the client should wait
* for a response from the server before aborting. This must be called
* <em>after</em> 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 <em>after</em> a successful call to connect,
* otherwise the timeout will not be set.
*/
public void setTimeout (int timeout)
throws SocketException
@@ -202,11 +185,8 @@ public class CDDB
}
/**
* Closes the connection to the CDDB server previously opened with a
* call to <code>connect()</code>. 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
@@ -222,20 +202,16 @@ public class CDDB
}
/**
* 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
* <a href="http://www.freedb.org/sections.php?op=viewarticle&artid=6">
* here</a>.
* @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 <a
* href="http://www.freedb.org/sections.php?op=viewarticle&artid=6"> here</a>.
* @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
@@ -287,8 +263,8 @@ public class CDDB
}
/**
* A detail object contains all of the detailed information about a
* particular CD as retrieved from the CDDB server.
* A detail object contains all of the detailed information about a particular CD as retrieved
* from the CDDB server.
*/
public static class Detail
{
@@ -312,11 +288,10 @@ public class CDDB
}
/**
* Requests the detail information for a particular disc in a
* particular category from the CDDB server.
* Requests the detail information for a particular disc in a particular category from the CDDB
* server.
*
* @return A detail instance filled with the information for the
* requested CD.
* @return A detail instance filled with the information for the requested CD.
*/
public Detail read (String category, String discid)
throws IOException, CDDBException
@@ -355,7 +330,6 @@ public class CDDB
// now parse the contents
String input = _in.readLine();
for (int lno = 0; !input.equals(CDDBProtocol.TERMINATOR); lno++) {
if (input.startsWith("#")) {
// skip comments
@@ -374,12 +348,10 @@ public class CDDB
}
} else if (input.startsWith("TTITLE")) {
append(tnames, index(input, "TTITLE", lno),
contents(input, lno));
append(tnames, index(input, "TTITLE", lno), contents(input, lno));
} else if (input.startsWith("EXTT")) {
append(texts, index(input, "EXTT", lno),
contents(input, lno));
append(texts, index(input, "EXTT", lno), contents(input, lno));
}
// read in the next line of input
@@ -396,48 +368,43 @@ public class CDDB
}
/**
* Extracts the track index of the supplied line (the number
* immediately following the supplied prefix and preceding the equals
* sign) or throws a CDDBException if the line contains no equals sign
* or track index.
* Extracts the track index of the supplied line (the number immediately following the supplied
* prefix and preceding the equals sign) or throws a CDDBException if the line contains no
* equals sign or track index.
*/
protected final int index (String source, String prefix, int lineno)
throws CDDBException
{
int eidx = source.indexOf("=", prefix.length());
if (eidx == -1) {
throw new CDDBException(500, "Malformed line '" + source +
"' number " + lineno);
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
}
try {
return Integer.parseInt(source.substring(prefix.length(), eidx));
} catch (NumberFormatException nfe) {
throw new CDDBException(500, "Malformed line '" + source +
"' number " + lineno);
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
}
}
/**
* Extracts the contents of the supplied line (everything after the
* equals sign) or throws a CDDBException if the line contains no
* equals sign.
* Extracts the contents of the supplied line (everything after the equals sign) or throws a
* CDDBException if the line contains no equals sign.
*/
protected final String contents (String source, int lineno)
throws CDDBException
{
int eidx = source.indexOf("=");
if (eidx == -1) {
throw new CDDBException(500, "Malformed line '" + source +
"' number " + lineno);
throw new CDDBException(500, "Malformed line '" + source + "' number " + lineno);
}
return source.substring(eidx+1);
}
/**
* Appends the supplied string to the contents of the list at the
* supplied index. If the list has no contents at the supplied
* index, the supplied value becomes the contents at that index.
* Appends the supplied string to the contents of the list at the supplied index. If the list
* has no contents at the supplied index, the supplied value becomes the contents at that
* index.
*/
protected final void append (
ArrayList<String> list, int index, String value)
@@ -449,15 +416,6 @@ public class CDDB
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.
*/
@@ -500,15 +458,12 @@ public class CDDB
}
/**
* The client version number is extracted from the version control
* revision of this file from a string that is managed by the version
* control system.
* A simple class to encapsulate the response from the CDDB server.
*/
static
protected class Response
{
StringTokenizer tok = new StringTokenizer("$Revision: 1.8 $");
tok.nextToken();
CLIENT_VERSION = tok.nextToken();
public int code;
public String message;
}
protected Socket _sock;