## @package ByteArray # Byte wise structures utilities # # ----------------------------------------------------------------------------------- # # Copyright (c) 2009 Neobotix (www.neobotix.de) # # This software is allowed to be used and modified only in association with a Neobotix # robot platform. It is allowed to include the software into applications and # to distribute it with a Neobotix robot platform. # # This software is provided WITHOUT ANY WARRANTY; without even the # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the Neobotix License (Version 1.0) for more details. # # You should have received a copy of the Neobotix License # along with this software; if not, write to the # Gesellschaft fuer Produktionssysteme, Neobotix, Nobelstrasse 12, 70569 Stuttgart, Germany # # ----------------------------------------------------------------------------------- from array import array import struct class ByteArray(array): ''' Extended array.array class, with read and write methods. Acting like a buffer for reading, with a incremented index, for message conversion. ''' def __new__(cls, initialContent = ''): return super(ByteArray, cls).__new__(cls, 'B', initialContent) def __init__(self, initialContent = ''): ''' Constructor ''' self.readIndex = 0 def readInt(self): int = self.peekInt() if int is not None: self.readIndex += 4 return int def peekInt(self): if self.readIndex + 4 <= len(self): string = self[self.readIndex:self.readIndex+4] tmp = struct.unpack('!i', string) return tmp[0] else: return None def readDouble(self): double = self.peekDouble() if double is not None: self.readIndex += 8 return double def peekDouble(self): if self.readIndex + 8 <= len(self): string = self[self.readIndex:self.readIndex+8] tmp = struct.unpack('!d', string) #Double with big endian return tmp[0] else: return None def readByte(self): byte = self.peekByte() if byte is not None: self.readIndex += 1 return byte def peekByte(self): if self.readIndex <= len(self): byte = self[self.readIndex] return byte else: return None def readShortInt(self): int = self.peekShortInt() if int is not None: self.readIndex += 2 return int def readBoolean(self): byte = self.peekBoolean() if byte is not None: self.readIndex += 1 return byte def peekBoolean(self): if self.readIndex <= len(self): byte = self[self.readIndex] if byte: return True else: return False else: return None def peekShortInt(self): if self.readIndex + 2 <= len(self): string = self[self.readIndex:self.readIndex+2] tmp = struct.unpack('!h', string) return tmp[0] else: return None def writeByte(self, value): self.append(value) def writeBoolean(self, value): if value: self.append(1) else: self.append(0) def writeInt(self, value): tmp = struct.pack('!i', value) bytes = ByteArray(tmp) self.extend(bytes) def writeDouble(self, value): tmp = struct.pack('!d', value) bytes = ByteArray(tmp) self.extend(bytes) ## Write a string prefixed with its size as an Int. # @param string: the string to write in the ByteArray # # Write a Int representing the size of the dynamic string, and then the string itself. def writeString(self, string): self.writeInt(len(string)) tmp = ByteArray(string) self.extend(tmp)