package com.mindprod.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.zip.GZIPInputStream;
/**
* Read a stream from a server.
* <p/>
* See com.mindprod.submitter for sample code to use this class.
*
* @author Roedy Green, Canadian Mind Products
* @version 2.3 2010-11-14 new method setInstanceFollowRedirects
* @since 1998
*/
@SuppressWarnings( { "WeakerAccess" } )
public final class Read
{
/**
* characters should be arriving within a millisecond in ordinary circumstances. In order to avoid hogging at the
* CPU in a mad loop to read non-existence characters, we sleep and try again later. Time to sleep in ms.
*/
private static final int SLEEP_TIME = 100;
/**
* Static only. Prevent instantiation.
*/
private Read()
{
}
/**
* 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.
*
* @param in stream to read
* @param b the buffer into which the data is read.
* @param off the start offset of the data in the array, not offset into the file!
* @param len the number of bytes to read.
*
* @return number of bytes actually read.
* @throws IOException if an I/O error occurs, usually a timeout.
*/
@SuppressWarnings( { "NestedAssignment", "EmptyCatchBlock" } )
public static int readBytesBlocking( final InputStream in,
final byte b[],
final int off,
final int len ) throws IOException
{
int totalBytesRead = 0;
while ( totalBytesRead < len )
{
final int bytesRead = in.read( b, off + totalBytesRead, len - totalBytesRead );
if ( bytesRead < 0 )
{
return totalBytesRead;
}
totalBytesRead += bytesRead;
}
return totalBytesRead;
}
/**
* 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.
*
* @param is InputStream to read from.
* @param estimatedLength Estimated number of <strong>bytes</strong> 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.
* @param gzipped true if the bytes are compressed with gzip. Request decompression.
* @param charSet The encoding of the byte stream. readStringBlocking converts to a standard Unicode-16
* String. usually UTF-8 or ISO-8859-1.
*
* @return String representing the contents of the entire stream.
* @throws IOException if connection lost, timeout etc., possibly UnsupportedEncodingException If the named charset
* is not supported
*/
@SuppressWarnings( { "NestedAssignment", "EmptyCatchBlock" } )
public static String readStringBlocking( InputStream is,
int estimatedLength,
boolean gzipped,
Charset charSet ) throws IOException
{
if ( estimatedLength <= 0 )
{
estimatedLength = Http.DEFAULT_LENGTH;
}
final int chunkSizeInBytes = Math.min( estimatedLength, 4 * 1024 );
final byte[] ba = new byte[ chunkSizeInBytes ];
final ByteArrayOutputStream baos = new ByteArrayOutputStream( estimatedLength + 1024 );
final InputStream decoder;
if ( gzipped )
{
decoder = new GZIPInputStream( is, 4 * 1024);
}
else
{
decoder = is;
}
int bytesRead;
while ( ( bytesRead = decoder.read( ba, 0, chunkSizeInBytes ) ) >= 0 )
{
baos.write( ba, 0, bytesRead );
}
decoder.close();
baos.close();
return new String( baos.toByteArray(), charSet );
}
}