Java Reference
In-Depth Information
This recipe works differently than Recipe 3.4 in that the text file is not first loaded to a
string. Rather, the text file is read from the input stream as it is written to the output stream. A
method is provided, called
downloadText
, which accepts an input stream and an output
stream. The input stream should be from the URL, and the output stream should be to a disk
file. This method is shown here:
private void downloadText(InputStream is, OutputStream os) throws
IOException
{
The first thing that the
downloadText
method must do is obtain the line separator for
the current operating system. This can be done with a call to
System.getProperty
,
as shown here:
byte lineSep[] = System.getProperty("line.separator").getBytes();
Next, several variables are declared. First, the variable
ch
is used to hold the current
character, which was just read in from the
InputStream
. Next, a
boolean
named
inLineBreak
is used to hold whether the
InputStream
is currently inside of a line
break. The next two variables,
hadLF
and
hadCR
, are set if the line break was caused by a
line feed (char code 10) or a carriage return (char code 13). These lines are shown here:
int ch = 0;
boolean inLineBreak = false;
boolean hadLF = false;
boolean hadCR = false;
Next, a
do/while
loop is used to read each character in, and process it.
do
{
ch = is.read();
Each character is then checked to see if it is a line break character.
if (ch != -1)
{
if ((ch == '\r') || (ch == '\n'))
{
The above code checks to see if the character returned is -1, which indicates we have
reached the end and there are no more characters to read. Otherwise, we check to see if the
character returned was a line break character.
inLineBreak = true;
if (ch == '\r')
{
if (hadCR)
os.write(lineSep);
else
hadCR = true;