Android Object Serialization loadObjectFromFile(String filename)

Here you can find the source of loadObjectFromFile(String filename)

Description

load Object From File

License

Apache License

Declaration

public static Object loadObjectFromFile(String filename) 

Method Source Code

/*/*from w w  w.  j  a  va2s. c o  m*/
Copyright (c) 2012 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.FileChannel.*;
import java.util.*;
import java.util.zip.*;

public class Main{
    private static HashMap<String, String> serial;
    public static final String SERIALPREFIX = "SER:";
    public static Object loadObjectFromFile(String filename) {
        if (filename.startsWith(SERIALPREFIX)) {
            filename = serialFileName(filename);
        }
        File f = new File(filename);
        try {
            InputStream is = new BufferedInputStream(new FileInputStream(f));
            if (filename.endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
            ObjectInputStream ois = new RefactoringObjectInputStream(is);
            Object object = ois.readObject();
            ois.close();
            return object;
        } catch (FileNotFoundException fe) {
            System.err.println("No file: " + filename);
            return null;
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static String serialFileName(String serId) {
        if (serId.startsWith(SERIALPREFIX)) {
            serId = serId.substring(SERIALPREFIX.length());
        }
        return serialProperties().get(serId);
    }
    private static HashMap<String, String> serialProperties() {
        if (serial == null) {
            Properties serialP = PropertyLoader
                    .loadProperties("serialization");
            HashMap<String, String> serialM = new HashMap<String, String>();
            for (Map.Entry<Object, Object> e : serialP.entrySet()) {
                if (e.getKey() instanceof String
                        && e.getValue() instanceof String) {
                    serialM.put((String) e.getKey(), (String) e.getValue());
                } else {
                    System.err.println("Bad property: " + e);
                }
            }
            //do replacement on the properties
            serial = new HashMap<String, String>();
            for (Map.Entry<String, String> e : serialM.entrySet()) {
                String path = e.getValue();
                for (Map.Entry<String, String> oe : serialM.entrySet()) {
                    if (e.getKey() != oe.getKey()) {
                        path = path.replace("${" + oe.getKey() + "}",
                                oe.getValue());
                    }
                }
                serial.put(e.getKey(), path);
            }
        }
        return serial;
    }
}

Related

  1. readObjectFromFile(File fileLocation)
  2. readObjectFromFile(String filename)
  3. saveObjectAsFile(String filename, Object object)
  4. writeObjectToFile(File fileLocation, Object obj)