扩展输入流类时的问题



我正在尝试扩展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 类的读取方法。

我不知道我在这里错过了什么。

我使用 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);
 } 
}

如果要扩展输入流类,则需要为以下方法提供具体定义:

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

相关内容

  • 没有找到相关文章

最新更新