9

I have a code where I am converting array list to byte array and then saving that byte array as a BLOB in MySQL database. Below is code:-

Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
    List extraAttributes = (ArrayList) temp;
    resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));    

The method createByteArray is defined as below:

 private byte [] createByteArray( Object obj)
    {
        byte [] bArray = null;
        try
        {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream objOstream = new ObjectOutputStream(baos);
                objOstream.writeObject(obj);
                bArray = baos.toByteArray();
        }
        catch (Exception e)
        {
      TraceDbLog.writeError("Problem in createByteArray", e);

        }

                return bArray;

    }

Well the above code was written earlier for writing HashMap to BLOB i am using same for converting ArrayList if HashMap to BLOB.

The problem which is occurring in read code when i am reading the blob.

 private Object readBytes (ResultSet rs, String columnName)
    throws SQLException
    {
        ObjectInputStream ois  = null;
        byte [] newArray;
        Object obj = null;

        try
        {
            newArray = rs.getBytes(columnName);

            ois = new ObjectInputStream (new ByteArrayInputStream(newArray));
            obj = ois.readObject ();
        }

In the read part the object is not coming as arrayList of hasMap and in debug perspective in eclipse eclipse is also not able to inspect the object which is coming.

I have also tried typecasting the object to List but still no success in getting the right response. Please tell me whether there is any flaw in reading/writing the above BLOB.

9
  • what object r u getting after read object? what is it printing if u do System.out.println(obj.getClass().getName()) Commented Jan 1, 2014 at 14:01
  • Its showing com.sun.jdi.InvocationtargetException which means eclipse cant resolve. Although this exception is coming in debug perspective there is no point when code moves into catch block the program execution is normal Commented Jan 1, 2014 at 14:03
  • is obj = ois.readObject (); failing when u execute the code ? if it's not failing what type of object is it returning Commented Jan 1, 2014 at 14:06
  • IN logs i printed obj.getClass().getName() its returning java.util.ArrayList Commented Jan 1, 2014 at 14:48
  • So read object is returning an ArrayList as expected. What is the exact issue you are facing ? Is the data inside the list not what u expect ? Commented Jan 1, 2014 at 14:52

2 Answers 2

5

I have added sample coding for convert ArrayList to byte[].

One reasonable way would be to use UTF-8 encoding like DataOutputStream does for each string in the list. For a string it writes 2 bytes for the length of the UTF-8 encoding followed by the UTF-8 bytes.

This would be portable if you're not using Java on the other end. Here's an example of encoding and decoding an ArrayList:

// example input list
List<String> list = new ArrayList<String>();
list.add("foo");
list.add("bar");
list.add("baz");

// write to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(baos);

for (String element : list) {
    out.writeUTF(element);
}
byte[] bytes = baos.toByteArray();

// read from byte array
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
DataInputStream in = new DataInputStream(bais);
while (in.available() > 0) {
    String element = in.readUTF();
    System.out.println(element);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just in case the built-in bytes need to be parsed using an xml unmarshaller, the encoding schemes need to be matched. In my case, it was not in UTF-8, so I just have to use out.writeBytes instead of out.writeUTF. Or else, the marshaller would throw up exception saying that unexpected content appears in the prolog. Just FYI.
1

The easiest way is to convert it to json string and then to bytes

Gson gson = new Gson();
Type type = new TypeToken<List<Alarm>>() {}.getType();
String json = gson.toJson(list, type);
byte[] bytes = json.getBytes();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.