【问题标题】:issue while extending InputStream class扩展 InputStream 类时的问题
【发布时间】:2015-03-26 10:39:44
【问题描述】:

我正在尝试扩展 InputStream 类并使用自定义的 read() 方法。 这是我的课堂快照:

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
         {
         //..................
         }
     }
 }

这是我的客户端类

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);

     } 
 }

我想做的是在 cl1.read 完成后调用 MyInputStream 类的读取方法。

我不知道我在这里缺少什么。

【问题讨论】:

    标签: java inputstream datainputstream bufferedinputstream


    【解决方案1】:

    我使用 MyInputStream 创建了 DataInputStream 对象并让它工作。这是更新的代码:

    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);
    
     } 
    }
    

    【讨论】:

      【解决方案2】:

      如果你要扩展输入流类,那么你需要给出以下方法的具体定义:

      public abstract int read() throws IOException
      

      你的类有 read 方法,其签名为:

      public int read(byte[] b) throws IOException
      

      所以请在read(byte[] b)之外实现read()。我做了一些修改,现在可以使用了...

      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;
              }
           }
       }
      

      【讨论】:

      • 是的,也添加了读取功能。以下是我在 MyClass 中实现的功能: - - 1. public synchronized int read() throws IOException 2. public synchronized int read( byte b[ ] ) 抛出 IOException 3. public synchronized int read( byte b[], int off, int len ) 抛出 IOException 4. public synchronized int read( byte b[], int off, int len, byte t[] ) 抛出 IOException 5 . public synchronized int available() 抛出 IOException
      • 是的,它是这样工作的,但我想让它与 BufferedInputStream / DataInputStream 一起工作。我是否有可能对 DataInputStream 实例使用 read() 操作并执行 MyClass 的实现?
      • 在编译时参考决定将调用哪个方法,而在运行时,对象实际上决定将调用哪个方法。在这种情况下,mBufInStream 属于 Super 类,而 object 也属于 BufferedInputStream ,因此在运行时 jvm 会考虑调用 BufferedInputStream 类中的对象和覆盖方法。
      • 是的,实际上DataInputStream对底层Inputstream对象进行了读取操作。我通过使用 MyInputStream 对象创建 DataInputStream 对象来实现它。不过还是谢谢。
      • 嘿,你确定当你修改上面提到的代码时调用了 DataInputStream 的读取方法,即通过执行 -> mInStream.read(buffer);我不相信 MyInputStream 的 read() 会被使用引用变量 mInStream 调用,实际上我尝试了相同的代码并且调用的是 DataInputStream 的读取方法而不是 MyInputStream 的读取方法。
      猜你喜欢
      • 2011-06-17
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多