• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding InputStream and its subclass FileInputStream

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, no worries!
I'll move the topic to our Android forum for you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic