i need a Buffer class for socket send/recv : 1.memmove/memcpy as less as possible 2.object creation as less as possible 3.the class user need no worry for the extend issue is there anything liks that in the standard library ? or how can i make one ?
i wrote this but i can't use it for socket, because the buffer(from the standard library) is read-only:
import array
class Buffer :
def __init__( self, len ) :
self.__array= array.array( 'c', '\0'*len )
self.__databegin= 0
self.__datalen= 0
def getFreespace( self, minlen ) :
rightSpace= self.__getRightSpace()
if rightSpace < minlen :
totalSpace= self.__databegin + rightSpace
if totalSpace < minlen :
dlen= minlen - totalSpace
self.__array.extend( '\0'*dlen )
self.__array[ 0:self.__datalen ]= self.__array[ self.__databegin:self.__datalen+self.__databegin ]
return buffer( self.__array, self.__databegin + self.__datalen, self.__getRightSpace() )
def pushDataLen( self, len ) :
self.__datalen += len
def getDataSpace( self ) :
return buffer( self.__array, self.__databegin, self.__datalen )
def popDataLen( self, len ) :
len= min( len, self.__datalen )
self.__databegin += len
self.__datalen -= len
if self.__datalen == 0 :
self.__databegin= 0
def __getRightSpace( self ) :
return len( self.__array ) - self.__databegin - self.__datalen