@@ -386,12 +386,12 @@ function fillbuffer(stream::TranscodingStream)
386
386
# reset
387
387
stream. state. code = startproc (stream. codec, :read , stream. state. error)
388
388
if stream. state. code == :error
389
- handle_error (stream)
389
+ changestate! (stream, :panic )
390
390
end
391
391
end
392
392
makemargin! (buffer2, 1 )
393
393
readdata! (stream. stream, buffer2)
394
- _, Δout = call_process (stream. codec, stream . state , buffer2, buffer1)
394
+ _, Δout = call_process (stream, buffer2, buffer1)
395
395
nfilled += Δout
396
396
end
397
397
return nfilled
@@ -431,25 +431,26 @@ function process_to_write(stream::TranscodingStream)
431
431
# reset
432
432
stream. state. code = startproc (stream. codec, :write , stream. state. error)
433
433
if stream. state. code == :error
434
- handle_error (stream)
434
+ changestate! (stream, :panic )
435
435
end
436
436
end
437
437
buffer2 = stream. state. buffer2
438
438
writebuffer! (stream. stream, buffer2)
439
- Δin, _ = call_process (stream. codec, stream . state , buffer1, buffer2)
439
+ Δin, _ = call_process (stream, buffer1, buffer2)
440
440
makemargin! (buffer1, 0 )
441
441
return Δin
442
442
end
443
443
444
- function call_process (codec:: Codec , state:: State , inbuf:: Buffer , outbuf:: Buffer )
444
+ function call_process (stream:: TranscodingStream , inbuf:: Buffer , outbuf:: Buffer )
445
+ state = stream. state
445
446
input = buffermem (inbuf)
446
- makemargin! (outbuf, minoutsize (codec, input))
447
- Δin, Δout, state. code = process (codec, input, marginmem (outbuf), state. error)
447
+ makemargin! (outbuf, minoutsize (stream . codec, input))
448
+ Δin, Δout, state. code = process (stream . codec, input, marginmem (outbuf), state. error)
448
449
inbuf. bufferpos += Δin
449
450
outbuf. marginpos += Δout
450
451
outbuf. total += Δout
451
452
if state. code == :error
452
- handle_error (codec, state )
453
+ changestate! (stream, :panic )
453
454
elseif state. code == :ok && Δin == Δout == 0
454
455
# When no progress, expand the output buffer.
455
456
makemargin! (outbuf, max (16 , marginsize (outbuf) * 2 ))
@@ -471,16 +472,25 @@ function changestate!(stream::TranscodingStream, newstate::Symbol)
471
472
if state == newstate
472
473
# state does not change
473
474
return
475
+ elseif newstate == :panic
476
+ if ! haserror (stream. state. error)
477
+ # set a default error
478
+ stream. state. error[] = ErrorException (" unknown error happened while processing data" )
479
+ end
480
+ stream. state. state = newstate
481
+ finalize_codec (stream. codec, stream. state. error)
482
+ throw (stream. state. error[])
474
483
elseif state == :idle
475
484
if newstate == :read || newstate == :write
476
485
stream. state. code = startproc (stream. codec, newstate, stream. state. error)
477
486
if stream. state. code == :error
478
- handle_error (stream)
487
+ changestate! (stream, :panic )
479
488
end
480
489
stream. state. state = newstate
481
490
return
482
491
elseif newstate == :close
483
- finalize_codec (stream, :close )
492
+ stream. state. state = newstate
493
+ finalize_codec (stream. codec, stream. state. error)
484
494
return
485
495
end
486
496
elseif state == :read
@@ -530,41 +540,15 @@ function throw_panic_error()
530
540
throw (ArgumentError (" stream is in unrecoverable error; only isopen and close are callable" ))
531
541
end
532
542
533
-
534
- # Error Handler
535
- # -------------
536
-
537
- # Handle an error happened while transcoding data.
538
- function handle_error (stream:: TranscodingStream )
539
- handle_error (stream. codec, stream. state)
540
- end
541
-
542
- function handle_error (codec:: Codec , state:: State )
543
- if ! haserror (state. error)
544
- # set a generic error
545
- state. error[] = ErrorException (" unknown error happened while processing data" )
546
- end
547
- finalize_codec (codec, state, :panic )
548
- throw (state. error[])
549
- end
550
-
551
543
# Call the finalize method of the codec.
552
- function finalize_codec (stream:: TranscodingStream , newstate:: Symbol )
553
- finalize_codec (stream. codec, stream. state, newstate)
554
- end
555
-
556
- function finalize_codec (codec:: Codec , state:: State , newstate:: Symbol )
557
- @assert newstate ∈ (:close , :panic )
544
+ function finalize_codec (codec:: Codec , error:: Error )
558
545
try
559
546
finalize (codec)
560
547
catch
561
- if state. state == :error && haserror (state. error)
562
- # throw an exception that happended before
563
- throw (state. error[])
548
+ if haserror (error)
549
+ throw (error[])
564
550
else
565
551
rethrow ()
566
552
end
567
- finally
568
- state. state = newstate
569
553
end
570
554
end
0 commit comments