+ def read(self,length=None):
+ if not length is None:
+ if type(length)!=type(0):
+ raise TypeError("length to read should be number")
+ buf=create_string_buffer(length)
+ readbytes=libcrypto.BIO_read(self.bio,buf,length)
+ if readbytes==-2:
+ raise NotImplementedError("Function is not supported by this BIO")
+ if readbytes==-1:
+ raise IOError
+ if readbytes==0:
+ return ""
+ return buf.raw[:readbytes]
+ else:
+ buf=create_string_buffer(1024)
+ out=""
+ r=1
+ while r>0:
+ r=libcrypto.BIO_read(self.bio,buf,1024)
+ if r==-2:
+ raise NotImplementedError("Function is not supported by this BIO")
+ if r==-1:
+ raise IOError
+ if (r>0):
+ out+=buf.raw[:r]
+ return out
+
+ def write(self,data):
+ r=libcrypto.BIO_write(self.bio,data,len(data))
+ if r==-2:
+ raise NotImplementedError("Function not supported by this BIO")
+ if r<len(data):
+ raise IOError("Not all data were successfully written")
+