-
Notifications
You must be signed in to change notification settings - Fork 593
/
StreamDecoder.scala
293 lines (262 loc) · 12.5 KB
/
StreamDecoder.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2013 Functional Streams for Scala
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Adapted from scodec-protocols, licensed under 3-clause BSD
package fs2
package interop
package scodec
import _root_.scodec.{Attempt, DecodeResult, Decoder, Err, codecs}
import _root_.scodec.bits.BitVector
import cats.MonadThrow
/** Supports binary decoding of a stream that emits elements as they are decoded.
*
* The main purpose of using a `StreamDecoder` over a `scodec.Decoder` is mixing
* decoding with processing. For example, `scodec.codecs.vector(decoderA): Decoder[Vector[A]]`
* could be used to decode a bit stream but the decoded `Vector[A]` would not be
* emitted until the end of the bit stream. With `StreamDecoder.many(decoderA): StreamDecoder[A]`,
* each decoded `A` value is emitted as soon as it is decoded.
*
* The `StreamDecoder` companion has various constructors -- most importantly, `once`
* and `many`, that allow a `Decoder[A]` to be lifted to a `StreamDecoder[A]`.
*
* Given a `StreamDecoder[A]`, a bit stream can be decoded via the `decode` method or
* by calling a variant of `toPipe`.
*/
final class StreamDecoder[+A] private (private val step: StreamDecoder.Step[A]) { self =>
import StreamDecoder._
/** Converts this decoder to a `Pipe[F, BitVector, A]`. */
def toPipe[F[_]: RaiseThrowable]: Pipe[F, BitVector, A] = decode(_)
/** Converts this decoder to a `Pipe[F, Byte, A]`. */
def toPipeByte[F[_]: RaiseThrowable]: Pipe[F, Byte, A] =
in => in.chunks.map(_.toBitVector).through(toPipe)
/** Returns a `Stream[F, A]` given a `Stream[F, BitVector]`. */
def decode[F[_]: RaiseThrowable](s: Stream[F, BitVector]): Stream[F, A] =
apply(s).void.stream
/** Returns a `Pull[F, A, Option[Stream[F, BitVector]]]` given a `Stream[F, BitVector]`.
* The result of the returned pull is the remainder of the input stream that was not used in decoding.
*/
def apply[F[_]: RaiseThrowable](
s: Stream[F, BitVector]
): Pull[F, A, Option[Stream[F, BitVector]]] =
step match {
case Empty => Pull.pure(Some(s))
case Result(a) => Pull.output1(a).as(Some(s))
case Failed(cause) => Pull.raiseError(cause)
case Append(x, y) =>
x(s).flatMap {
case None => Pull.pure(None)
case Some(rem) => y()(rem)
}
case Decode(decoder, once, failOnErr) =>
def loop(
carry: BitVector,
s: Stream[F, BitVector]
): Pull[F, A, Option[Stream[F, BitVector]]] =
s.pull.uncons1.flatMap {
case Some((hd, tl)) =>
val buffer = carry ++ hd
decoder(buffer) match {
case Attempt.Successful(DecodeResult(value, remainder)) =>
val next = if (remainder.isEmpty) tl else tl.cons1(remainder)
val p = value(next)
if (once) p
else
p.flatMap {
case Some(next) => loop(BitVector.empty, next)
case None => Pull.pure(None)
}
case Attempt.Failure(_: Err.InsufficientBits) =>
loop(buffer, tl)
case Attempt.Failure(comp: Err.Composite)
if comp.errs.exists(_.isInstanceOf[Err.InsufficientBits]) =>
loop(buffer, tl)
case Attempt.Failure(e) =>
if (failOnErr) Pull.raiseError(CodecError(e))
else Pull.pure(Some(tl.cons1(buffer)))
}
case None => if (carry.isEmpty) Pull.pure(None) else Pull.pure(Some(Stream(carry)))
}
loop(BitVector.empty, s)
case Isolate(bits, decoder) =>
def loop(
carry: BitVector,
s: Stream[F, BitVector]
): Pull[F, A, Option[Stream[F, BitVector]]] =
s.pull.uncons1.flatMap {
case Some((hd, tl)) =>
val (buffer, remainder) = (carry ++ hd).splitAt(bits)
if (buffer.size == bits)
decoder[F](Stream(buffer)) >> Pull.pure(Some(tl.cons1(remainder)))
else loop(buffer, tl)
case None => if (carry.isEmpty) Pull.pure(None) else Pull.pure(Some(Stream(carry)))
}
loop(BitVector.empty, s)
}
/** Creates a stream decoder that, upon decoding an `A`, applies it to the supplied function and decodes
* the next part of the input with the returned decoder. When that decoder finishes, the remainder of
* the input is returned to the original decoder for further decoding.
*/
def flatMap[B](f: A => StreamDecoder[B]): StreamDecoder[B] =
new StreamDecoder[B](
self.step match {
case Empty => Empty
case Result(a) => f(a).step
case Failed(cause) => Failed(cause)
case Decode(g, once, failOnErr) =>
Decode(in => g(in).map(_.map(_.flatMap(f))), once, failOnErr)
case Isolate(bits, decoder) => Isolate(bits, decoder.flatMap(f))
case Append(x, y) => Append(x.flatMap(f), () => y().flatMap(f))
}
)
def handleErrorWith[A2 >: A](f: Throwable => StreamDecoder[A2]): StreamDecoder[A2] =
new StreamDecoder[A2](
self.step match {
case Empty => Empty
case Result(a) => Result(a)
case Failed(cause) => f(cause).step
case Decode(g, once, failOnErr) =>
Decode(in => g(in).map(_.map(_.handleErrorWith(f))), once, failOnErr)
case Isolate(bits, decoder) => Isolate(bits, decoder.handleErrorWith(f))
case Append(x, y) => Append(x.handleErrorWith(f), () => y().handleErrorWith(f))
}
)
/** Maps the supplied function over each output of this decoder. */
def map[B](f: A => B): StreamDecoder[B] = flatMap(a => StreamDecoder.emit(f(a)))
/** Creates a stream decoder that first decodes until this decoder finishes and then decodes
* using the supplied decoder.
*
* Note: this should not be used to write recursive decoders (e.g., `def ints: StreamDecoder[A] = once(int32) ++ ints`)
* if each incremental decoding step can fail with `InsufficientBits`. Otherwise, it decoding can get stuck in
* an infinite loop, where the remaining bits are fed to the recursive call.
*/
def ++[A2 >: A](that: => StreamDecoder[A2]): StreamDecoder[A2] =
new StreamDecoder(Append(this, () => that))
/** Alias for `StreamDecoder.isolate(bits)(this)`. */
def isolate(bits: Long): StreamDecoder[A] = StreamDecoder.isolate(bits)(this)
/** Converts this stream decoder to a `Decoder[Vector[A]]`. */
def strict: Decoder[Vector[A]] =
new Decoder[Vector[A]] {
def decode(bits: BitVector): Attempt[DecodeResult[Vector[A]]] = {
type ET[X] = Either[Throwable, X]
self
.map(Left(_))
.apply[Fallible](Stream(bits))
.flatMap { remainder =>
remainder
.map { r =>
r.map(Right(_)).pull.echo
}
.getOrElse(Pull.done)
}
.stream
.compile[Fallible, ET, Either[A, BitVector]]
.fold((Vector.empty[A], BitVector.empty)) { case ((acc, rem), entry) =>
entry match {
case Left(a) => (acc :+ a, rem)
case Right(r2) => (acc, rem ++ r2)
}
}
.fold(
{
case CodecError(e) => Attempt.failure(e)
case other => Attempt.failure(Err.General(other.getMessage, Nil))
},
{ case (acc, rem) => Attempt.successful(DecodeResult(acc, rem)) }
)
}
}
}
object StreamDecoder {
private sealed trait Step[+A]
private case object Empty extends Step[Nothing]
private case class Result[A](value: A) extends Step[A]
private case class Failed(cause: Throwable) extends Step[Nothing]
private case class Decode[A](
f: BitVector => Attempt[DecodeResult[StreamDecoder[A]]],
once: Boolean,
failOnErr: Boolean
) extends Step[A]
private case class Isolate[A](bits: Long, decoder: StreamDecoder[A]) extends Step[A]
private case class Append[A](x: StreamDecoder[A], y: () => StreamDecoder[A]) extends Step[A]
/** Stream decoder that emits no elements. */
val empty: StreamDecoder[Nothing] = new StreamDecoder[Nothing](Empty)
/** Stream decoder that emits a single `A` and consumes no bits from the input. */
def emit[A](a: A): StreamDecoder[A] = new StreamDecoder[A](Result(a))
/** Stream decoder that emits the supplied `A` values and consumes no bits from the input. */
def emits[A](as: Iterable[A]): StreamDecoder[A] =
as.foldLeft(empty: StreamDecoder[A])((acc, a) => acc ++ emit(a))
/** Creates a stream decoder that decodes one `A` using the supplied decoder.
* Input bits are buffered until the decoder is able to decode an `A`.
*/
def once[A](decoder: Decoder[A]): StreamDecoder[A] =
new StreamDecoder[A](
Decode(in => decoder.decode(in).map(_.map(emit)), once = true, failOnErr = true)
)
/** Creates a stream decoder that repeatedly decodes `A` values using the supplied decoder.
*/
def many[A](decoder: Decoder[A]): StreamDecoder[A] =
new StreamDecoder[A](
Decode(in => decoder.decode(in).map(_.map(emit)), once = false, failOnErr = true)
)
/** Creates a stream decoder that attempts to decode one `A` using the supplied decoder.
* Input bits are buffered until the decoder is able to decode an `A`.
* If decoding fails, the bits are not consumed and the stream decoder yields no values.
*/
def tryOnce[A](decoder: Decoder[A]): StreamDecoder[A] =
new StreamDecoder[A](
Decode(in => decoder.decode(in).map(_.map(emit)), once = true, failOnErr = false)
)
/** Creates a stream decoder that repeatedly decodes `A` values until decoding fails.
* If decoding fails, the read bits are not consumed and the stream decoder terminates,
* having emitted any successfully decoded values earlier.
*/
def tryMany[A](decoder: Decoder[A]): StreamDecoder[A] =
new StreamDecoder[A](
Decode(in => decoder.decode(in).map(_.map(emit)), once = false, failOnErr = false)
)
/** Creates a stream decoder that fails decoding with the specified exception. */
def raiseError(cause: Throwable): StreamDecoder[Nothing] = new StreamDecoder(Failed(cause))
/** Creates a stream decoder that fails decoding with the specified error. */
def raiseError(err: Err): StreamDecoder[Nothing] = raiseError(CodecError(err))
/** Creates a stream decoder that reads the specified number of bits and then decodes
* them with the supplied stream decoder. Any remainder from the inner stream decoder is
* discarded.
*/
def isolate[A](bits: Long)(decoder: StreamDecoder[A]): StreamDecoder[A] =
new StreamDecoder(Isolate(bits, decoder))
/** Creates a stream decoder that ignores the specified number of bits. */
def ignore(bits: Long): StreamDecoder[Nothing] =
once(codecs.ignore(bits)).flatMap(_ => empty)
implicit val instance: MonadThrow[StreamDecoder] = new MonadThrow[StreamDecoder] {
def pure[A](a: A) = StreamDecoder.emit(a)
def flatMap[A, B](da: StreamDecoder[A])(f: A => StreamDecoder[B]) = da.flatMap(f)
def tailRecM[A, B](a: A)(f: A => StreamDecoder[Either[A, B]]): StreamDecoder[B] =
f(a).flatMap {
case Left(a) => tailRecM(a)(f)
case Right(b) => pure(b)
}
def handleErrorWith[A](da: StreamDecoder[A])(
f: Throwable => StreamDecoder[A]
): StreamDecoder[A] =
da.handleErrorWith(f)
def raiseError[A](e: Throwable): StreamDecoder[A] =
StreamDecoder.raiseError(e)
}
}