1

I am trying to extend InputStream class and use customized read() methods. This is my class snapshot:

class MyClass
{    
     /** Input stream */
     private final MyInputStream in = new MyInputStream();
     /**get the InputStream
     public InputStream getInputStream()
     {
         return in;
     }

     /** Inner class for MyInputStream */
     class MyInputStream extends InputStream
     {
         //here i am keeping implementation of read methods
         public synchronized int read( byte b[] ) throws IOException
         {
         //..................
         }
     }
 }

Here is my client class

public class MyClient {
     //InStreams
     protected BufferedInputStream mBufInStream;
     protected DataInputStream mInStream;

     public int read(byte[] buffer)
     {
           MyClass obj1 =  new MyClass();
           mBufInStream = new BufferedInputStream(obj1.getInputStream());
           mInStream = new DataInputStream(mBufInStream);
           try
           {
               int i = mBufInStream.read(buffer);
               return i;
           }
           catch (IOException ex)
           {
               return -1;
           }
     }

     public static void main(String args[])
     {
        MyClient cl1 = new MyClient();
        int ret = 0;
        byte[] data = {};

        ret = cl1.read(data);

     } 
 }

What i wanted to do is call my read method of MyInputStream Class when cl1.read is done.

I don't know what i am missing here.

dbarnes
  • 1,803
  • 3
  • 17
  • 31
user2147688
  • 95
  • 1
  • 9

2 Answers2

2

I created the DataInputStream object using MyInputStream and got it working. Here is the updated code:

public class MyClient {
 //InStreams
 protected DataInputStream mInStream;

 public int read(byte[] buffer)
 {
       MyClass obj1 =  new MyClass();
       mInStream = new DataInputStream(obj1.getInputStream());
       try
       {
           int i = mInStream.read(buffer);
           return i;
       }
       catch (IOException ex)
       {
           return -1;
       }
 }

 public static void main(String args[])
 {
    MyClient cl1 = new MyClient();
    int ret = 0;
    byte[] data = {};

    ret = cl1.read(data);

 } 
}
Holger
  • 285,553
  • 42
  • 434
  • 765
user2147688
  • 95
  • 1
  • 9
1

If you are extending input stream class then you will need to give the concrete definition for the following method:

public abstract int read() throws IOException

Your class has the read method with the signature as:

public int read(byte[] b) throws IOException

So please implement read() in addition to read(byte[] b). I have made some modifications and it works now...

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class MyClient {
     //InStreams
     protected BufferedInputStream mBufInStream;
     protected DataInputStream mInStream;

     public int read(byte[] buffer) {
           MyClass obj1 =  new MyClass();
        //   mBufInStream = new BufferedInputStream(obj1.getInputStream());
         //  mInStream = new DataInputStream(mBufInStream);
           try {
               int i = obj1.getInputStream().read(buffer);
               return i;
           } catch (IOException ex) {
               return -1;
           }
     }

     public static void main(String args[]) {
        MyClient cl1 = new MyClient();
        int ret = 0;
        byte[] data = {'a','b'};

        ret = cl1.read(data);
        System.out.println(ret);
     } 
 }



import java.io.IOException;
import java.io.InputStream;

class MyClass {

     /** Input stream */
     private final MyInputStream in = new MyInputStream();
     //get the InputStream
     public InputStream getInputStream() {
         return in;
     }

     class MyInputStream extends InputStream {
         //here i am keeping implementation of read methods
         public int read( byte b[] ) throws IOException {
            System.out.println("Inside my read()");
            return b.length;
         //..................
         }

        @Override
        public int read() throws IOException {
            // TODO Auto-generated method stub
            return 0;
        }
     }
 }
byxor
  • 5,930
  • 4
  • 27
  • 44
Rahul Prasad
  • 747
  • 1
  • 9
  • 13
  • yes have added the read function as well.. follwoing are the functions i have implemented in MyClass: - - 1. public synchronized int read() throws IOException 2. public synchronized int read( byte b[] ) throws IOException 3. public synchronized int read( byte b[], int off, int len ) throws IOException 4. public synchronized int read( byte b[], int off, int len, byte t[] ) throws IOException 5. public synchronized int available() throws IOException – user2147688 Jan 27 '15 at 19:22
  • yes it was working this way but i wanted to make it work with BufferedInputStream / DataInputStream. is it possible at all that i use read() operations on DataInputStream instance and implementation from MyClass gets executed? – user2147688 Jan 27 '15 at 20:22
  • At compile time reference decides which method will be called and at runtime , Object actually decides which method will be called. In this case , mBufInStream is of Super class and object is also of BufferedInputStream , so at runtime jvm considers the object and overriding method in class BufferedInputStream is called. – Rahul Prasad Jan 28 '15 at 04:24
  • Yes, actually DataInputStream perform the read operation on underlying Inputstream object. I got it working by creating DataInputStream Object using MyInputStream Object. Thanks though. – user2147688 Jan 28 '15 at 15:52
  • Hey are you sure that the read method of DataInputStream was called when you modified the code as mentioned above i.e. by doing -> mInStream.read(buffer); I do not believe read() of MyInputStream will be called by using the reference variable mInStream, in fact I tried the same code and the read method of DataInputStream is being called not the read method of MyInputStream. – Rahul Prasad Jan 28 '15 at 18:25
  • yes actually DataInputStream class will call read(byte b[], int off, int len) of Inputstream. it adds '0' as offset and length of byte array and then call the read method of underlying InputStream object. as i had defined this one also so i got it working. it calls "public int read(byte b[], int off, int len)" of MyInputStream class. – user2147688 Jan 28 '15 at 21:06