deepdiff.unitprocessor.BinaryCompareDiffUnitProcessor Maven / Gradle / Ivy
/*
* Copyright 2011 DeepDiff Contributors
*
* 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.
*/
package deepdiff.unitprocessor;
import java.io.IOException;
import java.io.InputStream;
import org.apache.log4j.Logger;
import deepdiff.core.DiffPoint;
import deepdiff.core.DiffPointProcessor;
import deepdiff.core.DiffScope;
import deepdiff.core.DiffUnit;
import deepdiff.core.DiffUnitProcessor;
/**
* A DiffUnitProcessor that performs binary file comparisons on DiffUnits.
*/
public class BinaryCompareDiffUnitProcessor implements DiffUnitProcessor {
private static final Logger log = Logger.getLogger(BinaryCompareDiffUnitProcessor.class);
/**
* Performs a binary file comparison on diffUnit
, and passes any {@link DiffPoint}s
* found to processor
*
* @param diffUnit the unit to process
* @param processor the processor to pass differences to
*
* @return null (no child scopes needed)
*/
public DiffScope processDiffUnit(DiffUnit diffUnit, DiffPointProcessor processor) {
InputStream is1 = null;
InputStream is2 = null;
try {
is1 = diffUnit.getLeftInputStream();
is2 = diffUnit.getRightInputStream();
int b1 = is1.read();
int b2 = is2.read();
while (b1 != -1 && b2 != -1) {
if (b1 != b2) {
DiffPoint diffPoint = new DiffPoint(diffUnit, "Binary file difference");
processor.processDiffPoint(diffPoint);
break;
}
b1 = is1.read();
b2 = is2.read();
}
if (b1 != b2) {
DiffPoint diffPoint = new DiffPoint(diffUnit, "Binary file difference");
processor.processDiffPoint(diffPoint);
}
} catch (IOException ioe) {
log.error("Failure performing binary comparison on " + diffUnit.getScopedPath(), ioe);
} finally {
if (is1 != null) {
try {
is1.close();
} catch (Exception ex) {
// Ignore exception on close
}
}
if (is2 != null) {
try {
is2.close();
} catch (Exception ex) {
// Ignore exception on close
}
}
}
return null; // No child scopes needed
}
}