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!
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.
Re: Who did the job for the InputStream
Quote:
Originally Posted by
Norm
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?
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.
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.
Re: Who did the job for the InputStream
Quote:
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:
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.
Re: Who did the job for the InputStream
Oh,Thanks! I guess it should be what I am looking for.
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!
Re: Who did the job for the InputStream
Quote:
Originally Posted by
neosky11
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.
Re: Who did the job for the InputStream
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.
Quote:
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.