Results 1 to 10 of 10
  1. #1
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Who did the job for the InputStream

    As we know, the Java API has an abstract class InputStream. It provide a abstract method int read(). The other methods in the InputStream rely on this method to read a byte from the outer stream.
    Since the method doesn't provide any implementation, neither by this child class, for instance FileInputStream.
    Who did the job for InputStream?
    How can I learn about this information.
    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Who did the job for the InputStream

    The source for many of the java classes is in the JDK in a zip file. You could look there.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Who did the job for the InputStream

    Quote Originally Posted by Norm View Post
    The source for many of the java classes is in the JDK in a zip file. You could look there.
    I am sorry,but I don't understand what do you mean?
    The API source code? or some other zip files?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Who did the job for the InputStream

    Have you installed the JDK? Search in its folders for a zip file. One of them has the source for many of the java classes.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Who did the job for the InputStream

    Let I say, I read the source code InputStream.java and FileInputStream.java
    That's why I have this question.
    I am not sure whether you ask me to read the source code or rather than other Zip file.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Who did the job for the InputStream

    As we know, the Java API has an abstract class InputStream. It provide a abstract method int read(). The other methods in the InputStream rely on this method to read a byte from the outer stream.
    Since the method doesn't provide any implementation, neither by this child class, for instance FileInputStream.
    FileInputStream *does* provide an implementation for the abstract method kinda/sorta:

    Java Code:
        /**
         * Reads a byte of data from this input stream. This method blocks
         * if no input is yet available.
         *
         * @return     the next byte of data, or <code>-1</code> if the end of the
         *             file is reached.
         * @exception  IOException  if an I/O error occurs.
         */
        public native int read() throws IOException;
    It declares the method to be native. (See, eg this old JavaWorld article.)

    As for what (native) library is being used: I don't know, but you should be able to find out from the java code what libraries it loads. (Nothing from within FileInputStream.java itself, by the looks of things.) As for the native source code for these libraries you might try somewhere like OpenJDK: JDK 6 - I am pretty sure the code is (or was) available under the Java Research License.

  7. #7
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Who did the job for the InputStream

    Oh,Thanks! I guess it should be what I am looking for.

  8. #8
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Who did the job for the InputStream

    I review this guide
    http://java.sun.com/docs/books/jni/download/jni.pdf
    It is very helpful to understand the JNI, although I also don't where the Java API calls the library yet.
    Now I rise another confusion. I thought the output should work like the input one, but it seems not the same case
    for instance,we all know
    System.out.print(int b) can output the bytes, The System has a static field "out" is a PrintStream. It implements the print(int b)
    but the print(int b) calls the write(b). PrintStream calls the write(int b) from its parent FilterOutputStream, so I guess the write(int b) should be implemented in FilterOutputStream, but it doesn't!

    public FilterOutputStream(OutputStream out) {
    this.out = out;
    }
    public void write(int b) throws IOException {
    out.write(b);
    }
    Did I miss anything again? Thanks!

  9. #9
    neosky11 is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Who did the job for the InputStream

    Quote Originally Posted by neosky11 View Post
    I review this guide
    http://java.sun.com/docs/books/jni/download/jni.pdf
    It is very helpful to understand the JNI, although I also don't where the Java API calls the library yet.
    Now I rise another confusion. I thought the output should work like the input one, but it seems not the same case
    for instance,we all know
    System.out.print(int b) can output the bytes, The System has a static field "out" is a PrintStream. It implements the print(int b)
    but the print(int b) calls the write(b). PrintStream calls the write(int b) from its parent FilterOutputStream, so I guess the write(int b) should be implemented in FilterOutputStream, but it doesn't!

    public FilterOutputStream(OutputStream out) {
    this.out = out;
    }
    public void write(int b) throws IOException {
    out.write(b);
    }
    Did I miss anything again? Thanks!
    After reading the System.java code carefully, I found that the System.out is a special one.
    The FileOutputStream is similar to the FileInoutStream, you will the final one is a native one.
    But the System.out doesn't directly use the native one, I guess it uses some initial jvm action to do this thing.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Who did the job for the InputStream

    I review this guide
    http://java.sun.com/docs/books/jni/download/jni.pdf
    It is very helpful to understand the JNI, although I also don't where the Java API calls the library yet.
    Yes - I was going to refer you to something that described the JNI. When I looked at FileOutputStream - as I mentioned - I couldn't see which native library was involved. Perhaps you could check the classes mentioned in that class's fields and constructors.

    But the System.out doesn't directly use the native one
    This is quite common. PrintStream calls a method on its OutputStream field and that one (?) is the native one. In general you have the "chase" a Java method back to find its native origins. I suppose (but I'm no expert) that *every* method is like this: look far enough back and you will find a library (.dll, .so or whatever) that does the right thing for the operating system it is designed for.

Similar Threads

  1. InputStream and BufferedInputStream
    By dalu in forum New To Java
    Replies: 2
    Last Post: 10-21-2011, 02:29 PM
  2. Creating an InputStream
    By c26354 in forum New To Java
    Replies: 10
    Last Post: 07-12-2011, 04:48 AM
  3. Problem with InputStream
    By hedonist in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:09 PM
  4. InputStream problem
    By javabarn in forum New To Java
    Replies: 10
    Last Post: 06-29-2010, 04:09 PM
  5. Java InputStream
    By Bill88 in forum New To Java
    Replies: 10
    Last Post: 09-21-2009, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •