Example usage for java.io InputStream equals

List of usage examples for java.io InputStream equals

Introduction

In this page you can find the example usage for java.io InputStream equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:de.micromata.genome.test.web.SimHttpServletRequest.java

protected void setInputStream(final InputStream servletIs) {
    this.servletIs = new ServletInputStream() {

        public int available() throws IOException {
            return servletIs.available();
        }/* ww w .j av a2s  .  c om*/

        public void close() throws IOException {
            servletIs.close();
        }

        public boolean equals(Object obj) {
            return servletIs.equals(obj);
        }

        public int hashCode() {
            return servletIs.hashCode();
        }

        public void mark(int readlimit) {
            servletIs.mark(readlimit);
        }

        public boolean markSupported() {
            return servletIs.markSupported();
        }

        public int read(byte[] b, int off, int len) throws IOException {
            return servletIs.read(b, off, len);
        }

        public int read(byte[] b) throws IOException {
            return servletIs.read(b);
        }

        public void reset() throws IOException {
            servletIs.reset();
        }

        public long skip(long n) throws IOException {
            return servletIs.skip(n);
        }

        public String toString() {
            return servletIs.toString();
        }

        @Override
        public int read() throws IOException {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:tokyo.northside.io.IOUtils2.java

/**
 * Compare the contents of two Streams to determine if they are equal or not.
 *
 * @param first  first input stream.//from   ww  w .j ava 2s . c o  m
 * @param second  second input stream.
 * @param off     compare from offset
 * @param len     comparison length
 * @return boolean true if content of input streams are equal, true if streams are equal,
 *     otherwise false.
 * @throws IOException when I/O error occurred.
 */
public static boolean contentEquals(final InputStream first, final InputStream second, final long off,
        final long len) throws IOException {
    boolean result;

    if (len < 1) {
        throw new IllegalArgumentException();
    }
    if (off < 0) {
        throw new IllegalArgumentException();
    }
    if (first.equals(second)) {
        return false;
    }

    try {
        byte[] firstBytes = new byte[BUF_LEN];
        byte[] secondBytes = new byte[BUF_LEN];

        if (off > 0) {
            long totalSkipped = 0;
            while (totalSkipped < off) {
                long skipped = first.skip(off - totalSkipped);
                if (skipped == 0) {
                    throw new IOException("Cannot seek offset bytes.");
                }
                totalSkipped += skipped;
            }
            totalSkipped = 0;
            while (totalSkipped < off) {
                long skipped = second.skip(off - totalSkipped);
                if (skipped == 0) {
                    throw new IOException("Cannot seek offset bytes.");
                }
                totalSkipped += skipped;
            }
        }

        long readLengthTotal = 0;
        result = true;
        while (readLengthTotal < len) {
            int readLength = BUF_LEN;
            if (len - readLengthTotal < (long) BUF_LEN) {
                readLength = (int) (len - readLengthTotal);
            }
            int lenFirst = first.read(firstBytes, 0, readLength);
            int lenSecond = second.read(secondBytes, 0, readLength);
            if (lenFirst != lenSecond) {
                result = false;
                break;
            }
            if ((lenFirst < 0) && (lenSecond < 0)) {
                result = true;
                break;
            }
            readLengthTotal += lenFirst;
            if (lenFirst < firstBytes.length) {
                byte[] a = Arrays.copyOfRange(firstBytes, 0, lenFirst);
                byte[] b = Arrays.copyOfRange(secondBytes, 0, lenSecond);
                if (!Arrays.equals(a, b)) {
                    result = false;
                    break;
                }
            } else if (!Arrays.equals(firstBytes, secondBytes)) {
                result = false;
                break;
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (IOException ioe) {
        throw ioe;
    }
    return result;
}