public class Codecs
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static byte[] |
base64Decode(byte[] data)
This method decodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static java.lang.String |
base64Decode(java.lang.String str)
This method decodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static byte[] |
base64Encode(byte[] data)
This method encodes the given byte[] using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static java.lang.String |
base64Encode(java.lang.String str)
This method encodes the given string using the base64-encoding
specified in RFC-2045 (Section 6.8).
|
static java.lang.Object |
chunkedDecode(java.io.InputStream input)
Decodes chunked data.
|
static byte[] |
chunkedEncode(byte[] data,
int off,
int len,
NVPair[] ftrs,
boolean last)
Encodes data used the chunked encoding.
|
static byte[] |
chunkedEncode(byte[] data,
NVPair[] ftrs,
boolean last)
Encodes data used the chunked encoding.
|
static NVPair[] |
mpFormDataDecode(byte[] data,
java.lang.String cont_type,
java.lang.String dir)
This method decodes a multipart/form-data encoded string.
|
static NVPair[] |
mpFormDataDecode(byte[] data,
java.lang.String cont_type,
java.lang.String dir,
FilenameMangler mangler)
This method decodes a multipart/form-data encoded string.
|
static byte[] |
mpFormDataEncode(NVPair[] opts,
NVPair[] files,
NVPair[] ct_hdr)
This method encodes name/value pairs and files into a byte array
using the multipart/form-data encoding.
|
static byte[] |
mpFormDataEncode(NVPair[] opts,
NVPair[] files,
NVPair[] ct_hdr,
FilenameMangler mangler)
This method encodes name/value pairs and files into a byte array
using the multipart/form-data encoding.
|
static java.lang.String |
nv2query(NVPair[] pairs)
Turns an array of name/value pairs into the string
"name1=value1&name2=value2&name3=value3".
|
static NVPair[] |
query2nv(java.lang.String query)
Turns a string of the form "name1=value1&name2=value2&name3=value3"
into an array of name/value pairs.
|
static java.lang.String |
quotedPrintableDecode(java.lang.String str)
This method does a quoted-printable decoding of the given string
according to RFC-2045 (Section 6.7).
|
static java.lang.String |
quotedPrintableEncode(java.lang.String str)
This method does a quoted-printable encoding of the given string
according to RFC-2045 (Section 6.7).
|
static java.lang.String |
URLDecode(java.lang.String str)
This method decodes the given urlencoded string.
|
static java.lang.String |
URLEncode(java.lang.String str)
This method urlencodes the given string.
|
static byte[] |
uudecode(char[] data)
This method decodes the given uuencoded char[].
|
static char[] |
uuencode(byte[] data)
This method encodes the given byte[] using the unix uuencode
encding.
|
public static final java.lang.String base64Encode(java.lang.String str)
str
- the stringpublic static final byte[] base64Encode(byte[] data)
data
- the datapublic static final java.lang.String base64Decode(java.lang.String str)
str
- the base64-encoded string.public static final byte[] base64Decode(byte[] data)
data
- the base64-encoded data.public static final char[] uuencode(byte[] data)
Note: just the raw data is encoded; no 'begin' and 'end'
lines are added as is done by the unix uuencode
utility.
data
- the datapublic static final byte[] uudecode(char[] data)
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.
data
- the uuencode-encoded data.public static final java.lang.String quotedPrintableEncode(java.lang.String str)
str
- the stringpublic static final java.lang.String quotedPrintableDecode(java.lang.String str) throws ParseException
str
- the messageParseException
- If a '=' is not followed by a valid
2-digit hex number or '\r\n'.public static final java.lang.String URLEncode(java.lang.String str)
str
- the stringpublic static final java.lang.String URLDecode(java.lang.String str) throws ParseException
str
- the url-encoded stringParseException
- If a '%' is not followed by a valid
2-digit hex number.public static final NVPair[] mpFormDataDecode(byte[] data, java.lang.String cont_type, java.lang.String dir) throws java.io.IOException, ParseException
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.java.io.IOException
- If any file operation fails.ParseException
- If an error during parsing occurs.mpFormDataDecode(byte[], java.lang.String, java.lang.String, HTTPClient.FilenameMangler)
public static final NVPair[] mpFormDataDecode(byte[] data, java.lang.String cont_type, java.lang.String dir, FilenameMangler mangler) throws java.io.IOException, ParseException
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(), ".");(where 'req' is the HttpServletRequest).
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"}
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.mangler
- the filename mangler, or null if no mangling is
to be done. This is invoked just before each
file is created and written, thereby allowing
you to control the names of the files.java.io.IOException
- If any file operation fails.ParseException
- If an error during parsing occurs.public static final byte[] mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] ct_hdr) throws java.io.IOException
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).ct_hdr
- 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).java.io.IOException
- If any file operation fails.mpFormDataEncode(HTTPClient.NVPair[], HTTPClient.NVPair[], HTTPClient.NVPair[], HTTPClient.FilenameMangler)
public static final byte[] mpFormDataEncode(NVPair[] opts, NVPair[] files, NVPair[] ct_hdr, FilenameMangler mangler) throws java.io.IOException
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" Content-Type: text/plain Gnus and Gnats are not Gnomes. -----------------------------114975832116442893661388290519--where the "Gnus and Gnats ..." is the contents of the file comment.txt in the current directory.
If no elements are found in the parameters then a zero-length byte[] is returned and the content-type is set to application/octet-string (because a multipart must always have at least one part.
For files an attempt is made to discover the content-type, and if found a Content-Type header will be added to that part. The content type is retrieved using java.net.URLConnection.guessContentTypeFromName() - see java.net.URLConnection.setFileNameMap() for how to modify that map. Note that under JDK 1.1 by default the map seems to be empty. If you experience troubles getting the server to accept the data then make sure the fileNameMap is returning a content-type for each file (this may mean you'll have to set your own).
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.
null elements in the array are ingored.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). null elements in the array
are ingored.ct_hdr
- 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). The exception to this is that if no
opts or files are given the type is set to
"application/octet-stream" instead.mangler
- the filename mangler, or null if no mangling is
to be done. This allows you to change the name
used in the filename attribute of the
Content-Disposition header. Note: the mangler
will be invoked twice for each filename.java.io.IOException
- If any file operation fails.public static final java.lang.String nv2query(NVPair[] pairs)
pairs
- the array of name/value pairspublic static final NVPair[] query2nv(java.lang.String query) throws ParseException
query
- the query string containing the encoded name/value pairsParseException
- If the '=' is missing in any field, or if
the urldecoding of the name or value failspublic static final byte[] chunkedEncode(byte[] data, NVPair[] ftrs, boolean last)
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.public static final byte[] chunkedEncode(byte[] data, int off, int len, NVPair[] ftrs, boolean last)
data
- the data to be encoded; may be null.off
- an offset into the datalen
- the number of bytes to take from dataftrs
- optional headers to include in the footer (ignored if
not last); may be null.last
- whether this is the last chunk.public static final java.lang.Object chunkedDecode(java.io.InputStream input) throws ParseException, java.io.IOException
input
- the stream from which to read the next chunk.ParseException
- If any exception during parsing occured.java.io.IOException
- If any exception during reading occured.