• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding InputStream and its subclass FileInputStream

 
Jack McGuire
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry if posted in wrong forum :-(
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, no worries!
I'll move the topic to our Android forum for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic