aboutsummaryrefslogtreecommitdiffstats
path: root/FuseArchive/ChunkBuffer.py
blob: 7b6beeec573af9be2159996fb7f0877af060fd1b (plain)
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
import logging

# Handle efficient operations on a non-fixed length buffer like appending,
# replacing, reading chunks, etc
class ChunkBuffer:
    def __init__( self, data = '' ):
        logging.debug( "Creating chunkbuffer: %s" % data )
        self.chunk = [ data ]

    def append( self, s ):
        self.chunk.append( s )

    def replace( self, buf, start, end ):
        # We make a string of our chunk, then create a new list of the 3
        # strings, the start, new chunk, and remaining chunk
        s = ''.join( self.chunk )
        l = end - start
        self.chunk = [ s[ :start ], buf[ :l ], s[ end: ] ]

    def length( self ):
        if len( self.chunk ) > 5:
            self._simplify()

        l = 0;
        for c in self.chunk:
            l += len( c )

        return l

    def _simplify( self ):
        logging.debug( "Simplify!" )
        self.chunk = [ ''.join( self.chunk ) ]

    def string(self):
        logging.debug( "Stringifying: %s" % self.chunk )
        return ''.join( self.chunk )