Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DigestInputStream utils #110

Merged
merged 2 commits into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions core/src/main/scala/better/files/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.net.{URI, URL}
import java.nio.charset.Charset
import java.nio.channels._
import java.nio.file._
import attribute._
import java.nio.file.attribute._
import java.security.{DigestInputStream, MessageDigest}
import java.time.Instant
import java.util.zip._
Expand Down Expand Up @@ -319,6 +319,12 @@ class File private(val path: Path) {
def inputStream(implicit openOptions: File.OpenOptions = File.OpenOptions.default): ManagedResource[InputStream] =
newInputStream(openOptions).autoClosed

def newDigestInputStream(digest: MessageDigest)(implicit openOptions: File.OpenOptions = File.OpenOptions.default): DigestInputStream =
new DigestInputStream(newInputStream(openOptions), digest)

def digestInputStream(digest: MessageDigest)(implicit openOptions: File.OpenOptions = File.OpenOptions.default): ManagedResource[DigestInputStream] =
newDigestInputStream(digest)(openOptions).autoClosed

def newScanner(implicit config: Scanner.Config = Scanner.Config.default): Scanner =
Scanner(newBufferedReader(config.charset))(config)

Expand Down Expand Up @@ -360,21 +366,13 @@ class File private(val path: Path) {
this
}

def digest(algorithmName: String): Array[Byte] = {
val algorithm = MessageDigest.getInstance(algorithmName)
def digest(algorithm: MessageDigest): Array[Byte] = {
listRelativePaths.toSeq.sorted foreach { relativePath =>
val file: File = path.resolve(relativePath)

if(file.isDirectory) {
algorithm.update(relativePath.toString.getBytes)
} else {
val buffer = new Array[Byte](8192)
val inputStream = new DigestInputStream(file.newInputStream, algorithm)
try {
while (inputStream.read(buffer) != -1) {}
} finally {
inputStream.close()
}
file.digestInputStream(algorithm).foreach(_.consume())
}
}
algorithm.digest()
Expand All @@ -396,7 +394,7 @@ class File private(val path: Path) {
/**
* @return checksum of this file (or directory) in hex format
*/
def checksum(algorithm: String): String =
def checksum(algorithm: MessageDigest): String =
DatatypeConverter.printHexBinary(digest(algorithm))

def md5: String =
Expand Down
16 changes: 14 additions & 2 deletions core/src/main/scala/better/files/Implicits.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package better.files

import java.io.{File => JFile, _}, StreamTokenizer.{TT_EOF => eof}
import java.io.{File => JFile, _}
import StreamTokenizer.{TT_EOF => eof}
import java.nio.MappedByteBuffer
import java.nio.channels.FileChannel
import java.nio.charset.Charset
import java.nio.file.Path
import java.security.{DigestInputStream, MessageDigest}
import java.util.StringTokenizer
import java.util.stream.{Stream => JStream}
import java.util.zip.{Deflater, GZIPInputStream, ZipEntry, ZipOutputStream, GZIPOutputStream}
import java.util.zip.{Deflater, GZIPInputStream, GZIPOutputStream, ZipEntry, ZipOutputStream}

import scala.annotation.tailrec
import scala.util.control.NonFatal
Expand Down Expand Up @@ -38,6 +40,13 @@ trait Implicits {
File(file.getPath)
}

implicit class DigestInputStreamsOps(in: DigestInputStream) {
def consume(bufferSize: Int = 1<<10): Unit = {
val buffer = Array.ofDim[Byte](bufferSize)
while(in.read(buffer) != -1) {}
}
}

implicit class InputStreamOps(in: InputStream) {
def >(out: OutputStream): Unit =
pipeTo(out)
Expand Down Expand Up @@ -262,6 +271,9 @@ trait Implicits {
}
}

implicit def stringToMessageDigest(algorithmName: String): MessageDigest =
MessageDigest.getInstance(algorithmName)

implicit def tokenizerToIterator(s: StringTokenizer): Iterator[String] =
produce(s.nextToken()).till(s.hasMoreTokens)

Expand Down
4 changes: 0 additions & 4 deletions core/src/test/scala/better/files/FileSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package better.files

import java.nio.charset.StandardCharsets

import File.{root, home}
import Dsl._

Expand Down Expand Up @@ -296,7 +294,6 @@ class FileSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
}

it should "support hashing algos" in {
implicit val charset = StandardCharsets.UTF_8
t1.writeText("")
assert(md5(t1) == "D41D8CD98F00B204E9800998ECF8427E")
assert(sha1(t1) == "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709")
Expand All @@ -305,7 +302,6 @@ class FileSpec extends FlatSpec with BeforeAndAfterEach with Matchers {
}

it should "compute correct checksum for non-zero length string" in {
implicit val charset = StandardCharsets.UTF_8
t1.writeText("test")
assert(md5(t1) == "098F6BCD4621D373CADE4E832627B4F6")
assert(sha1(t1) == "A94A8FE5CCB19BA61C4C0873D391E987982FBBD3")
Expand Down