aspose file tools*
The moose likes Android and the fly likes Understanding InputStream and its subclass FileInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Understanding InputStream and its subclass FileInputStream" Watch "Understanding InputStream and its subclass FileInputStream" New topic
Author

Understanding InputStream and its subclass FileInputStream

Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
Think I understand it, but just checking.

The documentation for InputStream has these 2 public methods...

int read(byte[] buffer) - Equivalent to read(buffer, 0, buffer.length)
int read(byte[] buffer, int offset, int length) - Reads at most length bytes from this stream and stores them in the byte array b starting at offset

The documentation for FileInputStream (a subclass of InputStream) has only one corresponding method to those above...

int read(byte[] buffer, int offset, int length)

So I'm thinking that if I use an instance of FileInputStream class, I can still call the
int read(byte[] buffer)
method, which will reference the corresponding
int read(byte[] buffer)
in the parent InputStream which then calls the over-ridden
int read(byte[] buffer, int offset, int length)
from FileInputStream (using values buffer, 0, buffer.length as parameters) to read from the stream.

Is this the correct way to understand things?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Actually, FileInputStream overrides both the read(byte[]) and read(byte[], int, int) methods in InputStream. That fact is also apparent from the API documentation when you look at FileInputStream's method summary.
Beyond that, if and how they interact isn't really important, but just to satisfy your curiosity: the methods in FileInputStream don't call the super variants in InputStream, but instead invoke FileInputStream's private readBytes(byte[], int, int) method, which is declared native.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Jack McGuire wrote:The documentation for FileInputStream (a subclass of InputStream) has only one corresponding method to those above...

Which documentation are you reading ? If you follow the auto-generated link for FileInputStream to the Java 7 javadoc, it shows both methods.
Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
I see what you're saying Jelle Klap in the Java 7 Documentation and I also see what your saying about the super variants in InputStream not being "called".

I should have said, Stuart, that I was looking at FileInputStream in Android documentation - (I'm experimenting with Android). In this documentation, it seems to say that int read(byte[] buffer) is inherited and not overridden.

Or so it would seem. A difference between Oracle Java and Android Java, maybe.

But I think I get the picture.
Jack McGuire
Greenhorn

Joined: Jan 24, 2012
Posts: 10
Sorry if posted in wrong forum :-(
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Ah, no worries!
I'll move the topic to our Android forum for you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Understanding InputStream and its subclass FileInputStream