net.noderunner.http
Class HttpUtil

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

public class HttpUtil
extends java.lang.Object

Contains utility functions for common HTTP I/O tasks.


Method Summary
static java.lang.String[] add(java.lang.String[] sa, java.lang.String s)
          Creates and returns a new array one entry longer, with a new value at the end.
static void discard(java.io.BufferedReader r)
          Discards the contents of a BufferedReader.
static java.lang.String read(java.io.BufferedReader r)
          Returns the contents of a BufferedReader as a String.
static byte[] read(java.io.InputStream stream)
          Reads an input stream until EOF.
static int readFully(java.io.InputStream stream)
          Reads an input stream until EOF.
static java.lang.String readHttpLine(java.io.InputStream in)
          Returns a single line from a 8-bit InputStream (which is assumed to be ASCII).
static java.util.Map<java.lang.String,java.lang.String[]> urlDecode(java.io.InputStream is)
          URL decodes an input stream.
static java.util.Map<java.lang.String,java.lang.String[]> urlDecode(java.lang.String urlEncodedData)
          URL decodes a string into a Map.
static java.lang.String[] urlDecodeToArray(java.io.InputStream is)
          URL decodes an input stream.
static java.lang.String[] urlDecodeToArray(java.lang.String urlEncodedData)
          Performs the same operation as urlDecode(java.lang.String), except the returned data is in an ordered array.
static void urlEncode(java.io.ByteArrayOutputStream os, byte[] buf)
          URL encodes a single value, writing its value to an output stream.
static byte[] urlEncode(java.util.Map<java.lang.String,java.lang.String> map)
          URL encodes a Map.
static byte[] urlEncode(java.util.Map<java.lang.String,java.lang.String> map, java.lang.String encoding)
          URL encodes a Map.
static byte[] urlEncode(java.lang.String[] nvPairs)
          URL encodes a series of parameters, using the Strings are converted to the default platform encoding.
static byte[] urlEncode(java.lang.String[] nvPairs, java.lang.String encoding)
          URL encodes a series of parameters.
static java.io.InputStream wrapInputStream(java.io.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(java.io.InputStream stream)
                     throws java.io.IOException
Reads an input stream until EOF. Returns the number of bytes read.

Throws:
java.io.IOException

read

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

Throws:
java.io.IOException

wrapInputStream

public static java.io.InputStream wrapInputStream(java.io.InputStream stream,
                                                  MessageHeaders hl)
                                           throws java.io.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:
java.io.IOException

readHttpLine

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

Throws:
java.io.IOException

urlEncode

public static byte[] urlEncode(java.lang.String[] nvPairs,
                               java.lang.String encoding)
                        throws java.io.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:
java.io.UnsupportedEncodingException - if the encoding is invalid

urlEncode

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


urlEncode

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


urlEncode

public static byte[] urlEncode(java.util.Map<java.lang.String,java.lang.String> map,
                               java.lang.String encoding)
                        throws java.io.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:
java.io.UnsupportedEncodingException - if the encoding is invalid

urlEncode

public static byte[] urlEncode(java.util.Map<java.lang.String,java.lang.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 java.lang.String[] add(java.lang.String[] sa,
                                     java.lang.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 java.util.Map<java.lang.String,java.lang.String[]> urlDecode(java.lang.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:
java.lang.IllegalArgumentException - if null data is passed in
See Also:
urlEncode(java.lang.String[], java.lang.String)

urlDecode

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

Returns:
mapping of name value pairs
Throws:
java.io.IOException

urlDecodeToArray

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

Returns:
array of name value pairs
Throws:
java.io.IOException

urlDecodeToArray

public static java.lang.String[] urlDecodeToArray(java.lang.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(java.io.BufferedReader r)
                    throws java.io.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:
java.io.IOException

read

public static java.lang.String read(java.io.BufferedReader r)
                             throws java.io.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:
java.lang.IllegalArgumentException - if the reader is null
java.io.IOException


Copyright © 2010. All Rights Reserved.