Example usage for java.io ObjectInputStream readObject

List of usage examples for java.io ObjectInputStream readObject

Introduction

In this page you can find the example usage for java.io ObjectInputStream readObject.

Prototype

public final Object readObject() throws IOException, ClassNotFoundException 

Source Link

Document

Read an object from the ObjectInputStream.

Usage

From source file:hydrograph.engine.hadoop.inputformat.TupleMemoryInputFormat.java

public static ITupleGenerator retrieveTupleGenerator(JobConf conf, String key) {
    String s = conf.get(key);/*from  w ww .  ja  v a 2 s  . co  m*/
    if (s == null)

        return null;

    String[] pieces = s.split(":");

    byte[] val;

    if (pieces.length > 1) {
        val = decodeBytes(pieces[1]);
    } else {
        val = new byte[0];
    }

    ByteArrayInputStream stream = new ByteArrayInputStream(val);
    ObjectInputStream in;

    ITupleGenerator tupleGenerator;
    try {
        in = new ObjectInputStream(stream);
        tupleGenerator = (ITupleGenerator) in.readObject();
        in.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }

    return tupleGenerator;
}

From source file:test.integ.be.fedict.performance.util.PerformanceResultDialog.java

public static PerformanceResultsData readResults(File resultsFile) throws Exception {

    if (!resultsFile.exists()) {
        throw new Exception("Results file: " + resultsFile.getPath() + " does not exist.");
    }//from w  w w  . jav  a 2 s  . c  o m

    ObjectInputStream in = new ObjectInputStream(new FileInputStream(resultsFile));
    return (PerformanceResultsData) in.readObject();
}

From source file:com.openteach.diamond.network.waverider.slave.SlaveState.java

public static SlaveState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {/*from w  w  w  . jav  a2  s .  c  o m*/
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (SlaveState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        if (oin != null) {
            try {
                oin.close();
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }
}

From source file:MSUmpire.BaseDataStructure.InstrumentParameter.java

public static InstrumentParameter ReadParametersSerialization(String filepath) {
    if (!new File(FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + "_params.ser")
            .exists()) {/*  w w w. ja va2s . co m*/
        return null;
    }
    try {
        Logger.getRootLogger().info("Reading parameters from file:" + FilenameUtils.getFullPath(filepath)
                + FilenameUtils.getBaseName(filepath) + "_params.ser...");

        FileInputStream fileIn = new FileInputStream(
                FilenameUtils.getFullPath(filepath) + FilenameUtils.getBaseName(filepath) + "_params.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        InstrumentParameter params = (InstrumentParameter) in.readObject();
        in.close();
        fileIn.close();
        return params;

    } catch (Exception ex) {
        Logger.getRootLogger().error(ExceptionUtils.getStackTrace(ex));
        return null;
    }
}

From source file:com.smartitengineering.jetty.session.replication.impl.hbase.SessionDataObjectConverter.java

private static Object deserialize(InputStream inputStream) {
    if (inputStream == null) {
        throw new IllegalArgumentException("The InputStream must not be null");
    }//from   w  w  w .  j  a  va2  s.c  o m
    ObjectInputStream in = null;
    try {
        // stream closed in the finally
        in = new ClassLoaderObjectInputStream(Thread.currentThread().getContextClassLoader(), inputStream);
        return in.readObject();

    } catch (ClassNotFoundException ex) {
        throw new SerializationException(ex);
    } catch (IOException ex) {
        throw new SerializationException(ex);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
}

From source file:Main.java

public static final Object RestoreObject(final String path) {
    FileInputStream fileInputStream = null;
    ObjectInputStream objectInputStream = null;
    Object object = null;/*from   w ww . j a va 2s .c  o m*/
    File file = new File(path);
    if (!file.exists()) {
        return null;
    }
    try {
        fileInputStream = new FileInputStream(file);
        objectInputStream = new ObjectInputStream(fileInputStream);
        object = objectInputStream.readObject();
        return object;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (objectInputStream != null) {
                objectInputStream.close();
            }
            if (fileInputStream != null) {
                fileInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return object;
}

From source file:de.tud.cs.se.flashcards.persistence.Store.java

public static FlashcardSeries openSeries(File file) throws IOException {

    ObjectInputStream oin = null;
    try {/*from   w  ww  . ja v a 2  s. c  o  m*/

        FlashcardSeries series = new FlashcardSeries();

        oin = new ObjectInputStream(new FileInputStream(file));
        int size = oin.readInt();
        for (int i = 0; i < size; i++) {
            series.addCard((Flashcard) oin.readObject());
        }

        return series;
    } catch (ClassNotFoundException e) {
        // the file did contain something unexpected...
        throw new IOException(e);
    } finally {
        if (oin != null)
            IOUtils.closeQuietly(oin);
    }
}

From source file:com.facebook.infrastructure.utils.FBUtilities.java

public static Object deserializeFromStream(byte[] bytes) {
    Object o = null;//from ww w .  j a va 2  s  . c  o  m
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    try {
        ObjectInputStream ois = new ObjectInputStream(bis);
        try {
            o = ois.readObject();
        } catch (ClassNotFoundException e) {
        }
        ois.close();
        bis.close();
    } catch (IOException e) {
        LogUtil.getLogger(FBUtilities.class.getName()).info(LogUtil.throwableToString(e));
    }
    return o;
}

From source file:cpd3314.buildit12.CPD3314BuildIt12.java

/**
 * Build a sample method that saves a handful of car instances as Serialized
 * objects, and as JSON objects, then re-open the saved versions in a
 * different method// w  w  w  .j  a  v  a  2  s.  c o  m
 */
public static void doBuildIt2Input() {
    // Unserialize the Objects -- Save them to an ArrayList and Output
    try {
        FileInputStream objFile = new FileInputStream("cars.obj");
        ObjectInputStream objStream = new ObjectInputStream(objFile);

        ArrayList<Car> cars = new ArrayList<>();

        boolean eof = false;
        while (!eof) {
            try {
                cars.add((Car) objStream.readObject());
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
            } catch (EOFException ex) {
                // We must catch the EOF exception to leave the loop
                eof = true;
            }
        }

        System.out.println("Unserialized Data:");
        for (Car car : cars) {
            System.out.printf("Make: %s, Model: %s, Year: %d\n", car.getMake(), car.getModel(), car.getYear());
        }
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }

    // Read from JSON and Output
    try {
        File file = new File("cars.json");
        Scanner input = new Scanner(file);
        while (input.hasNext()) {
            JSONParser parse = new JSONParser();
            try {
                // This should be the root JSON Object, which has an array of Car objects
                JSONObject json = (JSONObject) parse.parse(input.nextLine());

                // We pull the array out to work with it
                JSONArray cars = (JSONArray) json.get("cars");

                System.out.println("Un-JSON-ed Data:");
                for (Object car : cars) {
                    // Convert each Object to a JSONObject
                    JSONObject carJSON = (JSONObject) car;
                    // Convert each JSONObject to an actual Car
                    Car carObj = new Car(carJSON);
                    // Output from the Actual Car class                        
                    System.out.printf("Make: %s, Model: %s, Year: %d\n", carObj.getMake(), carObj.getModel(),
                            carObj.getYear());
                }
            } catch (ParseException ex) {
                Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(CPD3314BuildIt12.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:net.menthor.editor.v2.util.Util.java

/** Read the object from Base64 string. */
public static Object fromBase64String(String s) throws IOException, ClassNotFoundException {
    byte[] data = Base64.getDecoder().decode(s);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o = ois.readObject();
    ois.close();/*from w w w  . j ava2  s. c  om*/
    return o;
}