首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展inputStream的InputStream新类

扩展inputStream的InputStream新类
EN

Stack Overflow用户
提问于 2015-09-07 15:58:58
回答 2查看 4.4K关注 0票数 3

我创建了一个扩展InputStream的新类,它必须@Override ()。我试图使用read(int )方法,但是当我使用它时,它会转到read()方法,而我不能使用这个参数,我传递给了它。

这是我的代码:

代码语言:javascript
复制
public class Run {

    public static void main(String[] args) {

        DFSMaze3dGenerator mg = new DFSMaze3dGenerator();
        try {
            Maze3d maze3d = mg.generate(1, 5, 5);
            maze3d.print3DMaze();
            OutputStream out = new MyCompressorOutputStream(
                    new FileOutputStream("1.maz"));
            out.write(maze3d.toByteArray());
            byte[] arr = maze3d.toByteArray();
            System.out.println("");
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + ",");
            }
            out.close();
            InputStream in = new MyDecompressorInputStream(new FileInputStream(
                    "1.maz"));
            byte b[] = new byte[maze3d.toByteArray().length];
            in.read(b);
            in.close();
            Maze3d loaded = new Maze3d(b);
            System.out.println(loaded.equals(maze3d));

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

当我使用该方法时,如何使用参数: read(b);?

代码语言:javascript
复制
public class MyDecompressorInputStream extends InputStream {

    InputStream in;
    int count;
    boolean even = false;

    public MyDecompressorInputStream(InputStream in) {
        super();
        this.in = in;
    }

    @Override
    public int read() throws IOException {

        return 100;

    }



}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-07 16:24:46

您需要子类InputStream有什么原因吗?主程序中的任何代码都不会利用实现中添加的任何代码。但是,您应该在实现中实现read(byte[])。这是一个在我的机器上工作的类似的实现。

代码语言:javascript
复制
class MyInputStream extends InputStream {
    InputStream in;
    int count;
    boolean even = false;

    public MyInputStream(InputStream stream){
        this.in = stream;
    }

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

    @Override
    public int read(byte[] toStore) throws IOException {
        return this.in.read(toStore);
    }
}

我的主要用途也是这样:

代码语言:javascript
复制
public static void main(String[] args){
    MyInputStream stream = new MyInputStream(new ByteArrayInputStream(new byte[] {0, 0, 1}));
    byte[] storage = new byte[3];
    try {
        stream.read(storage);
        for (int i = 0; i < storage.length; ++i){
            System.out.println(storage[i]); //0 0 1
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    stream.close()

}
票数 4
EN

Stack Overflow用户

发布于 2015-09-07 16:05:33

InputStream.read(byte[]) calls (MyDecompressorInputStream.)read()

此外,您可能已经将委托in

代码语言:javascript
复制
@Override
public int read() throws IOException {
    return in.read();
}

@Override
public void close() throws IOException {
    return in.close();
}

您可能想要扩展FilterInputStream

一般来说,对于压缩,GzipInputStream是很好的(.gz格式)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32442435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档