I stored a Vector<'String> into a BLOB field in a sqlite DB.
The problem is when I want to get that Vector, it has to be with cur.getBlob (method from a Cursor cur) and it returns a byte[], the question is, how can I get my vector from these byte[].
Thanks!!!
I do not have any experience with android but from java viewpoint this should work:
byte[] youByteArray;
ByteArrayInputStream in = new ByteArrayInputStream(youByteArray);
ObjectInputStream is = new ObjectInputStream(in);
// Will throw compiler warnings but should work fine.
Vector<String> yourVector = (Vector<String>) is.readObject();
References: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::p11_question_id:1285601748584
Vector<byte[]> records = new Vector<byte[]>();
records.add(byteData);
Related
I'm trying to decompress a json object in Java that was initially compressed in PHP. Here's how it gets compressed into PHP:
function zip_json_encode(&$arr) {
$uncompressed = json_encode($arr);
return pack('L', strlen($uncompressed)).gzcompress($uncompressed);
}
and decoded (again in PHP):
function unzip_json_decode(&$data) {
$uncompressed = #gzuncompress(substr($data,4));
return json_decode($uncompressed, $array_instead_of_object);
}
That gets put into MySQL and now it must be pulled out of the db by Java. We pull it out from the ResultSet like this:
String field = rs.getString("field");
I then pass that string to a method to decompress it. This is where it falls apart.
private String decompressHistory(String historyString) throws SQLException {
StringBuffer buffer = new StringBuffer();
try {
byte[] historyBytes = historyString.substring(4).getBytes();
ByteArrayInputStream bin = new ByteArrayInputStream(historyBytes);
InflaterInputStream in = new InflaterInputStream(bin, new Inflater(true));
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
// buf should be decoded, right?
}
} catch (IOException e) {
e.getStackTrace();
}
return buffer.toString();
}
Not quite sure what's going wrong here, but any pointers would be appreciated!
You need to get rid of the true in Inflater(true). Use just Inflater(). The true makes it expect raw deflate data. Without the true, it is expecting zlib-wrapped deflate data. PHP's gzcompress() produces zlib-wrapped deflate data.
Gzipped data is binary, byte[]. Using String, Unicode text, not only needs conversion, but is faulty.
For instance this involves a conversion:
byte[] historyBytes = historyString.substring(4).getBytes();
byte[] historyBytes = historyString.substring(4).getBytes("ISO-8859-1");
The first version uses the default platform encoding, making the application non-portable.
The first to-do is to use binary data in the database as VARBINARY or BLOB.
ImputStream field = rs.getBinaryStream("field");
try (InputStream in = new GZIPInputStream(field)) {
...
}
Or so. Mind the other answer.
In the end, neither of the above solutions worked, but both have merits. When we pulled the data out of mysql and cast it to bytes we have a number of missing character bytes (67). This made it impossible to decompress on the java side. As for the answers above. Mark is correct that gzcompress() uses zlib and therefore you should use the Inflater() class in Java.
Joop is correct that the data conversion is faulty. Our table was too large to convert it to varbinary or blob. That may have solved the problem, but didn't work for us. We ended up having java make a request to our PHP app, then simply unpacked the compressed data on the PHP side. This worked well. Hopefully this is helpful to anyone else that stumbles across it.
I have a table column type bytea. It is populated using hibernate and a field of type Serializable. Any ideas how I would be able to retrieve the values outside hibernate in a simple java standalone program? The values are of type string, double or integer.
the database is UTF8
while(res.next()){
byte[] byteArr = res.getBytes("value");
}
while (res.next()) {
byte[] byteArr = res.getBytes("value");
ByteArrayInputStream is = new ByteArrayInputStream(byteArr);
ObjectInputStream ois = new ObjectInputStream(is);
Serializable val = (Serializable) ois.readObject();
System.out.println(val);
}
I'm trying to find an easy way to create a mutable byte array that can automatically append any primitive Java data type. I've been searching but could not find anything useful.
I'm looking for something like this
ByteAppender byteStructure = new ByteAppender();
byteStructure.appendInt(5);
byteStructure.appendDouble(10.0);
byte[] bytes = byteStructure.toByteArray();
There is ByteByffer which is great, but you have to know the size of the buffer before you start, which won't work in my case. There is a similar thing (StringBuilder) for creating Strings, but I cannot find one for Bytes.
I thought this would be obvious in Java.
I guess you are looking for java.io.DataOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(out);
dout.writeInt(1234);
dout.writeLong(123L);
dout.writeFloat(1.2f);
byte[] storingData = out.toByteArray();
How to use storingData?
//how to use storingData?
ByteArrayInputStream in = new ByteArrayInputStream(storingData);
DataInputStream din = new DataInputStream(in);
int v1 = din.readInt();//1234
long v2 = din.readLong();//123L
float v3 = din.readFloat();//1.2f
I'm currently writing a tool to plug into an existing enterprise application that uses Hibernate. My tool at install time needs to write some values into the database where one of the columns is a serialized version of a setting descriptor object. This object has two lists of objects and a few primitive types.
My current approach is to create a ByteArrayOutputStream and an ObjectOutputStream and then write the ObjectOutputStream to the ByteArrayOutputStream, then passing the resulting byte array into the sql with Spring's 1SimpleJdbcTemplate1. My current issue with this approach is that when the enterprise tool pulls my rows it fails to de-serialze the column with the following:
org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
I feel I may need to serialize the inner objects, but have no clue how to do that and keep everything together.
Ended up solving my own problem. In the hibernate API there is a class called SerializationHelper that has a static function serialize(Serializable obj) which I was able to use to serialize my object and then insert it into the database. Hibernate was then able to read it in the enterprise app.
You can serealize a Java object into bytes and then store it in a BLOB.
Serialize:
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream objOut = new ObjectOutputStream(byteOut);
objOut.writeObject(object);
objOut.close();
byteOut.close();
byte[] bytes = byteOut.toByteArray()
Deserialize:
public <T extends Serializable> T getObject(Class<T> type) throws IOException, ClassNotFoundException{
if(bytes == null){
return null;
}
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
ObjectInputStream in = new ObjectInputStream(byteIn);
T obj = (T) in.readObject();
in.close();
return obj;
}
I'm trying to save a java ArrayList in a database (H2) by setting it as a blob, for retrieval later. If this is a bad approach, please say - I haven't been able to find much information on this area.
I have a column of type Blob in the database, and Hibernate maps to this with java.sql.Blob. The code I'm struggling with is:
Drawings drawing = new Drawings();
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(bos);
oos.writeObject(plan.drawingPane21.pointList);
byte[] buff = bos.toByteArray();
Blob drawingBlob = null;
drawingBlob.setBytes(0, buff);
drawing.setDrawingObject(drawingBlob);
} catch (Exception e){
System.err.println(e);
}
The object I'm trying to save into a blob (plan.drawingPane21.pointList) is of type ArrayList<DrawingDot>, DrawingDot being a custom class implementing Serializable.
My code is failing on the line drawingBlob.setBytes(0, buff); with a NullPointerException.
Help appreciated.
In case anyone is having the same problem, I solved it by taking advantage of the SerialBlob class's constructor rather than using setBytes:
byte[] buff = bos.toByteArray();
Blob drawingBlob = null;
drawingBlob = new SerialBlob(buff);
drawing.setDrawingObject(drawingBlob);
You have never initialized the variable drawingBlob:
Blob drawingBlob = null;//<- not initialized
drawingBlob.setBytes(0, buff);//<- drawingBlob is null here.
My knowledge of Hibernate is very limited, but I believe that if the data type is mapped to a Blob then hibernate will perform the serialization for you, which makes sense as the standard way to set data in a blob is via the methods in a parametrized PreparedStatement.