Google Answers Logo
View Question
 
Q: FilteredInputStream in Java ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: FilteredInputStream in Java
Category: Computers > Programming
Asked by: brianct-ga
List Price: $8.00
Posted: 20 Dec 2003 17:09 PST
Expires: 19 Jan 2004 17:09 PST
Question ID: 289135
I am reading the book java An object-oriented Approach by Arnow and
Weiss and am wondering why a sample code that is provided in the book
does not work.  Please help me fix the code so it works and explain
why it did not work (it is the exact code in the book).  I am using
NetBeans IDE 3.5.1 with J2SE 1.4.2.

import java.net.*;
import java.io.*;

public class WHWWW {
    
    public static void main(String[] args) throws Exception {
        URL u = new URL("http://www.whitehouse.gov/");
        FilterInputStream ins = u.openStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader whitehouse = new BufferedReader(isr);
        System.out.println(whitehouse.readLine());
        System.out.println(whitehouse.readLine());
        
    }
    
}
Answer  
Subject: Re: FilteredInputStream in Java
Answered By: endo-ga on 20 Dec 2003 17:53 PST
Rated:5 out of 5 stars
 
Hi,

Thank you for your question. Here is the correct code. You will see
that the change is very minor:

package counting;

import java.net.*;
import java.io.*;

public class WHWWW {

    public static void main(String[] args) throws Exception {
        URL u = new URL("http://www.whitehouse.gov/");
        FilterInputStream ins = (FilterInputStream)u.openStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader whitehouse = new BufferedReader(isr);
        System.out.println(whitehouse.readLine());
        System.out.println(whitehouse.readLine());

    }

}


You need specifically cast the InputStream into a FilterInputStream,
that's what (FilterInputStream) does. The Java compiler does not allow
you to use FilterInputStream where InputStream is expected.

There are other (more complicated) solutions provided on:

javajunkies
http://www.javajunkies.org/index.pl?lastnode_id=458&node_id=1034


I hope this answers your question. If anything is unclear or you
require any clarifications, please do not hesitate to ask.

Thanks.
endo

Request for Answer Clarification by brianct-ga on 20 Dec 2003 19:17 PST
Though your answer (and the comment) was extremely helpful, I am still
getting this

error: cannot read: C:\Documents; and
Settings\brian\.netbeans\3.5\sampledir\WHWWW.java
1 error
Errors compiling.

I copied your exact code and the code from the comment.

Clarification of Answer by endo-ga on 20 Dec 2003 20:29 PST
Hi,

Can you please try creating a new file in another directory and try
compiling it again please?

The code definitely works, I've tried it, so it must be some other
problem. Try putting the file in C:\j2sdk1.4.2\bin and compile it from
please. Please make sure it is not write protected or anything like
that.

Thanks.
endo
brianct-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
Thanks for the help.

Comments  
Subject: Re: FilteredInputStream in Java
From: eadfrith-ga on 20 Dec 2003 17:55 PST
 
I checked the errata for the book and sure enough there is a problem with the
code you were trying to use. The errata can be found here:

ftp://ftp.aw.com/cseng/authors/arnow/Java2SourceCode/Errata.txt

The relevent entry states:

---------
Chapter 3
---------

- page 79- The WHWWW class contains two typos:
    
- A 'throws Exception' is missing from the signature of the 'main' method
    
- The declaration:
	FilterInputStream ins = u.openStream();
    shoud read:
	InputStream ins = u.openStream();

- The online source code contains the correct version.

The online code can be found here:

ftp://ftp.aw.com/cseng/authors/arnow/Java2SourceCode/

The corrected code is:

import  java.net.*;
import java.io.*;

class WHWWW {
   public static void main(String[] arg) throws Exception {
   		URL u = new URL("http://www.whitehouse.gov/");
		InputStream ins = u.openStream();
		InputStreamReader isr = new InputStreamReader(ins);
		BufferedReader whiteHouse = new BufferedReader(isr);
		System.out.println(whiteHouse.readLine());
		System.out.println(whiteHouse.readLine());
		System.out.println(whiteHouse.readLine());
		System.out.println(whiteHouse.readLine());
		System.out.println(whiteHouse.readLine());
	}
}

The problem with the original code is that it tries to assign the
object returned by the openStream method of the URL class, which in
the API is declared to be of type InputStream, to a variable of type
FilterInputStream. The relevent line is:

FilterInputStream ins = u.openStream();

This will cause a compile time error, since it attempts a narrowing
conversion without a cast. In plane English this means that you're
trying to store an object of a given class in a variable that's
declared to be a more specific class. If you knew that the object
returned was in fact a FilterInputStream, or a subclass, then you
could correct the code by adding a narrowing cast, as in:

FilterInputStream ins = (FilterInputStream )u.openStream();

This will now compile. However, it's not correct, since we don't know,
and shouldn't care, what the actual type of the object returned is. In
fact if you were to check, by printing the class of the object, you'd
find it was an instance of the HttpURLConnection class.

Hope this helps.

Cheers,

Eadfrith
Subject: Re: FilteredInputStream in Java
From: eadfrith-ga on 21 Dec 2003 10:28 PST
 
I don't believe the problem is with your code but with the java 
compiler getting confused by the spaces in the location of the file. 
I've not used netbeans, but it appears that when you try to compile 
your class it's issuing the command:

javac C:\Documents and Settings\brian\.netbeans\3.5\sampledir\WHWWW.java

which is fine, except that javac sees the spaces in the "Document and 
Settings" and breaks up the command line. This is an issue under 
windows.

How to fix it? If this was the command line you could simple put quotes 
around the filename, as in 

javac "C:\Documents and Settings\brian\.netbeans\3.5
\sampledir\WHWWW.java"

but since netbeans is issuing the compile command I'm not sure how to 
cause it to do this.

The simpler solution would be to create your project in a location 
other than "Documents and Settings\brian", which is the actual location 
of the "My Documents" folder under windows. Create it in a location 
which doesn't have any spaces and you should be fine.

Cheers,

Eadfrith
Subject: Re: FilteredInputStream in Java
From: endo-ga on 21 Dec 2003 16:24 PST
 
Thank you for the great rating and generous tip.
Regards,
endo

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy