ByteArrayInputStream to FileInputStream without writing the file to hard drive

╄→гoц情女王★ 提交于 2021-01-29 10:40:09

问题


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

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

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?


回答1:


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.

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

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.

Note: thanks to @Torben for a version using an empty FileDescriptor.

(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.".)



来源:https://stackoverflow.com/questions/58950910/bytearrayinputstream-to-fileinputstream-without-writing-the-file-to-hard-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!