【问题标题】:How to reverse a InputStream in Java?如何在 Java 中反转 InputStream?
【发布时间】:2021-07-22 04:12:04
【问题描述】:

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

我有类似的问题How to get the content of an input stream in reverse order? 但首先那是大约 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);
}

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

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

【问题讨论】:

  • 溪流,就像它们更大的表亲河流一样,只向一个方向流动。只有有足够大的桶才能将它们翻转(一旦它们完全飞行)。
  • @Kayaman 我不知道要保留多大的桶!一个大水桶不适合一杯水,而一个小水桶则不适合更大的规模!我正在为这两项任务寻找一个高效的存储桶
  • 你不能真正地​​ 反向流是我试图提出的观点。所以你不能反向读取一个大文件,虽然你可以创建一个(笨拙的)ReverseFileInputStream,但这很愚蠢。文件是从一个方向读取的,从头到尾(虽然你可以在文件中seek,但这不再是流式传输)。
  • 普遍有效地逆转任何给定的InputStream实际上是不可能的(正如Kayaman诗意地证明的那样)。但在某些情况下,完全有可能采用特定 解决方案来反转特定流的流动。那么你能告诉我们潜在的目标是什么吗?这可能只是一个XY Problem。
  • 大地震可以逆转河流。也许猛烈地翻转 HDD 可能会使流反转。

标签: java file stream inputstream reverse


【解决方案1】:

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

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

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

这是我在这门课上的尝试。这段代码完全未经测试(尽管它可以编译)。

/**
 * 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 等也是如此。

【讨论】:

    猜你喜欢
    • 2020-08-13
    • 2021-04-08
    • 2019-11-04
    • 2011-12-26
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多