Created
October 23, 2014 12:13
-
-
Save UberMouse/89508b368d2e7c954497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStream | |
import java.io.FileInputStream | |
object Main { | |
implicit def streamToPimpedStream(stream: InputStream) = new PimpedInputStream(stream) | |
def main(args: Array[String]) { | |
def master = new FileInputStream("C:\\test1.txt") | |
val identical = new FileInputStream("C:\\test2.txt") | |
val modified = new FileInputStream("C:\\test3.txt") | |
println(master.isIdentical(identical) == true) | |
println(master.isIdentical(modified) == false) | |
} | |
} | |
class PimpedInputStream(stream: InputStream) { | |
val READ_SIZE = 65535 | |
def isIdentical(other: InputStream): Boolean = { | |
isIdenticalRecursive(stream, other) | |
} | |
private def isIdenticalRecursive(left: InputStream, right: InputStream): Boolean = { | |
val ourBytes = Array.ofDim[Byte](READ_SIZE) | |
val theirBytes = Array.ofDim[Byte](READ_SIZE) | |
val bytesRead = List(left.read(ourBytes), right.read(theirBytes)) | |
//Both streams have been fully read | |
if(bytesRead.forall(_ == -1)) | |
true | |
else if(!ourBytes.sameElements(theirBytes)) | |
false | |
else | |
isIdenticalRecursive(left, right) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment