bytewriter.go 937 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2014-2022 Ulrich Kunitz. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package lzma
  5. import (
  6. "errors"
  7. "io"
  8. )
  9. // ErrLimit indicates that the limit of the LimitedByteWriter has been
  10. // reached.
  11. var ErrLimit = errors.New("limit reached")
  12. // LimitedByteWriter provides a byte writer that can be written until a
  13. // limit is reached. The field N provides the number of remaining
  14. // bytes.
  15. type LimitedByteWriter struct {
  16. BW io.ByteWriter
  17. N int64
  18. }
  19. // WriteByte writes a single byte to the limited byte writer. It returns
  20. // ErrLimit if the limit has been reached. If the byte is successfully
  21. // written the field N of the LimitedByteWriter will be decremented by
  22. // one.
  23. func (l *LimitedByteWriter) WriteByte(c byte) error {
  24. if l.N <= 0 {
  25. return ErrLimit
  26. }
  27. if err := l.BW.WriteByte(c); err != nil {
  28. return err
  29. }
  30. l.N--
  31. return nil
  32. }