Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中反转InputStream?_Java_File_Stream_Inputstream_Reverse - Fatal编程技术网

如何在Java中反转InputStream?

如何在Java中反转InputStream?,java,file,stream,inputstream,reverse,Java,File,Stream,Inputstream,Reverse,假设我有一个输入流,并且想要反转它 我有一个类似的问题,但首先那是8岁左右,而且这也不是我想要的 假设我有一个输入流,如: FileInputStream in = new FileInputStream(new File("some_random_file.bin")); 注意这不是专门针对文本文件,而是针对二进制文件 现在, 我有一种方法可以扭转这种局面: public static InputStream reverseStream(InputStream in) t

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

我有一个类似的问题,但首先那是8岁左右,而且这也不是我想要的

假设我有一个输入流,如:

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);
}
但我不确定这是最有效的方法


我想有一个有效的方法来实现这一点,即使是大文件

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

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);
}
如果文件太大,您不想一次加载所有文件,那么您将需要使用类以相反的顺序读取文件。您需要使用某种内部缓冲来避免糟糕的性能。您可以将其封装在自己的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;
        }
    }
}

请注意,如果您试图将读取器包装到这一点上,那么结果很可能是无意义的,除非底层文件的文本编码是每个字符一个字节。与DataInputStream等类似。

流就像它们更大的近亲河流一样,只朝一个方向流动。只有有一个足够大的水桶,当它们完全飞起来时,你才能把它们倒过来。@Kayaman我不知道要保留多大的水桶!大水桶装不下一杯水,而小水桶装不下更大的水尺!我正在寻找一个能同时完成这两项任务的高效水桶。我想说的一点是,你不能真正地逆流。所以你不能反向读取一个大文件,虽然你可以创建一个笨拙的反向输入流,但那将是愚蠢的。文件的读取方向是一个方向,从开始到结束,虽然你可以在一个文件中读取,但这不再是流。正如Kayaman诗意地证明的那样,普遍有效地反转任何给定的输入流实际上是不可能的。但在某些情况下,逆转特定流的特定解决方案是完全可能的。那么你能告诉我们基本目标是什么吗?这可能只是一场灾难。大地震可以逆转河流。也许猛烈地翻转硬盘可能会逆转这一趋势。