繁体   English   中英

如何反转 Java 中的 InputStream?

[英]How to reverse a InputStream in Java?

假设我有一个输入 stream 并且想要反转它!

我类似的问题如何以相反的顺序获取输入 stream 的内容? 但首先那是大约 8 岁,也不是我想要的!

假设我有一个 InputStream 像:

FileInputStream in = new FileInputStream(new File("some_random_file.bin"));

请注意,这不是专门针对文本文件,而是针对二进制文件!

现在,

我有办法扭转它:

public static InputStream reverseStream(InputStream in) throws Exception{
    byte bytes[] = in.readAllBytes();
    byte bytesRev[] = new byte[bytes.length];
    for(int i=bytes.length - 1, j = 0;i >= 0; i--, j++)
        bytesRev[i] = bytes[j];
    return new ByteArrayInputStream(bytesRev);
}

但我不确定这是最有效的方法!

即使对于大文件,我也希望有一种有效的方法来实现这一点!

如果您愿意将整个文件读入 memory,那么您的解决方案非常好。 memory 封装可以通过反转放置的内容而不是分配第二个数组来存储反转的内容来改进:

public static InputStream reverseStream(InputStream in) throws Exception{
    byte bytes[] = in.readAllBytes();
    for(int i=bytes.length - 1, j = 0;i >j; i--, j++) {
        byte tmp = bytes[i];
        bytes[i] = bytes[j];
        bytes[j] = tmp;
    }
    return new ByteArrayInputStream(bytes);
}

如果文件太大以至于您不想一次加载所有文件,则需要使用 class java.io.RandomAccessFile以相反的顺序读取文件。 您将需要使用某种内部缓冲来避免糟糕的性能。 您可以将其包装在您自己的InputStream实现中,该实现通过缓冲区向后读取,并根据需要动态加载新的缓冲区满。

这是我对执行此操作的 class 的尝试。 此代码完全未经测试(尽管它可以编译)。

/**
 * An input stream that reads a file in reverse. (UNTESTED)
 *
 * @author Ted Hopp
 */
class ReverseFileInputStream extends InputStream {
    private final RandomAccessFile file;
    /** Internal buffer for reading chunks of the file in reverse. */
    private final byte[] buffer;
    /** Position of the start of the buffer in the file. */
    private long bufferPosition;
    /** Position of the next byte to be read from the buffer. */
    private int bufferCursor;

    public ReverseFileInputStream(File file, int bufferSize) throws IOException {
        this.file = new RandomAccessFile(file, "r");
        buffer = new byte[bufferSize];
        bufferPosition = this.file.length();
        bufferCursor = -1;
    }

    @Override public int read() throws IOException {
        if (bufferCursor < 0) {
            fillBuffer();
        }
        return bufferCursor < 0 ? -1 : (buffer[bufferCursor--] & 0xff);
    }

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

    private void fillBuffer() throws IOException {
        if (bufferPosition > 0) {
            long newBufferPosition = Math.max(0L, bufferPosition - buffer.length);
            bufferCursor = (int) (bufferPosition - newBufferPosition);
            file.seek(newBufferPosition);
            file.readFully(buffer, 0, bufferCursor--);
            bufferPosition = newBufferPosition;
        }
    }
}

请注意,如果您尝试围绕此包装Reader ,则结果可能是无意义的,除非基础文件的文本编码是每个字符一个字节。 同样与DataInputStream等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM