Skip to content

How to clear an io.BytesIO buffer. #13708

Answered by wnelis
wnelis asked this question in ESP8266
Feb 19, 2024 · 1 comments · 8 replies
Discussion options

You must be logged in to vote

As a static pre-allocated bytearray seems to be the way to go, I'll try to use class StaticBuffer, shown below, as a replacement of class io.BytesIO. Class StaticBuffer is (for now) modeled after class io.BytesIO.

class StaticBufferException( Exception ):
  pass

# 0 <= next <= lngt <= size
class StaticBuffer():

  def __init__( self, size ):
    self._bfr= bytearray( size )
    self.size= size			# Allocated size
    self.lngt= 0			# Current length
    self.next= 0			# Ordinal to start next write

  def clear( self ):
    self.lngt= 0
    self.next= 0

  def flush( self ):
    pass

  def getvalue( self ):
    return bytes(self._bfr[:self.lngt])

  def seek( self, ordinal ):
    if ordina…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@jimmo
Comment options

@wnelis
Comment options

@jimmo
Comment options

@wnelis
Comment options

Answer selected by wnelis
@bixb922
Comment options

@wnelis
Comment options

@bixb922
Comment options

@wnelis
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants