转换到ArrayList的字节数组 [英] Convert ArrayList to byte array

查看:135
本文介绍了转换到ArrayList的字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code,其中我转换数组列表为字节数组,然后保存该字节数组在MySQL数据库中的BLOB。下面是code: -

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));    

该方法 createByteArray 的定义如下:

 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;

    }

那么上面的code的写HashMap中为BLOB我使用同样的转换的ArrayList如果HashMap中以BLOB前面写的。

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

这是在读code发生时,我正在读的斑点问题。

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 ();
        }

在读取的部分对象是不是来为hasMap的ArrayList和在Eclipse Eclipse Debug透视也无法检查其即将到来的对象。

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.

我也曾尝试类型转换对象列出,但仍然在获得正确的反应没有成功。
请告诉我是否有读/缺陷的任何书面上述BLOB。

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.

推荐答案

我添加了一些样本编码为byte []。
一种合理的方法是使用UTF-8编码像DataOutputStream类确实为列表中的每个字符串。为一个字符串它为UTF-8编码,随后由UTF-8字节的长度,写入2个字节。这将如果你不是在另一端使用Java可移植的。这里是编码的例子和解码一个ArrayList

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);
}

这篇关于转换到ArrayList的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆