Skip to content

Commit b56c63b

Browse files
committed
Limited read and write to ByteBuffer.
1 parent 92de8f1 commit b56c63b

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## [unreleased]
66
- Introduced support direct allocated ByteBuffer.
77
- Cross build for scala-2.10.7.
8+
- Limited read and write to ByteBuffer.
89

910
## [2.4.1] - 2021-02-19
1011
- Fixed regression on RFC4648 encoding introduced in 2.4.0.

shared/src/main/scala/ky/korins/blake3/Hasher.scala

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,31 @@ trait Hasher {
4949
*/
5050
def update(input: String): Hasher
5151

52+
/**
53+
* Updates a hasher from specified InputStream, returns the same hasher
54+
*
55+
* It reads `input` until it returns `-1` or it reads len bytes
56+
*/
57+
def update(input: InputStream, len: Int): Hasher
58+
5259
/**
5360
* Updates a hasher from specified InputStream, returns the same hasher
5461
*
5562
* It reads `input` until it returns `-1`
5663
*/
57-
def update(input: InputStream): Hasher
64+
def update(input: InputStream): Hasher =
65+
update(input, Int.MaxValue)
66+
67+
/**
68+
* Updates a hasher from specified ByteBuffer with no more than len bytes, returns the same hasher
69+
*/
70+
def update(input: ByteBuffer, len: Int): Hasher
5871

5972
/**
60-
* Updates a hasher from specified ByteBuffer, returns the same hasher
73+
* Updates a hasher from specified ByteBuffer until the end, returns the same hasher
6174
*/
62-
def update(input: ByteBuffer): Hasher
75+
def update(input: ByteBuffer): Hasher =
76+
update(input, input.remaining())
6377

6478
/**
6579
* Calculate a hash into specified byte array
@@ -91,9 +105,15 @@ trait Hasher {
91105
def done(out: OutputStream, len: Int): Unit
92106

93107
/**
94-
* Calculate a hash into specified OutputStream with specified output length in bytes
108+
* Calculate a hash into specified ByteBuffer with specified output length in bytes
109+
*/
110+
def done(out: ByteBuffer, len: Int): Unit
111+
112+
/**
113+
* Calculate a hash into specified ByteBuffer until the end of ByteBuffer
95114
*/
96-
def done(out: ByteBuffer): Unit
115+
def done(out: ByteBuffer): Unit =
116+
done(out, out.remaining())
97117

98118
/**
99119
* Calculate a hash as single short

shared/src/main/scala/ky/korins/blake3/HasherImpl.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

shared/src/main/scala/ky/korins/blake3/Output.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,9 @@ private[blake3] class Output(
172172
}
173173
}
174174

175-
def rootBytes(out: ByteBuffer): Unit = {
175+
def rootBytes(out: ByteBuffer, len: Int): Unit = {
176176
var outputBlockCounter = 0
177177
var pos = 0
178-
val len = out.remaining()
179178

180179
val blockLenWords = BLOCK_LEN_WORDS
181180
val words = new Array[Int](blockLenWords)

0 commit comments

Comments
 (0)