@@ -134,7 +134,7 @@ private[blake3] class HasherImpl(
134134 this
135135 }
136136
137- def update (input : InputStream ): Hasher = synchronized {
137+ def update (input : InputStream , len : Int ): Hasher = synchronized {
138138 val bytes = new Array [Byte ](CHUNK_LEN )
139139
140140 var consume = chunkState.len() match {
@@ -145,24 +145,28 @@ private[blake3] class HasherImpl(
145145 }
146146
147147 var read = input.read(bytes, 0 , consume)
148- while (read > 0 ) {
148+ var remaining = len - read
149+ while (remaining > 0 && read >= 0 ) {
149150 val len = finalizeWhenCompleted()
150151 chunkState.update(bytes, 0 , read)
151152 consume = CHUNK_LEN - len
152153 read = input.read(bytes, 0 , consume)
154+ remaining -= read
153155 }
154156
155157 this
156158 }
157159
158- def update (input : ByteBuffer ): Hasher = synchronized {
160+ def update (input : ByteBuffer , len : Int ): Hasher = synchronized {
159161 val bytes = new Array [Byte ](CHUNK_LEN )
160162
161- while (input.hasRemaining) {
162- val len = finalizeWhenCompleted()
163- val consume = Math .min(CHUNK_LEN - len, input.remaining())
163+ var remaining = len
164+ while (remaining > 0 && input.hasRemaining) {
165+ val chunkLen = finalizeWhenCompleted()
166+ val consume = Math .min(CHUNK_LEN - chunkLen, remaining)
164167 input.get(bytes, 0 , consume)
165168 chunkState.update(bytes, 0 , consume)
169+ remaining -= consume
166170 }
167171
168172 this
@@ -216,6 +220,6 @@ private[blake3] class HasherImpl(
216220 def done (out : OutputStream , len : Int ): Unit =
217221 getOutput.rootBytes(out, len)
218222
219- def done (out : ByteBuffer ): Unit =
220- getOutput.rootBytes(out)
223+ def done (out : ByteBuffer , len : Int ): Unit =
224+ getOutput.rootBytes(out, len )
221225}
0 commit comments