| java.lang.Object HTTPClient.Codecs
Codecs | public class Codecs (Code) | | This class collects various encoders and decoders.
version: 0.3-2 18/06/1999 author: Ronald Tschalär |
Method Summary | |
final public static String | URLDecode(String str) This method decodes the given urlencoded string. | final public static String | URLEncode(String str) This method urlencodes the given string. | final public static 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. | final public static 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. | final public static String | base64Encode(String str) This method encodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8). | final public static byte[] | base64Encode(byte[] data) This method encodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8). | final public static Object | chunkedDecode(InputStream input) Decodes chunked data. | final public static byte[] | chunkedEncode(byte[] data, NVPair[] ftrs, boolean last) Encodes data used the chunked encoding. | final public static byte[] | chunkedEncode(byte[] data, int off, int len, NVPair[] ftrs, boolean last) Encodes data used the chunked encoding. | final static int | getChunkLength(InputStream input) Gets the length of the chunk.
Parameters: input - the stream from which to read the next chunk. | final public static NVPair[] | mpFormDataDecode(byte[] data, String cont_type, String dir) This method decodes a multipart/form-data encoded string.
Parameters: data - the form-data to decode. Parameters: cont_type - the content type header (must contain theboundary string). Parameters: dir - the directory to create the files in. | final public static NVPair[] | mpFormDataDecode(byte[] data, String cont_type, String dir, FilenameMangler mangler) This method decodes a multipart/form-data encoded string. | final public static byte[] | mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] cont_type) This method encodes name/value pairs and files into a byte array
using the multipart/form-data encoding.
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. Parameters: files - the files to encode (may be null); for eachNVPair the name refers to the 'name' attributeto be used in the header of the part, and thevalue is the actual filename (the file will beread and it's contents put in the body of thatpart). Parameters: cont_type - this returns a new NVPair in the 0'th elementwhich containsname = "Content-Type",value = "multipart/form-data; boundary=..."(the reason this parameter is an array isbecause a) that's the only way to simulatepass-by-reference and b) you need an array forthe headers parameter to the Post() or Put()anyway). | final public static byte[] | mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] cont_type, FilenameMangler mangler) This method encodes name/value pairs and files into a byte array
using the multipart/form-data encoding. | final public static String | nv2query(NVPair pairs) Turns an array of name/value pairs into the string
"name1=value1&name2=value2&name3=value3". | final public static NVPair[] | query2nv(String query) Turns a string of the form "name1=value1&name2=value2&name3=value3"
into an array of name/value pairs. | final public static String | quotedPrintableDecode(String str) This method does a quoted-printable decoding of the given string
according to RFC-2045 (Section 6.7). | final public static String | quotedPrintableEncode(String str) This method does a quoted-printable encoding of the given string
according to RFC-2045 (Section 6.7). | final public static byte[] | uudecode(char[] data) This method decodes the given uuencoded char[].
Note: just the actual data is decoded; any 'begin' and
'end' lines such as those generated by the unix uuencode
utility must not be included.
Parameters: data - the uuencode-encoded data. | final public static char[] | uuencode(byte[] data) This method encodes the given byte[] using the unix uuencode
encding. |
URLDecode | final public static String URLDecode(String str) throws ParseException(Code) | | This method decodes the given urlencoded string.
Parameters: str - the url-encoded string the decoded string exception: ParseException - If a '%' is not followed by a valid2-digit hex number. |
URLEncode | final public static String URLEncode(String str)(Code) | | This method urlencodes the given string. This method is here for
symmetry reasons and just calls java.net.URLEncoder.encode().
Parameters: str - the string the url-encoded string |
base64Decode | final public static String base64Decode(String str)(Code) | | This method decodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
Parameters: str - the base64-encoded string. the decoded str. |
base64Decode | final public static byte[] base64Decode(byte[] data)(Code) | | This method decodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
Parameters: data - the base64-encoded data. the decoded data. |
base64Encode | final public static String base64Encode(String str)(Code) | | 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 the base64-encoded str |
base64Encode | final public static byte[] base64Encode(byte[] data)(Code) | | This method encodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
Parameters: data - the data the base64-encoded data |
chunkedDecode | final public static Object chunkedDecode(InputStream input) throws ParseException, IOException(Code) | | 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. If this was a data chunk then it returns a byte[]; elseit's the footer and it returns a NVPair[] containing thefooters. exception: ParseException - If any exception during parsing occured. exception: IOException - If any exception during reading occured. |
chunkedEncode | final public static byte[] chunkedEncode(byte[] data, NVPair[] ftrs, boolean last)(Code) | | 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. Parameters: ftrs - optional headers to include in the footer (ignored ifnot last); may be null. Parameters: last - whether this is the last chunk. an array of bytes containing the chunk |
chunkedEncode | final public static byte[] chunkedEncode(byte[] data, int off, int len, NVPair[] ftrs, boolean last)(Code) | | 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. Parameters: off - an offset into the data Parameters: len - the number of bytes to take from data Parameters: ftrs - optional headers to include in the footer (ignored ifnot last); may be null. Parameters: last - whether this is the last chunk. an array of bytes containing the chunk |
getChunkLength | final static int getChunkLength(InputStream input) throws ParseException, IOException(Code) | | Gets the length of the chunk.
Parameters: input - the stream from which to read the next chunk. the length of chunk to follow (w/o trailing CR LF). exception: ParseException - If any exception during parsing occured. exception: IOException - If any exception during reading occured. |
mpFormDataDecode | final public static NVPair[] mpFormDataDecode(byte[] data, String cont_type, String dir, FilenameMangler mangler) throws IOException, ParseException(Code) | | 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. Parameters: cont_type - the content type header (must contain theboundary string). Parameters: dir - the directory to create the files in. Parameters: mangler - the filename mangler, or null if no mangling isto be done. This is invoked just before eachfile is created and written, thereby allowingyou to control the names of the files. an array of name/value pairs, one for each part;the name is the 'name' attribute given in theContent-Disposition header; the value is eitherthe name of the file if a filename attribute wasfound, or the contents of the part. exception: IOException - If any file operation fails. exception: ParseException - If an error during parsing occurs. |
mpFormDataEncode | final public static byte[] mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] cont_type) throws IOException(Code) | | This method encodes name/value pairs and files into a byte array
using the multipart/form-data encoding.
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. Parameters: files - the files to encode (may be null); for eachNVPair the name refers to the 'name' attributeto be used in the header of the part, and thevalue is the actual filename (the file will beread and it's contents put in the body of thatpart). Parameters: cont_type - this returns a new NVPair in the 0'th elementwhich containsname = "Content-Type",value = "multipart/form-data; boundary=..."(the reason this parameter is an array isbecause a) that's the only way to simulatepass-by-reference and b) you need an array forthe headers parameter to the Post() or Put()anyway). an encoded byte array containing all the optsand files. exception: IOException - If any file operation fails. See Also: Codecs.mpFormDataEncode(HTTPClient.NVPair[],HTTPClient.NVPair[],HTTPClient.NVPair[],HTTPClient.FilenameMangler) |
mpFormDataEncode | final public static byte[] mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] cont_type, FilenameMangler mangler) throws IOException(Code) | | 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. Parameters: files - the files to encode (may be null); for eachNVPair the name refers to the 'name' attributeto be used in the header of the part, and thevalue is the actual filename (the file will beread and it's contents put in the body of thatpart). Parameters: cont_type - this returns a new NVPair in the 0'th elementwhich containsname = "Content-Type",value = "multipart/form-data; boundary=..."(the reason this parameter is an array isbecause a) that's the only way to simulatepass-by-reference and b) you need an array forthe headers parameter to the Post() or Put()anyway). Parameters: mangler - the filename mangler, or null if no mangling isto be done. This allows you to change the nameused in the filename attribute of theContent-Disposition header. Note: the manglerwill be invoked twice for each filename. an encoded byte array containing all the optsand files. exception: IOException - If any file operation fails. |
nv2query | final public static String nv2query(NVPair pairs)(Code) | | 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 a string containg the encoded name/value pairs |
query2nv | final public static NVPair[] query2nv(String query) throws ParseException(Code) | | 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 an array of NVPairs exception: ParseException - If the '=' is missing in any field, or ifthe urldecoding of the name or value fails |
quotedPrintableDecode | final public static String quotedPrintableDecode(String str) throws ParseException(Code) | | 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 the decoded message exception: ParseException - If a '=' is not followed by a valid2-digit hex number or '\r\n'. |
quotedPrintableEncode | final public static String quotedPrintableEncode(String str)(Code) | | 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 the quoted-printable encoded string |
uudecode | final public static byte[] uudecode(char[] data)(Code) | | This method decodes the given uuencoded char[].
Note: just the actual data is decoded; any 'begin' and
'end' lines such as those generated by the unix uuencode
utility must not be included.
Parameters: data - the uuencode-encoded data. the decoded data. |
uuencode | final public static char[] uuencode(byte[] data)(Code) | | 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 the uuencoded data |
|
|