Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)

public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(out); os.writeObject(obj); return out.toByteArray(); } public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException { ByteArrayInputStream in = new ByteArrayInputStream(data); ObjectInputStream is = new ObjectInputStream(in); return is.readObject(); }

Add “b” prefix to python variable?

# only an example, you can choose a different encoding bytes(‘example’, encoding=’utf-8′) In Python3: Bytes literals are always prefixed with ‘b’ or ‘B’; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with … Read more

How to know bytes size of python object like arrays and dictionaries? – The simple way

There’s: >>> import sys >>> sys.getsizeof([1,2, 3]) 96 >>> a = [] >>> sys.getsizeof(a) 72 >>> a = [1] >>> sys.getsizeof(a) 80 But I wouldn’t say it’s that reliable, as Python has overhead for each object, and there are objects that contain nothing but references to other objects, so it’s not quite the same as … Read more

Saving any file to in the database, just convert it to a byte array?

Since it’s not mentioned what database you mean I’m assuming SQL Server. Below solution works for both 2005 and 2008. You have to create table with VARBINARY(MAX) as one of the columns. In my example I’ve created Table Raporty with column RaportPlik being VARBINARY(MAX) column. Method to put file into database from drive: public static … Read more