def __blob(self, obj=0): assert self.has_table("blobtable"), "no blob table" tabname, sql = self.table("blobtable") fn = tempfile.mktemp() fp = None c = self.cursor() try: hello = ("hello", ) * 1024 c.execute(sql) self.db.commit() from java.io import FileOutputStream, FileInputStream, ObjectOutputStream, ObjectInputStream, ByteArrayInputStream fp = FileOutputStream(fn) oos = ObjectOutputStream(fp) oos.writeObject(hello) fp.close() fp = FileInputStream(fn) blob = ObjectInputStream(fp) value = blob.readObject() fp.close() assert hello == value, "unable to serialize properly" if obj == 1: fp = open(fn, "rb") else: fp = FileInputStream(fn) c.execute("insert into %s (a, b) values (?, ?)" % (tabname), [(0, fp)], {1: zxJDBC.BLOB}) self.db.commit() c.execute("select * from %s" % (tabname)) f = c.fetchall() bytes = f[0][1] blob = ObjectInputStream(ByteArrayInputStream(bytes)).readObject() assert hello == blob, "blobs are not equal" finally: c.execute("drop table %s" % (tabname)) c.close() self.db.commit() if os.path.exists(fn): if fp: fp.close() os.remove(fn)
def test_serialization(self): s = set(range(5, 10)) output = ByteArrayOutputStream() serializer = ObjectOutputStream(output) serializer.writeObject(s) serializer.close() input = ByteArrayInputStream(output.toByteArray()) unserializer = ObjectInputStream(input) self.assertEqual(s, unserializer.readObject())
def deserialize(filepath): f = None o = None obj = None try: f = FileInputStream(filepath) o = ObjectInputStream(f) obj = o.readObject() except: syncPrintQ(sys.exc_info()) finally: if f: f.close() if o: o.close() if obj is None: syncPrintQ("Failed to deserialize object at " + filepath) return obj
def loadModel(file): s = ObjectInputStream(FileInputStream(file)) crf = s.readObject() s.close() return crf
def readInstanceList(fname): s = ObjectInputStream(FileInputStream(fname)) instances = s.readObject() s.close() return instances