Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Map to Bytes and save to internal storage

How can I convert my Map<Integer, String> to byte[], and then write it to internal storage? I currently have:

        try {
            FileOutputStream fos = context.openFileOutput(Const.FILE_CATEGORIES, Context.MODE_PRIVATE);
            fos.write(null);
        } catch (FileNotFoundException e) {
            // reload and create the file again
        }

But..I don't know how to get the Map into the correct format, and then decode it back to it's original format once I need to load it again. I need to recreate this file once per week, and load it on application startup.

like image 957
Cody Avatar asked Dec 15 '11 08:12

Cody


People also ask

How do you read all bytes from InputStream?

InputStream reads bytes with the following read methods : read(byte[] b) — reads up to b. length bytes of data from this input stream into an array of bytes. read(byte[] b, int off, int len) — reads up to len bytes of data from this input stream into an array of bytes.

What is the process used to convert an object to a stream of bytes?

Data serialization is the process of converting an object into a stream of bytes to more easily save or transmit it.


2 Answers

  1. Using serialization in java you can easily parse any serializable objects to byte stream. Try to use the ObjectInputStream and the ObjectOuputStream.

  2. Use json to restore. You can use google-gson to convert Java objects to JSON and vice-versa.

  3. Use Parcel in android. The class android.os.Parcel is designd to pass data between the components in android(activity, service), but you can still use it to do data persistence. Just remember do not send the data to internet since differenct platforms may have different algorithm to do parsing.

I wrote a demo for serialization , have a try.

public static void main(String[] args) throws Exception {
    // Create raw data.
    Map<Integer, String> data = new HashMap<Integer, String>();
    data.put(1, "hello");
    data.put(2, "world");
    System.out.println(data.toString());

    // Convert Map to byte array
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(byteOut);
    out.writeObject(data);

    // Parse byte array to Map
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    ObjectInputStream in = new ObjectInputStream(byteIn);
    Map<Integer, String> data2 = (Map<Integer, String>) in.readObject();
    System.out.println(data2.toString());
}
like image 177
faylon Avatar answered Sep 24 '22 22:09

faylon


I know I am subscribing to an old thread but it popped out in my google search. So I will leave my 5 cents here:

You can use org.apache.commons.lang3.SerializationUtils which has these two methods:

/**
 * Serialize the given object to a byte array.
 * @param object the object to serialize
 * @return an array of bytes representing the object in a portable fashion
 */
public static byte[] serialize(Object object);

/**
 * Deserialize the byte array into an object.
 * @param bytes a serialized object
 * @return the result of deserializing the bytes
 */
public static Object deserialize(byte[] bytes);
like image 27
Evgeni Atanasov Avatar answered Sep 21 '22 22:09

Evgeni Atanasov