sk.baka.ambient.commons
Class IOUtils

java.lang.Object
  extended by sk.baka.ambient.commons.IOUtils

public final class IOUtils
extends Object

Contains IO and HTTP utility methods.

Author:
Martin Vysny

Nested Class Summary
static class IOUtils.HttpRequest
          The http request
 
Method Summary
static void cat(Reader r)
          Cat given reader to log.
static void copy(InputStream in, OutputStream out, int bufferSize)
          Copies input stream to the output stream.
static String encodeURL(String url)
          Encodes given URL as per URLEncoder.encode(String, String).
static String formatIP(int address)
          Formats IP address contained in an integer and returns it as a string.
static String getExt(String name)
          Returns the extension of the file, starting with dot character.
static IOUtils.HttpRequest parseRequest(String request)
          Parses given HTTP request.
static void parseXML(InputStream in, ContentHandler handler)
          Parses XML readable from given stream.
static String readLine(InputStream in)
          Reads a line from given stream.
static void readRequest(InputStream in)
          Reads the request until an empty string is encountered.
static String removeTrailingSlash(String name)
          Removes trailing slash from given file name.
static String stripExt(String name)
          Strips extension from given name.
static void writeHttpResponse(byte httpVer, int httpCode, boolean keepAlive, long size, String resultMime, OutputStream out)
          Writes HTTP response.
static void writeLine(OutputStream out)
          Writes an empty line to given stream.
static void writeLine(String string, OutputStream out)
          Writes a line to given stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encodeURL

public static String encodeURL(String url)
Encodes given URL as per URLEncoder.encode(String, String). Uses UTF-8 encoding. Spaces are correctly represented as %20.

Parameters:
url - the URL to encode
Returns:
URL-friendly string.

parseXML

public static void parseXML(InputStream in,
                            ContentHandler handler)
                     throws IOException,
                            SAXException
Parses XML readable from given stream. Note that the parser is configured to be namespace-aware - Android bug prevents us to use non-namespace-aware parser.

Parameters:
in - the stream, always closed.
handler - handles XML events.
Throws:
SAXException - if parsing fails
IOException - if i/o error occurs

readLine

public static String readLine(InputStream in)
                       throws IOException
Reads a line from given stream. No charset decoding is performed - only ASCII characters are expected. The line is expected to be terminated by the \n character. All \r characters are ignored.

Parameters:
in - the stream to read from. This should be an BufferedInputStream implementation for performance reasons.
Returns:
the line. May return null on end-of-stream.
Throws:
IOException - if i/o error occurs.

writeLine

public static void writeLine(String string,
                             OutputStream out)
                      throws IOException
Writes a line to given stream. No charset encoding is performed - only ASCII characters are expected. An additional \r\n characters will be written.

Parameters:
string - the string to write. It must consist of ASCII characters only.
out - the stream to write to.
Throws:
IOException - if i/o error occurs.

writeLine

public static void writeLine(OutputStream out)
                      throws IOException
Writes an empty line to given stream. An additional \r\n characters will be written.

Parameters:
out - the stream to write to.
Throws:
IOException - if i/o error occurs.

stripExt

public static String stripExt(String name)
Strips extension from given name. Does nothing if the file name does not have an extension.

Parameters:
name - the name to strip extension from
Returns:
stripped name.

copy

public static void copy(InputStream in,
                        OutputStream out,
                        int bufferSize)
                 throws IOException
Copies input stream to the output stream.

Parameters:
in - the input stream. Always closed.
out - the output stream. Always closed.
bufferSize - the buffer size in bytes
Throws:
IOException - thrown when i/o error occurs or when interrupted.

removeTrailingSlash

public static String removeTrailingSlash(String name)
Removes trailing slash from given file name.

Parameters:
name - the file name.
Returns:
file name without the trailing slash.

readRequest

public static void readRequest(InputStream in)
                        throws IOException
Reads the request until an empty string is encountered.

Parameters:
in - the stream to read from.
Throws:
IOException - if i/o error occurs.

parseRequest

public static IOUtils.HttpRequest parseRequest(String request)
                                        throws ServerHttpException
Parses given HTTP request.

Parameters:
request - the request
Returns:
parsed request or null if the request is malformed.
Throws:
ServerHttpException - on HTTP error.

writeHttpResponse

public static void writeHttpResponse(byte httpVer,
                                     int httpCode,
                                     boolean keepAlive,
                                     long size,
                                     String resultMime,
                                     OutputStream out)
                              throws IOException
Writes HTTP response. The response is not terminated by an empty line.

Parameters:
httpVer - the HTTP version, 0 or 1.
httpCode - the HTTP code.
keepAlive - if true then "keep-alive" is written. If false then "close" is written.
size - the size of the content. -1 if not known.
resultMime - response MIME type.
out - write the response here.
Throws:
IOException - if i/o error occurs

cat

public static void cat(Reader r)
Cat given reader to log.

Parameters:
r - the reader to cat.

formatIP

public static String formatIP(int address)
Formats IP address contained in an integer and returns it as a string.

Parameters:
address - the IP to decode
Returns:
decoded IP.

getExt

public static String getExt(String name)
Returns the extension of the file, starting with dot character.

Parameters:
name - the file name to get the extension from.
Returns:
extension or empty string if the file does not have an extension.


Copyright © 2007-2008. All Rights Reserved.