com.mindprod.http
Class Read

java.lang.Object
  extended by com.mindprod.http.Read

public final class Read
extends java.lang.Object

Read a stream from a server.

See com.mindprod.submitter for sample code to use this class.

Since:
1998
Version:
2.3 2010-11-14 new method setInstanceFollowRedirects
Author:
Roedy Green, Canadian Mind Products

Method Summary
static int readBytesBlocking(java.io.InputStream in, byte[] b, int off, int len, int timeoutInMillis)
          Reads exactly len bytes from the input stream into the byte array.
static java.lang.String readStringBlocking(java.io.InputStream is, int estimatedLength, long timeoutInMillis, boolean gzipped, java.lang.String charSet)
          Used to read until EOF on an InputStream that sometimes returns 0 bytes because data have not arrived yet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readBytesBlocking

public static int readBytesBlocking(java.io.InputStream in,
                                    byte[] b,
                                    int off,
                                    int len,
                                    int timeoutInMillis)
                             throws java.io.IOException
Reads exactly len bytes from the input stream into the byte array. This method reads repeatedly from the underlying stream until all the bytes are read. InputStream.read is often documented to block like this, but in actuality it does not always do so, and returns early with just a few bytes. readBytesBlocking blocks until all the bytes are read, the end of the stream is detected, or an exception is thrown. You will always get as many bytes as you asked for unless you get an eof or other exception. Unlike readFully, you find out how many bytes you did get. Formerly called readBlocking.

Parameters:
in - stream to read
b - the buffer into which the data is read.
off - the start offset of the data in the array, not offset into the file!
len - the number of bytes to read.
timeoutInMillis - give up after this amount of time.
Returns:
number of bytes actually read.
Throws:
java.io.IOException - if an I/O error occurs, usually a timeout.

readStringBlocking

public static java.lang.String readStringBlocking(java.io.InputStream is,
                                                  int estimatedLength,
                                                  long timeoutInMillis,
                                                  boolean gzipped,
                                                  java.lang.String charSet)
                                           throws java.io.IOException
Used to read until EOF on an InputStream that sometimes returns 0 bytes because data have not arrived yet. Does not close the stream. Formerly called readEverything.

Parameters:
is - InputStream to read from.
estimatedLength - Estimated number of bytes that will be read. -1 or 0 mean you have no idea. Best to make some sort of guess a little on the high side.
timeoutInMillis - give up after this amount of time.
gzipped - true if the bytes are compressed with gzip. Request decompression.
charSet - The encoding of the byte stream. readStringBlocking converts to a standard Unicode-16 String. usually UTF-8 or ISO-8859-1.
Returns:
String representing the contents of the entire stream.
Throws:
java.io.IOException - if connection lost, timeout etc., possibly UnsupportedEncodingException If the named charset is not supported