Hi Kim

You are right. In fact there are three methods to override. For e.g.:
-- Your Filter, inheriting FilterInputStream, has a constructor that sets the "in" member. As it is protected, you can access it from your derived class -- Your overridden read() just makes something like char c = Character.toUpperCase((char)in.Read()); -- The read(byte[] b) calls in.read(b), then converts all the bytes (assumed to be characters) to upper cases -- The read(byte[] b, int off, int len) calls in.read(b, off, len) then converts to upper cases only the bytes between the offset and offset + len. Something like:
   for ( int i = 0 ; i < len ; i++ ){
       b[off + i] = (byte)Character.toUpeerCase((char)b[off + i]);
   }

Hope it helps
Mihai


Kim Ching Koh a écrit :
Hi Mihai,
Not sure what do you mean by read a buffer, do you mean *read* <https://mail.google.com/mail/html/java/io/FilterInputStream.html#read%28byte%5B%5D%29>(byte[] b)? How about *read* <https://mail.google.com/mail/html/java/io/FilterInputStream.html#read%28byte%5B%5D,%20int,%20int%29>(byte[] b, int off, int len)? Need to override too? read(byte[] b) seems to be calling read(byte[] b, int off, int len), so we maybe able to to override read(byte[] b) but I am not sure how to override read(byte[] int off, int len) In Exercise 8, read(byte[] int off int len) seem to calling itself? How does this work?
public int read(byte[] b, int off, int len) throws IOException {
        len = in.read(b, off, len);
        if (len != -1) {
            cksum.update(b, off, len);
        }
        return len;
    }
Thanks.
KC


On Wed, May 26, 2010 at 2:15 PM, Mihai DINCA <[email protected] <mailto:[email protected]>> wrote:

    Hi Kim

    The idea of the topic is that InputStreams and OutputStreams are
    based on layers. Once you have an InputStream, you can read from
    it through a filter. For e.g., you can write InputStream in = new
    BufferedInputStream( new FileInputStream("filename.txt"));. The
    FileInputStream is the actual source of data, but you read it
    through a BufferedInputStream that allows a (more efficient)
    buffered read from the source, still allowing a byte-by-byte access.

    You can extend all this and write:
        InputStream in = new MyFilter1Stream( new MyFilter2Stream( new
    MyFilter3Stream( new FileInputStream("myfile.txt"))));
    where each filter adds something to the reading process. One of
    them might, for e.g., convert all the input characters to capital
    letters.

    The same idea applies to OutputStreams. You can have your own
    filter in the chain that converts, for e.g., all the dirty words
    in "***" strings.

    The homework is based by a lab exercise that reads from a file,
    through and InputStream, and echoes its contents to another one,
    through an OutputStream and asks to write other a filter for the
    input or a filter for the output that converts all letters to
    capital letters.

    To create your own FilterInputStream, you must override both read
    methods (the one that read on byte and the other that reads a
    buffer) so that they read from the source InputStream (one byte or
    a buffer), then convert the input to capital letters.
    Analogically, to create your own FilterOutputStream yo must
    override all the write methods.

    Hope it helps
    Mihai



    Kim Ching Koh a écrit :
    Hi,
Anyone has done this homework can advise? Regards
    KC

    On Tue, May 25, 2010 at 2:53 PM, kc <[email protected]
    <mailto:[email protected]>> wrote:

        Hi,

        I am not too sure what is the requirement of the homework for
        java
        stream I/O, is it just to overide read() in the
        ChangeToUpperCaseInputStream class so it will convert and
        return to
        uppercase?


        1. The homework is to either modify FilterInputOutputStream
        NetBeans
        project you've done in Exercise 8 above or create a new
        project as
        following.  (You might want to create a new project by
        copying the
        FilterInputOutputStream project.  You can name the homework
        project in
        any way you want but here I am going to call it
        MyIOStreamProject.)
        Write ChangeToUpperCaseInputStream class, which extends
        FilterInputStream.
        Write ChangeToUpperCaseOutputStream class, which extends
        FilterOutputStream.

        Use either ChangeToUpperCaseInputStream class or
        ChangeToUpperCaseOutputStream class to convert the characters
        read to
        upper case.

        Regards
        KC

        --
        To post to this group, send email to
        [email protected]
        <mailto:[email protected]>
        To unsubscribe from this group, send email to
        [email protected]
        <mailto:javaprogrammingwithpassion%[email protected]>
        For more options, visit this group at
        http://groups.google.com/group/javaprogrammingwithpassion?hl=en


-- To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/javaprogrammingwithpassion?hl=en


--
To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to