All Packages  Class Hierarchy  This Package  Previous  Next  Index  

Class HTTPClient.Codecs

java.lang.Object
    |
    +----HTTPClient.Codecs

public class Codecs
extends Object
This class collects various encoders and decoders.

Version:
0.3 30/01/1998
Author:
Ronald Tschalär

Method Index

 o base64Decode(byte[])
This method decodes the given byte[] using the base64-encoding specified in RFC-2045 (Section 6.8).
 o base64Decode(String)
This method decodes the given string using the base64-encoding specified in RFC-2045 (Section 6.8).
 o base64Encode(byte[])
This method encodes the given byte[] using the base64-encoding specified in RFC-2045 (Section 6.8).
 o base64Encode(String)
This method encodes the given string using the base64-encoding specified in RFC-2045 (Section 6.8).
 o chunkedDecode(InputStream)
Decodes chunked data.
 o chunkedEncode(byte[], int, int, NVPair[], boolean)
Encodes data used the chunked encoding.
 o chunkedEncode(byte[], NVPair[], boolean)
Encodes data used the chunked encoding.
 o mpFormDataDecode(byte[], String, String)
This method decodes a multipart/form-data encoded string.
 o mpFormDataEncode(NVPair[], NVPair[], NVPair[])
This method encodes name/value pairs and files into a byte array using the multipart/form-data encoding.
 o nv2query(NVPair[])
Turns an array of name/value pairs into the string "name1=value1&name2=value2&name3=value3".
 o query2nv(String)
Turns a string of the form "name1=value1&name2=value2&name3=value3" into an array of name/value pairs.
 o quotedPrintableDecode(String)
This method does a quoted-printable decoding of the given string according to RFC-2045 (Section 6.7).
 o quotedPrintableEncode(String)
This method does a quoted-printable encoding of the given string according to RFC-2045 (Section 6.7).
 o URLDecode(String)
This method decodes the given urlencoded string.
 o URLEncode(String)
This method urlencodes the given string.
 o uudecode(char[])
This method decodes the given uuencoded char[].
 o uuencode(byte[])
This method encodes the given byte[] using the unix uuencode encding.

Methods

 o base64Encode
public static final String base64Encode(String str)
This method encodes the given string using the base64-encoding specified in RFC-2045 (Section 6.8). It's used for example in the "Basic" authorization scheme.

Parameters:
str - the string
Returns:
the base64-encoded str
 o base64Encode
public static final byte[] base64Encode(byte[] data)
This method encodes the given byte[] using the base64-encoding specified in RFC-2045 (Section 6.8).

Parameters:
data - the data
Returns:
the base64-encoded data
 o base64Decode
public static final String base64Decode(String str)
This method decodes the given string using the base64-encoding specified in RFC-2045 (Section 6.8).

Parameters:
str - the base64-encoded string.
Returns:
the decoded str.
 o base64Decode
public static final byte[] base64Decode(byte[] data)
This method decodes the given byte[] using the base64-encoding specified in RFC-2045 (Section 6.8).

Parameters:
data - the base64-encoded data.
Returns:
the decoded data.
 o uuencode
public static final char[] uuencode(byte[] data)
This method encodes the given byte[] using the unix uuencode encding. The output is split into lines starting with the encoded number of encoded octets in the line and ending with a newline. No line is longer than 45 octets (60 characters), not including length and newline.

Note: just the raw data is encoded; no 'begin' and 'end' lines are added as is done by the unix uuencode utility.

Parameters:
data - the data
Returns:
the uuencoded data
 o uudecode
public static final byte[] uudecode(char[] data)
This method decodes the given uuencoded char[].

Note: just the actual data is decoded; any 'begin' and 'end' lines as generated by unix uuencode utility must not be included.

Parameters:
data - the uuencode-encoded data.
Returns:
the decoded data.
 o quotedPrintableEncode
public static final String quotedPrintableEncode(String str)
This method does a quoted-printable encoding of the given string according to RFC-2045 (Section 6.7). Note: this assumes 8-bit characters.

Parameters:
str - the string
Returns:
the quoted-printable encoded string
 o quotedPrintableDecode
public static final String quotedPrintableDecode(String str) throws ParseException
This method does a quoted-printable decoding of the given string according to RFC-2045 (Section 6.7). Note: this method expects the whole message in one chunk, not line by line.

Parameters:
str - the message
Returns:
the decoded message
Throws: ParseException
If a '=' is not followed by a valid 2-digit hex number or '\r\n'.
 o URLEncode
public static final String URLEncode(String str)
This method urlencodes the given string. This method is here for symmetry reasons and just calls java.net.URLEncoder.encode().

Parameters:
str - the string
Returns:
the url-encoded string
 o URLDecode
public static final String URLDecode(String str) throws ParseException
This method decodes the given urlencoded string.

Parameters:
str - the url-encoded string
Returns:
the decoded string
Throws: ParseException
If a '%' is not followed by a valid 2-digit hex number.
 o mpFormDataDecode
public static final NVPair[] mpFormDataDecode(byte[] data,
                                              String cont_type,
                                              String dir) throws IOException, ParseException
This method decodes a multipart/form-data encoded string. The boundary is parsed from the cont_type parameter, which must be of the form 'multipart/form-data; boundary=...'. Any encoded files are created in the directory specified by dir using the encoded filename.

Note: Does not handle nested encodings (yet).

Examples: If you're receiving a multipart/form-data encoded response from a server you could use something like:

    NVPair[] opts = Codecs.mpFormDataDecode(resp.getData(),
					    resp.getHeader("Content-type"), ".");
If you're using this in a Servlet to decode the body of a request from a client you could use something like:
    byte[] body = new byte[req.getContentLength()];
    new DataInputStream(req.getInputStream()).readFully(body);
    NVPair[] opts = Codecs.mpFormDataDecode(body, req.getContentType(), ".");
Assuming the data received looked something like:
-----------------------------114975832116442893661388290519
Content-Disposition: form-data; name="option"
 
doit
-----------------------------114975832116442893661388290519
Content-Disposition: form-data; name="comment"; filename="comment.txt"
 
Gnus and Gnats are not Gnomes.
-----------------------------114975832116442893661388290519--
you would get one file called comment.txt in the current directory, and opts would contain two elements: {"option", "doit"} and {"comment", "comment.txt"}

Parameters:
data - the form-data to decode.
cont_type - the content type header (must contain the boundary string).
dir - the directory to create the files in.
Returns:
an array of name/value pairs, one for each part; the name is the 'name' attribute given in the Content-Disposition header; the value is either the name of the file if a filename attribute was found, or the contents of the part.
Throws: IOException
If any file operation fails.
Throws: ParseException
If an error during parsing occurs.
 o mpFormDataEncode
public static final byte[] mpFormDataEncode(NVPair[] opts,
                                            NVPair[] files,
                                            NVPair[] cont_type) throws IOException
This method encodes name/value pairs and files into a byte array using the multipart/form-data encoding. The boundary is returned as part of cont_type.
Example:
    NVPair[] opts = { new NVPair("option", "doit") };
    NVPair[] file = { new NVPair("comment", "comment.txt") };
    NVPair[] hdrs = new NVPair[1];
    byte[]   data = Codecs.mpFormDataEncode(opts, file, hdrs);
    con.Post("/cgi-bin/handle-it", data, hdrs);
data will look something like the following:
-----------------------------114975832116442893661388290519
Content-Disposition: form-data; name="option"
 
doit
-----------------------------114975832116442893661388290519
Content-Disposition: form-data; name="comment"; filename="comment.txt"
 
Gnus and Gnats are not Gnomes.
-----------------------------114975832116442893661388290519--
where the "Gnus and Gnats ..." is the contents of the file comment.txt in the current directory.

Parameters:
opts - the simple form-data to encode (may be null); for each NVPair the name refers to the 'name' attribute to be used in the header of the part, and the value is contents of the part.
files - the files to encode (may be null); for each NVPair the name refers to the 'name' attribute to be used in the header of the part, and the value is the actual filename (the file will be read and it's contents put in the body of that part).
cont_type - this returns a new NVPair in the 0'th element which contains name = "Content-Type", value = "multipart/form-data; boundary=..." (the reason this parameter is an array is because a) that's the only way to simulate pass-by-reference and b) you need an array for the headers parameter to the Post() or Put() anyway).
Returns:
an encoded byte array containing all the opts and files.
Throws: IOException
If any file operation fails.
 o nv2query
public static final String nv2query(NVPair[] pairs)
Turns an array of name/value pairs into the string "name1=value1&name2=value2&name3=value3". The names and values are first urlencoded. This is the form in which form-data is passed to a cgi script.

Parameters:
pairs - the array of name/value pairs
Returns:
a string containg the encoded name/value pairs
 o query2nv
public static final NVPair[] query2nv(String query) throws ParseException
Turns a string of the form "name1=value1&name2=value2&name3=value3" into an array of name/value pairs. The names and values are urldecoded. The query string is in the form in which form-data is received in a cgi script.

Parameters:
query - the query string containing the encoded name/value pairs
Returns:
an array of NVPairs
Throws: ParseException
If the '=' is missing in any field, or if the urldecoding of the name or value fails
 o chunkedEncode
public static final byte[] chunkedEncode(byte[] data,
                                         NVPair[] ftrs,
                                         boolean last)
Encodes data used the chunked encoding. last signales if this is the last chunk, in which case the appropriate footer is generated.

Parameters:
data - the data to be encoded; may be null.
ftrs - optional headers to include in the footer (ignored if not last); may be null.
last - whether this is the last chunk.
Returns:
an array of bytes containing the chunk
 o chunkedEncode
public static final byte[] chunkedEncode(byte[] data,
                                         int off,
                                         int len,
                                         NVPair[] ftrs,
                                         boolean last)
Encodes data used the chunked encoding. last signales if this is the last chunk, in which case the appropriate footer is generated.

Parameters:
data - the data to be encoded; may be null.
off - an offset into the data
len - the number of bytes to take from data
ftrs - optional headers to include in the footer (ignored if not last); may be null.
last - whether this is the last chunk.
Returns:
an array of bytes containing the chunk
 o chunkedDecode
public static final Object chunkedDecode(InputStream input) throws ParseException, IOException
Decodes chunked data. The chunks are read from an InputStream, which is assumed to be correctly positioned. Use 'xxx instanceof byte[]' and 'xxx instanceof NVPair[]' to determine if this was data or the last chunk.

Parameters:
input - the stream from which to read the next chunk.
Returns:
If this was a data chunk then it returns a byte[]; else it's the footer and it returns a NVPair[] containing the footers.
Throws: ParseException
If any exception during parsing occured.
Throws: IOException
If any exception during reading occured.

All Packages  Class Hierarchy  This Package  Previous  Next  Index