net.noderunner.http
Class HttpUtil

java.lang.Object
  extended by net.noderunner.http.HttpUtil

public class HttpUtil
extends Object

Contains utility functions for common HTTP I/O tasks.


Method Summary
static String[] add(String[] sa, String s)
          Creates and returns a new array one entry longer, with a new value at the end.
static void discard(BufferedReader r)
          Discards the contents of a BufferedReader.
static String read(BufferedReader r)
          Returns the contents of a BufferedReader as a String.
static byte[] read(InputStream stream)
          Reads an input stream until EOF.
static int readFully(InputStream stream)
          Reads an input stream until EOF.
static String readHttpLine(InputStream in)
          Returns a single line from a 8-bit InputStream (which is assumed to be ASCII).
static InputStream uncloseableInputStream(InputStream stream)
          Returns a new input stream delegating to the given input stream that does nothing when closed.
static Map<String,String[]> urlDecode(InputStream is)
          URL decodes an input stream.
static Map<String,String[]> urlDecode(String urlEncodedData)
          URL decodes a string into a Map.
static String[] urlDecodeToArray(InputStream is)
          URL decodes an input stream.
static String[] urlDecodeToArray(String urlEncodedData)
          Performs the same operation as urlDecode(java.lang.String), except the returned data is in an ordered array.
static void urlEncode(ByteArrayOutputStream os, byte[] buf)
          URL encodes a single value, writing its value to an output stream.
static byte[] urlEncode(Map<String,String> map)
          URL encodes a Map.
static byte[] urlEncode(Map<String,String> map, String encoding)
          URL encodes a Map.
static byte[] urlEncode(String[] nvPairs)
          URL encodes a series of parameters, using the Strings are converted to the default platform encoding.
static byte[] urlEncode(String[] nvPairs, String encoding)
          URL encodes a series of parameters.
static InputStream wrapInputStream(InputStream stream, MessageHeaders hl)
          Returns a character reader for reading HTTP data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readFully

public static int readFully(InputStream stream)
                     throws IOException
Reads an input stream until EOF. Returns the number of bytes read.

Throws:
IOException

read

public static byte[] read(InputStream stream)
                   throws IOException
Reads an input stream until EOF. Returns the bytes read.

Throws:
IOException

wrapInputStream

public static InputStream wrapInputStream(InputStream stream,
                                          MessageHeaders hl)
                                   throws IOException
Returns a character reader for reading HTTP data. Returns either an InputStreamReader, wrapping either a ChunkedInputStream or LimitedInputStream, based on the supplied headers. If the headers did not indicate data was being sent, returns an input stream for reading the rest of the document.

Throws:
IOException

uncloseableInputStream

public static InputStream uncloseableInputStream(InputStream stream)
                                          throws IOException
Returns a new input stream delegating to the given input stream that does nothing when closed.

Throws:
IOException

readHttpLine

public static String readHttpLine(InputStream in)
                           throws IOException
Returns a single line from a 8-bit InputStream (which is assumed to be ASCII).

Throws:
IOException

urlEncode

public static byte[] urlEncode(String[] nvPairs,
                               String encoding)
                        throws UnsupportedEncodingException
URL encodes a series of parameters. Names are followed by their value in the array. For example:
 byte[] buf;
 buf = HttpUtil.urlEncode(new String[] { "name1", "value1", "name2", "value2" }); 
 
results in the byte array:
 "name1=value1&name2=value2"
 
Strings are converted to bytes using the given encoding.

Parameters:
nvPairs - array of name-value pairs, must be even in length
encoding - Java encoding, or null to use the default encoding
Returns:
a byte array which can be used in EasyHttpClient.doPostUrlEncoded(byte[])
Throws:
UnsupportedEncodingException - if the encoding is invalid

urlEncode

public static byte[] urlEncode(String[] nvPairs)
URL encodes a series of parameters, using the Strings are converted to the default platform encoding.


urlEncode

public static void urlEncode(ByteArrayOutputStream os,
                             byte[] buf)
URL encodes a single value, writing its value to an output stream.


urlEncode

public static byte[] urlEncode(Map<String,String> map,
                               String encoding)
                        throws UnsupportedEncodingException
URL encodes a Map.

Parameters:
map - a name-value map of entries to encode
encoding - Java encoding, or null to use the default encoding
Returns:
a byte buffer which can be used in EasyHttpClient.doPostUrlEncoded(byte[])
Throws:
UnsupportedEncodingException - if the encoding is invalid

urlEncode

public static byte[] urlEncode(Map<String,String> map)
URL encodes a Map.

Parameters:
map - a name-value map of entries to encode
Returns:
a byte buffer which can be used in EasyHttpClient.doPostUrlEncoded(byte[]) Strings are converted to the default platform encoding.

add

public static String[] add(String[] sa,
                           String s)
Creates and returns a new array one entry longer, with a new value at the end.

Parameters:
sa - existing array
s - new value
Returns:
new array

urlDecode

public static Map<String,String[]> urlDecode(String urlEncodedData)
URL decodes a string into a Map. Names without values are placed in the map, but given a null value. For example:
 foo&bar=&baz
 
results in a map with keys foo, bar, baz mapped to "". If the same key appears more than once, it is added to the array. This method will not throw exceptions even if the data is irregular.

Parameters:
urlEncodedData - data in the URL encoded format
Returns:
a map containing the decoded name-value pairs
Throws:
IllegalArgumentException - if null data is passed in
See Also:
urlEncode(java.lang.String[], java.lang.String)

urlDecode

public static Map<String,String[]> urlDecode(InputStream is)
                                      throws IOException
URL decodes an input stream.

Returns:
mapping of name value pairs
Throws:
IOException

urlDecodeToArray

public static String[] urlDecodeToArray(InputStream is)
                                 throws IOException
URL decodes an input stream.

Returns:
array of name value pairs
Throws:
IOException

urlDecodeToArray

public static String[] urlDecodeToArray(String urlEncodedData)
Performs the same operation as urlDecode(java.lang.String), except the returned data is in an ordered array.

See Also:
urlEncode(java.lang.String[], java.lang.String)

discard

public static void discard(BufferedReader r)
                    throws IOException
Discards the contents of a BufferedReader. If the reader is null, does nothing: This is to facilitate the coding of cases when the data is of no use to the client.

Throws:
IOException

read

public static String read(BufferedReader r)
                   throws IOException
Returns the contents of a BufferedReader as a String. By default, each line is appended with System.getProperty("line.separator"). This may or may not be the original line termination character used.

Throws:
IllegalArgumentException - if the reader is null
IOException


Copyright © 2011. All Rights Reserved.