ayInputStream到FileInputStream而不将

ayInputStream到FileInputStream而不将

本文介绍了ByteArrayInputStream到FileInputStream而不将文件写入硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ByteArrayInputStream 从数据库中读取了一个 byte [] 数组中的文件.

I read a file from the database that is in a byte[] array, using the ByteArrayInputStream.

InputStream input = new ByteArrayInputStream(lc.getTable());

要进一步处理文件,我需要一个 FileInputStream .但是我不想先将文件保存在硬盘上以再次读取.

For a further processing of the file I need a FileInputStream. But I don't want to save the file on the hard disk first to read it again.

可以做到这一点还是必须先将文件写出?

Can this be done or does the file have to be written out first?

推荐答案

不是.它只需要一个InputStream即可.可以使用 Files 类创建一个临时的退出时删除文件.可以创建一个输入输出 pipe ,该输入涉及一个额外的线程和一个物理文件.可以使用 RAM磁盘上的文件.

Not really. Then the further processing is overspecified; it should only require an InputStream. One could create a temporary deleted-on-exit file using the Files class. One could create an input output pipe involving an extra thread and still a physical file. One could use a File on a RAM disk.

但是,我们可以创建自己的FileInputStream,将其重定向/委托到您的ByteArrayInputStream:

However we can create our own FileInputStream, that redirects/delegates to your ByteArrayInputStream:

public class FakeFileInputStream extends FileInputStream {
    private final InputStream sourceInput;

    //public FakeFileInputStream(String name, InputStream sourceInput)
    //        throws FileNotFoundException {
    //    super(name);
    //    this.sourceInput = sourceInput;
    //}

    // @Torben offered this solution, which does not need an existing file.
    public FakeFileInputStream(InputStream sourceInput) {
        super(new FileDescriptor());
        this.sourceInput = sourceInput;
    }


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

    @Override
    public int read(byte[] b) throws IOException {
        return sourceInput.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return sourceInput.read(b, off, len);
    }

    @Override
    public long skip(long n) throws IOException {
        return sourceInput.skip(n);
    }

    @Override
    public int available() throws IOException {
        return sourceInput.available();
    }

    @Override
    public void close() throws IOException {
        sourceInput.close();
        //super.close();
    }

    @Override
    public FileChannel getChannel() {
        throw new UnsupportedOperationException();
    }

    @Override
    public synchronized void mark(int readlimit) {
        sourceInput.mark(readlimit);
    }

    @Override
    public synchronized void reset() throws IOException {
        sourceInput.reset();
    }

    @Override
    public boolean markSupported() {
        return sourceInput.markSupported();
    }
}

旧注:注释不清的构造函数将打开传递的文件 name ,最后关闭该文件,但不执行任何操作.您可以传递任何现有文件.

Old Note: the out-commented constructor would open the passed file name, and at the end close it, but do nothing with it. You could pass any existing file.

注意:感谢@Torben使用空FileDescriptor的版本.

(要创建这样的包装器/委托类:如果IDE不提供委托类的生成,则只需重写所有方法,然后将"super."替换为"delegate".)

(To create such a wrapper/delegating class: if the IDE offers no generation of a delegating class, simply override all methods, and replace "super." with "delegate.".)

这篇关于ByteArrayInputStream到FileInputStream而不将文件写入硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:03