-3

I'm trying to write my input stream which extends InputStream and learnt the following

Input stream is an abstract class which contains abstract methods and implementation for some method it has. Noticed that InputStream contains an abstract method

public abstract int read() throws IOException;

I'm not very sure about what this method does. So, referred for someother inputstream which extends InputStream and got FilterInputStream which extends InputStream and the read method implementation in this is like

public int read() throws IOException {
    return in.read();
}

in here refers to the underlying input stream. Consider the constructor of FilterInputStream which is like

 protected FilterInputStream(InputStream in) {
        this.in = in;
 }

In read() method implementation this simply invokes the read method in its parent class which is InputStream which is just an abstract method.

  1. What read() method in InputStream does?
  2. What happens when i invoke the read method of FSDataInputStream?
  3. Do we add read() method here just to avoid compilation error? Or do we have any other use with this?

I'm confused with these. Please help me know about it.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • 4
    1. Read the javadoc. 2. Read the javadoc. 3. Read the javadoc. Seriously, read the javadoc. The whole point of an InputStream is to be able to read bytes from a stream of bytes. read() is **the** method doing that: it reads the next byte in the stream of bytes that is an InputStream. – JB Nizet Dec 05 '16 at 07:31
  • Read the javadocs @JBNizet still I'm not clear.... If read() method reads the data from input stream how much does it read? will it read entire data from input stream? – Tom Taylor Dec 05 '16 at 07:33
  • What exactly would input be for if you didn't have a way of reading it? – user207421 Dec 05 '16 at 07:35
  • 3
    Read the javadoc. It says: *Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.* So it returns 1 byte. It's clearly documented. – JB Nizet Dec 05 '16 at 07:36
  • 2
    I have my doubts as to whether OP actually read the javadoc, because I can't see how *"Returns: **the next byte** of data"* can be unclear. – Andreas Dec 05 '16 at 07:43
  • And how can you possibly be implementing an `InputStream` if you don't already have a clear idea of what reading from it means? Your question doesn't make sense. – user207421 Dec 05 '16 at 09:25
  • @EJP I'm just extending it with some additional functionality.. First I have to be clear with methods of `InputStream` so have asked.. – Tom Taylor Dec 05 '16 at 11:44

2 Answers2

2

What read() method in InputStream does?

It reads a byte of data. It must be implemented by any (non-abstract) subclass of InputStream.

In the FilterInputStream case, it is implemented by reading from another stream; i.e. the stream that the filter is wrapping. That stream will be an instance of some subclass of InputStream that implements read() to actually read data from somewhere.

What happens when i invoke the read method of FSDataInputStream?

You call end up calling read() on the stream that FSDataInputStream wraps; i.e. the one passed in the FSDataInputStream constructor.

Do we add read() method here just to avoid compilation error? Or do we have any other use with this?

The purpose of the read() method in the InputStream API is to be a placeholder for an actual method in an actual stream class.

The purpose of the read() method in FilterInputStream is to be the actual method. And it does this by delegating the read() call to the next stream in the chain. (Note that in general, FilterInputStream has to be subclassed to be useful, and you would expect that the subclass would override at least some of the read methods.)

In this case, the FSDataInputStream (which is a FilterInputStream) acts as an adapter that allows a regular FSInputStream to be used as a DataInputStream. For the read() method, simple delegation is sufficient.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Very clearly explained @Stephen would be much happy if you could provide me an example of an underlying stream implementation example which implements this actual `read()` method. – Tom Taylor Dec 05 '16 at 07:46
  • @RajasubaSubramanian *Read the Javadoc.* There are already several examples of exactly that in `java.io`., and Stephen has already named two of them. – user207421 Dec 05 '16 at 07:48
  • There are lots of them in the Java source code. You can find some of them here: http://grepcode.com/search/usages?type=type&id=repository.grepcode.com%24java%24root@jdk%24openjdk@6-b14@java%24io@InputStream&k=d . Note that the input stream classes that read stuff from outside of the JVM will end up in calls to `native` methods. You need the full OpenJDK source code to see the C++ code that implements them. – Stephen C Dec 05 '16 at 07:55
0

As, @JB Nizet suggested ByteArrayInputStream implements the read in a clear way and the code snippet is as follows,

public synchronized int read() {
    return (pos < count) ? (buf[pos++] & 0xff) : -1;
}

So, this returns the next byte in the buffer which is declared like

protected byte buf[];

where buf[] is an array of bytes that was provided by the creator of the stream. The elements in the buffer are the only bytes that can ever be read from the stream; element buf[pos] is the next byte to read.

Why it returns the result like (buf[pos++] & 0xff)?

Here is a nice explanation which makes us clear on why value & with 0xff is done.

ie; to return the result in signed values (between 0 to 255). This converts the unsigned value (byte) to signed value by & with 0xff.

Community
  • 1
  • 1
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63