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:com.ikon.util.Serializer.java

/**
 * @param obj/*from w ww.  j a v a 2  s .c  om*/
 */
public static Object read(byte[] data) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;

    try {
        bais = new ByteArrayInputStream(data);
        ois = new ObjectInputStream(bais);
        return ois.readObject();
    } finally {
        IOUtils.closeQuietly(ois);
        IOUtils.closeQuietly(bais);
    }
}

From source file:RedisCache.java

/** Read the object from Base64 string. */
private static Object fromString(String s) throws IOException, ClassNotFoundException {
    byte[] data = Base64.decodeBase64(s);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o = ois.readObject();
    ois.close();/*  www  .  java 2 s .  c o  m*/
    return o;
}

From source file:com.db4o.sync4o.SyncKey.java

static public SyncKey fromEncodedString(String s) throws Exception {

    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(Base64.decodeBase64(s.getBytes())));
    SyncKey key = (SyncKey) ois.readObject();
    ois.close();/*from  w w w.j  a  v a  2  s  .  co  m*/

    return key;

}

From source file:org.versly.rest.wsdoc.RestDocumentation.java

/**
 * Read and return a serialized {@link RestDocumentation} instance from <code>in</code>,
 * as serialized by {@link #toStream}./*from w w w.j  a  va  2s . com*/
 */
public static RestDocumentation fromStream(InputStream in) throws IOException, ClassNotFoundException {
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(in);
        return (RestDocumentation) ois.readObject();
    } finally {
        if (ois != null)
            ois.close();
    }
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static Ticket decodeTicket(String ticket) {
    byte[] encryptedBytes = Base64.decodeBase64(ticket.getBytes());
    try {/*from ww  w.j a  va  2s. c o m*/
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        // Extract digest from decryptedBytes (MD5 length is 16 bytes)
        byte[] digest = Arrays.copyOfRange(decryptedBytes, decryptedBytes.length - 16, decryptedBytes.length);
        byte[] serializedMap = Arrays.copyOfRange(decryptedBytes, 0, decryptedBytes.length - 16);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        if (!Arrays.equals(digest, md.digest(serializedMap))) {
            throw new DigestException();
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedMap));
        return (Ticket) ois.readObject();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | IOException | ClassNotFoundException | DigestException e) {
        LOGGER.log(Level.SEVERE, "Error when decoding ticket: " + e.getMessage());
    }
    return null;
}

From source file:ProcessorDemo.java

private static void readConfig(String confFile) {
    String parent = new File(confFile).getParentFile().getParent();
    try {/*from  www  .  j  a v a2  s. c  o  m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new InputSource(confFile));
        NodeList nl = doc.getFirstChild().getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            org.w3c.dom.Node n = nl.item(i);
            if (n.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                String nn = n.getNodeName();
                String value = n.getFirstChild().getNodeValue();

                if (nn.equals("composit")) {
                    compositRule += value + "\n";
                }
                if (nn.equals("compound")) {
                    if (value.equals("\u69cb\u6210\u8a9e")) {
                        isCompound = false;
                    }
                }
                if (nn.equals("remark")) {
                    remarkRule += value + "\n";
                }
                if (nn.equals("dictionary")) {
                    // read nested tag in <dictinary>
                    NodeList dnl = n.getChildNodes();
                    for (int j = 0; j < dnl.getLength(); j++) {
                        org.w3c.dom.Node dn = dnl.item(j);
                        if (dn.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {

                            String dnn = dn.getNodeName();
                            if (dn.getFirstChild() == null) {
                                throw new IllegalArgumentException("element '" + dnn + "' is empty");
                            }
                            String dvalue = dn.getFirstChild().getNodeValue();

                            if (dnn.equals("compound")) {
                                compoundFile = SenUtils.getPath(dvalue, parent);
                            }
                        }
                    }
                }
            }
        }
        if (!isCompound) {
            try {
                ObjectInputStream is = new ObjectInputStream(new FileInputStream(compoundFile));
                HashMap hashmap = (HashMap) is.readObject();
            } catch (ClassNotFoundException e1) {
                throw new RuntimeException(e1);
            }
        }
    } catch (ParserConfigurationException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (FileNotFoundException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (SAXException e) {
        throw new IllegalArgumentException(e.getMessage());
    } catch (IOException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
}

From source file:SerialIntList.java

/**
 * Deserialize the contents of File f and return the resulting object
 *//*from   w ww .j  a v  a2s  .c  o m*/
static Object load(File f) throws IOException, ClassNotFoundException {
    ObjectInputStream in = // The class for de-serialization
            new ObjectInputStream(new FileInputStream(f));
    return in.readObject(); // This method deserializes an object graph
}

From source file:be.vdab.util.Programma.java

private static TreeSet<Voertuig> lees(String file) {//gekozen voor treeSet , zo duidelijk dat er een TreeSet uitkomt (die serialiseerbaar is)
    TreeSet<Voertuig> voertuigen = null;
    ObjectInputStream ois = null;
    try {//from w  ww .  jav  a2  s .com
        FileInputStream fis = new FileInputStream(file);
        ois = new ObjectInputStream(fis);
        voertuigen = (TreeSet<Voertuig>) ois.readObject();//je moet exact weten wat er is ingegaan, anders kan je object niet casten naar juiste klasse!?
    } catch (FileNotFoundException fnfe) {
        System.out.println(fnfe.getMessage());
    } catch (IOException ioe) {
        System.out.println(ioe.getMessage());
    } catch (ClassNotFoundException cnfe) {
        System.out.println(cnfe.getMessage());
    } finally {
        try {
            ois.close();
        } catch (IOException ioe) {
            System.err.println(" ioe.getMessage()");
        }
    }

    return voertuigen;
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static Object deserialize(byte[] in) {
    Object rv = null;/*from ww w. j  ava2 s  .  c  o  m*/

    try {
        if (in != null) {
            ByteArrayInputStream bis = new ByteArrayInputStream(in);
            ObjectInputStream is = new ObjectInputStream(bis);

            rv = is.readObject();
            is.close();
            bis.close();
        }
    } catch (Exception e) {
        throw new RuntimeException("deserialize failed", e);
    }

    return rv;
}

From source file:bencoding.securely.Converters.java

public static Object deserializeObjectFromString(String objectString) throws Exception {

    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(
            Base64.decode(objectString, Base64.DEFAULT));
    GZIPInputStream gzipInputStream = new GZIPInputStream(arrayInputStream);
    ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream);

    Object object = objectInputStream.readObject();

    objectInputStream.close();//from   w  w  w. j a v  a 2 s  . c  o m
    gzipInputStream.close();
    arrayInputStream.close();

    return object;
}