Java tutorial
//package com.java2s; /* * Copyright 2011 madvertise Mobile Advertising GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; public class Main { /** * Compares two streams * * @param inputStream1 * @param inputStream2 * @return true, if streams are the same * @throws IOException */ protected static boolean compareTwoStreams(InputStream inputStream1, InputStream inputStream2) throws IOException { boolean error = false; try { byte[] buffer1 = new byte[1024]; byte[] buffer2 = new byte[1024]; try { int numRead1 = 0; int numRead2 = 0; while (true) { numRead1 = inputStream1.read(buffer1); numRead2 = inputStream2.read(buffer2); if (numRead1 > -1) { if (numRead2 != numRead1) return false; if (!Arrays.equals(buffer1, buffer2)) return false; } else { return numRead2 < 0; } } } finally { inputStream1.close(); } } catch (IOException e) { error = true; throw e; } catch (RuntimeException e) { error = true; throw e; } finally { try { inputStream2.close(); } catch (IOException e) { if (!error) throw e; } } } }