比较输入流的快速方法

发布于 2024-10-04 11:31:59 字数 688 浏览 0 评论 0 原文

我有一个问题,我需要快速比较两个输入流。

今天我有一个这样的功能:

private boolean isEqual(InputStream i1, InputStream i2) throws IOException {

    try {
        // do the compare
        while (true) {
            int fr = i1.read();
            int tr = i2.read();

            if (fr != tr)
                return false;

            if (fr == -1)
                return true;
        }

    } finally {
        if (i1 != null)
            i1.close();
        if (i2 != null)
            i2.close();
    }
}

但是它真的很慢。我想使用缓冲读取,但还没有想出一个好的方法。

一些额外的东西使它变得更加困难:

  • 我不想将一个输入流读入内存(整个)
  • 我不想使用第三方库

我需要一个实用的解决方案 - 代码! :)

I have a problem, I need to compare two inputstreams fast.

Today I have a function like this:

private boolean isEqual(InputStream i1, InputStream i2) throws IOException {

    try {
        // do the compare
        while (true) {
            int fr = i1.read();
            int tr = i2.read();

            if (fr != tr)
                return false;

            if (fr == -1)
                return true;
        }

    } finally {
        if (i1 != null)
            i1.close();
        if (i2 != null)
            i2.close();
    }
}

But it's really slow. I want to use buffered reads but have not come up with a good way of doing it.

Some extra stuff that makes it harder:

  • I don't want to read one of the input streams into memory (the whole one)
  • I don't want to use a third party library

I need a practial solution - code! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄与心 2024-10-11 11:31:59

到目前为止,我最喜欢的是使用 org.apache.commons.io.IOUtils 帮助器类rel="noreferrer">Apache Commons IO 库

IOUtils.contentEquals( is1, is2 );

By far my favorite is to use the org.apache.commons.io.IOUtils helper class from the Apache Commons IO library:

IOUtils.contentEquals( is1, is2 );
终难遇 2024-10-11 11:31:59

像这样的事情可能会做:

private static boolean isEqual(InputStream i1, InputStream i2)
        throws IOException {

    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);

    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

    try {
        while (true) {

            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);

            if (n1 == -1 || n2 == -1) return n1 == n2;

            buf1.flip();
            buf2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++)
                if (buf1.get() != buf2.get())
                    return false;

            buf1.compact();
            buf2.compact();
        }

    } finally {
        if (i1 != null) i1.close();
        if (i2 != null) i2.close();
    }
}

Something like this may do:

private static boolean isEqual(InputStream i1, InputStream i2)
        throws IOException {

    ReadableByteChannel ch1 = Channels.newChannel(i1);
    ReadableByteChannel ch2 = Channels.newChannel(i2);

    ByteBuffer buf1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buf2 = ByteBuffer.allocateDirect(1024);

    try {
        while (true) {

            int n1 = ch1.read(buf1);
            int n2 = ch2.read(buf2);

            if (n1 == -1 || n2 == -1) return n1 == n2;

            buf1.flip();
            buf2.flip();

            for (int i = 0; i < Math.min(n1, n2); i++)
                if (buf1.get() != buf2.get())
                    return false;

            buf1.compact();
            buf2.compact();
        }

    } finally {
        if (i1 != null) i1.close();
        if (i2 != null) i2.close();
    }
}
比忠 2024-10-11 11:31:59

使用缓冲读取只需用 BufferedInputStreams 包装 InputStreams 即可。但是,一次读取大块可能会获得最佳性能。

private boolean isEqual(InputStream i1, InputStream i2) throws IOException {
    byte[] buf1 = new byte[64 *1024];
    byte[] buf2 = new byte[64 *1024];
    try {
        DataInputStream d2 = new DataInputStream(i2);
        int len;
        while ((len = i1.read(buf1)) > 0) {
            d2.readFully(buf2,0,len);
            for(int i=0;i<len;i++)
              if(buf1[i] != buf2[i]) return false;
        }
        return d2.read() < 0; // is the end of the second file also.
    } catch(EOFException ioe) {
        return false;
    } finally {
        i1.close();
        i2.close();
    }
}

Using buffered reads is just a matter of wrapping the InputStreams with BufferedInputStreams. However you are likely to get the best performance reading large blocks at a time.

private boolean isEqual(InputStream i1, InputStream i2) throws IOException {
    byte[] buf1 = new byte[64 *1024];
    byte[] buf2 = new byte[64 *1024];
    try {
        DataInputStream d2 = new DataInputStream(i2);
        int len;
        while ((len = i1.read(buf1)) > 0) {
            d2.readFully(buf2,0,len);
            for(int i=0;i<len;i++)
              if(buf1[i] != buf2[i]) return false;
        }
        return d2.read() < 0; // is the end of the second file also.
    } catch(EOFException ioe) {
        return false;
    } finally {
        i1.close();
        i2.close();
    }
}
夜深人未静 2024-10-11 11:31:59

为什么不简单地将两个流包装在方法的最开始处:

i1 = new BufferedInputStream(i1);
i2 = new BufferedInputStream(i2);

或者,您可以简单地尝试将两个流读入缓冲区:

public static boolean equals(InputStream i1, InputStream i2, int buf) throws IOException {
    try {
        // do the compare
        while (true) {
            byte[] b1 = new byte[buf];
            byte[] b2 = new byte[buf];

            int length = i1.read(b1);
            if (length == -1) {
                return i2.read(b2, 0, 1) == -1;
            }

            try {
                StreamUtils.readFully(i2, b2, 0, length);
            } catch (EOFException e) {
                // i2 is shorter than i1
                return false;
            }

            if (!ArrayUtils.equals(b1, b2, 0, length)) {
                return false;
            }
        }
    } finally {
        // simply close streams and ignore (log) exceptions
        StreamUtils.close(i1, i2);
    }
}

// StreamUtils.readFully(..) 
public static void readFully(InputStream in, byte[] b, int off, int len) throws EOFException, IOException {
    while (len > 0) {
        int read = in.read(b, off, len);
        if (read == -1) {
            throw new EOFException();
        }
        off += read;
        len -= read;
    }
}

// ArrayUtils.equals(..)
public static boolean equals(byte[] a, byte[] a2, int off, int len) {
    if (off < 0 || len < 0 || len > a.length - off || len > a2.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return true;
    }

    if (a == a2) {
        return true;
    }
    if (a == null || a2 == null) {
        return false;
    }

    for (int i = off; i < off + len; i++) {
        if (a[i] != a2[i]) {
            return false;
        }
    }

    return true;
}

编辑:我现在已经修复了我的实现。这就是没有 DataInputStream 或 NIO 的情况。代码可在 GitHubSonatype 的 OSS 快照存储库 Maven:

<dependency>
  <groupId>at.molindo</groupId>
  <artifactId>molindo-utils</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

why not simply wrap both streams at the very beginning of your method:

i1 = new BufferedInputStream(i1);
i2 = new BufferedInputStream(i2);

Alternatively, you could simply try reading both streams into a buffer:

public static boolean equals(InputStream i1, InputStream i2, int buf) throws IOException {
    try {
        // do the compare
        while (true) {
            byte[] b1 = new byte[buf];
            byte[] b2 = new byte[buf];

            int length = i1.read(b1);
            if (length == -1) {
                return i2.read(b2, 0, 1) == -1;
            }

            try {
                StreamUtils.readFully(i2, b2, 0, length);
            } catch (EOFException e) {
                // i2 is shorter than i1
                return false;
            }

            if (!ArrayUtils.equals(b1, b2, 0, length)) {
                return false;
            }
        }
    } finally {
        // simply close streams and ignore (log) exceptions
        StreamUtils.close(i1, i2);
    }
}

// StreamUtils.readFully(..) 
public static void readFully(InputStream in, byte[] b, int off, int len) throws EOFException, IOException {
    while (len > 0) {
        int read = in.read(b, off, len);
        if (read == -1) {
            throw new EOFException();
        }
        off += read;
        len -= read;
    }
}

// ArrayUtils.equals(..)
public static boolean equals(byte[] a, byte[] a2, int off, int len) {
    if (off < 0 || len < 0 || len > a.length - off || len > a2.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return true;
    }

    if (a == a2) {
        return true;
    }
    if (a == null || a2 == null) {
        return false;
    }

    for (int i = off; i < off + len; i++) {
        if (a[i] != a2[i]) {
            return false;
        }
    }

    return true;
}

EDIT: I've fixed my implementation now. That's how it looks like without DataInputStream or NIO. Code is available at GitHub or from Sonatype's OSS Snapshot Repository Maven:

<dependency>
  <groupId>at.molindo</groupId>
  <artifactId>molindo-utils</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文