net.kano.joscar
Class OscarTools

java.lang.Object
  extended bynet.kano.joscar.OscarTools

public final class OscarTools
extends java.lang.Object

Provides a set of tools for performing OSCAR-specific functions.


Nested Class Summary
static class OscarTools.HttpHeaderInfo
          A structure containing HTTP header information along with the binary data sent in the HTTP stream.
 
Method Summary
static java.lang.String getInfoString(ByteBlock infoData, java.lang.String infoType)
          Returns a string given its binary representation and one of AIM's text/x-aolrtf; charset=us-ascii content-type strings.
static java.lang.String getRoomNameFromCookie(java.lang.String cookie)
          Returns the name of the chat room described by the given "cookie" or "chat room URL."
static java.lang.String getString(ByteBlock data, java.lang.String charset)
          Creates a String from the given block of data and the given charset.
static java.lang.String getValidCharset(java.lang.String charset)
          Returns the given charset if it is supported by the JVM; if it is not supported, attempts to fix it and returns the "fixed" version.
static java.lang.String normalize(java.lang.String str)
          Returns a "normalized" version of the given string by removing all spaces and converting to lowercase.
static java.util.Map parseContentTypeString(java.lang.String str)
          Converts a string like text/x-aolrtf; charset=us-ascii to a Map with two keys: text/x-aolrtf (value null) and charset (value us-ascii).
static OscarTools.HttpHeaderInfo parseHttpHeader(ByteBlock data)
          Extracts HTTP header information and the block of data being sent in the HTTP stream from a block of binary data.
static StringBlock readScreenname(ByteBlock data)
          Reads a screenname which is preceded by a single byte describing the screenname's length from the given block of data.
static java.lang.String stripHtml(java.lang.String str)
          Uses a poorly conceived method to remove HTML from a string.
static void writeScreenname(java.io.OutputStream out, java.lang.String sn)
          Writes the given screenname to the given stream, preceded by a single byte containing the screenname's length in bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readScreenname

public static StringBlock readScreenname(ByteBlock data)
Reads a screenname which is preceded by a single byte describing the screenname's length from the given block of data. Returns null if no such valid object can be read.

Parameters:
data - the block containing a single-byte length followed by a screen name in US-ASCII format
Returns:
an object containing the screenname and the total number of bytes read

writeScreenname

public static void writeScreenname(java.io.OutputStream out,
                                   java.lang.String sn)
                            throws java.io.IOException
Writes the given screenname to the given stream, preceded by a single byte containing the screenname's length in bytes.

Parameters:
out - the stream to write to
sn - the screenname to write to the given stream
Throws:
java.io.IOException - if an I/O error occurs

parseContentTypeString

public static java.util.Map parseContentTypeString(java.lang.String str)
Converts a string like text/x-aolrtf; charset=us-ascii to a Map with two keys: text/x-aolrtf (value null) and charset (value us-ascii).

Parameters:
str - the content type string
Returns:
a map with keys and values extracted from the given string

getInfoString

public static java.lang.String getInfoString(ByteBlock infoData,
                                             java.lang.String infoType)
Returns a string given its binary representation and one of AIM's text/x-aolrtf; charset=us-ascii content-type strings.

Parameters:
infoData - the binary representation of the string
infoType - an AIM content-type string
Returns:
a string decoded from the given byte block and the charset that might be specified in the given content-type string

getValidCharset

public static java.lang.String getValidCharset(java.lang.String charset)
Returns the given charset if it is supported by the JVM; if it is not supported, attempts to fix it and returns the "fixed" version. This method will always return the name of a charset that can be used within this JVM. Note that if charset is null, "US-ASCII" will be returned.

Parameters:
charset - the charset name to "fix"
Returns:
either the given charset name or a valid charset name derived from the given name

getString

public static java.lang.String getString(ByteBlock data,
                                         java.lang.String charset)
Creates a String from the given block of data and the given charset. Note that this will never return null, even if the given charset is null. This method will do its best to produce a String from the given data using getValidCharset.

Parameters:
data - a block of data containing a string
charset - the name of a charset to use to extract a string from the given data, or null for US-ASCII
Returns:
a String decoded from the given block of data

normalize

public static java.lang.String normalize(java.lang.String str)
Returns a "normalized" version of the given string by removing all spaces and converting to lowercase. Several aspects of the AIM protocol are "normalized": an IM to "joustacular" is the same as an IM to "Joust Acular". Similarly, joining the chat room "JoUsTeRrIfIc" is equivalent to joining "Jousterrific".

Parameters:
str - the string to normalize
Returns:
a normalized version of the given string

stripHtml

public static java.lang.String stripHtml(java.lang.String str)
Uses a poorly conceived method to remove HTML from a string. "Not for production use."

Parameters:
str - the string from which to strip HTML tags
Returns:
the given string with HTML tags removed

parseHttpHeader

public static OscarTools.HttpHeaderInfo parseHttpHeader(ByteBlock data)
Extracts HTTP header information and the block of data being sent in the HTTP stream from a block of binary data. The header is presumed to be in US-ASCII encoding (that is, all characters are a single byte), but the data after the header can be in any format; it is not parsed, only returned. One should note that all three popular newline formats are supported in parsing the header: \r, \n, and \r\n.

An example:
Input:
Content-Type: text/x-aolrtf
Content-Encoding: binary

<HTML>I am a banana</HTML>
Sample code:
ByteBlock input = ...;
OscarTools.HttpHeaderInfo hinfo
        = OscarTools.parseHttpHeader(input);
System.out.println("Content type of input is "
        + hinfo.get("content-type"));
     System.out.println("Content encoding of input is "
     + hinfo.get("Content-Encoding"));
System.out.println("message text is "
        + ByteBlock.createString(hinfo.getData(),
        "US-ASCII");
     
Something important to note is that the header names in the returned structure are also present in lowercase form. (This is so mainly for convenience.)

Parameters:
data - the block of data containing an HTTP header followed by data
Returns:
a structure containing HTTP header information and the data following the headers for the given input

getRoomNameFromCookie

public static final java.lang.String getRoomNameFromCookie(java.lang.String cookie)
Returns the name of the chat room described by the given "cookie" or "chat room URL." See MiniRoomInfo.getCookie() for details. For example, with the input "!aol://2719:11-4-room%20name", this method will return "room name".

Parameters:
cookie - a chat room "cookie"
Returns:
the name of the chat room described by the cookie