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:de.alpharogroup.io.SerializedObjectUtils.java

/**
 * The Method toObject() converts the given byte array into an Object.
 *
 * @param byteArray//w  w w .ja  v a  2  s  .  co  m
 *            The byte array to convert into an Object.
 * @return The Object the was converted from the byte array.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws ClassNotFoundException
 *             is thrown when a class is not found in the classloader or no definition for the
 *             class with the specified name could be found.
 */
public static Object toObject(final byte[] byteArray) throws IOException, ClassNotFoundException {
    Object object = null;
    ByteArrayInputStream byteArrayInputStream = null;
    ObjectInputStream objectInputStream = null;
    try {
        byteArrayInputStream = new ByteArrayInputStream(byteArray);
        objectInputStream = new ObjectInputStream(byteArrayInputStream);
        object = objectInputStream.readObject();
        objectInputStream.close();
    } finally {
        StreamUtils.closeInputStream(byteArrayInputStream);
        StreamUtils.closeInputStream(objectInputStream);
    }
    return object;
}

From source file:com.titilink.camel.rest.util.CommonUtils.java

/**
 * clone ? ?/*  ww  w.ja v  a 2 s .co  m*/
 *
 * @param obj ?
 * @return ?
 */
public static Object clone(Object obj) {
    if (obj == null) {
        return null;
    }

    Object anotherObj = null;
    byte[] bytes = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        bytes = baos.toByteArray();
    } catch (Exception ex) {
        LOGGER.error("CloneObjectUtil cloneexception ", ex);
        return null;
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (Exception e) {
                LOGGER.error("CloneObjectUtil cloneexception ", e);
            }
        }
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(bais);
        anotherObj = ois.readObject();
    } catch (Exception ex) {
        LOGGER.error("CloneObjectUtil cloneexception ", ex);
        return null;
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (Exception e) {
                LOGGER.error("CloneObjectUtil cloneexception ", e);
            }
        }
    }
    return anotherObj;
}

From source file:license.regist.ProjectInfo.java

private static PrivateKey readPrivateKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(
            new BufferedInputStream(ProjectInfo.class.getClassLoader().getResourceAsStream("private.key")));
    try {/*from w w w  . j  av a 2  s.  c  o m*/
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePrivate(keySpec);
    } finally {
        oin.close();
    }
}

From source file:com.projity.interval.ValueObjectForIntervalTable.java

protected static ValueObjectForIntervalTable deserialize(ObjectInputStream s, ValueObjectForIntervalTable v)
        throws IOException, ClassNotFoundException {
    v.name = (String) s.readObject();
    v.valueObjects = (ArrayList) s.readObject();
    return v;/* w ww .  j a v  a2  s  .  c o m*/
}

From source file:jp.xet.baseunits.tests.SerializationTester.java

/**
 * ????????//  w  w w.  j a v  a2s  .  co m
 * 
 * @param serializable 
 * @throws AssertionError ????
 */
public static void assertCanBeSerialized(Object serializable) {
    if (Serializable.class.isInstance(serializable) == false) {
        fail("Object doesn't implement java.io.Serializable interface: " + serializable.getClass());
    }

    ObjectOutputStream out = null;
    ObjectInputStream in = null;
    ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
    ByteArrayInputStream byteArrayIn = null;
    try {
        out = new ObjectOutputStream(byteArrayOut);
        out.writeObject(serializable);

        byteArrayIn = new ByteArrayInputStream(byteArrayOut.toByteArray());
        in = new ObjectInputStream(byteArrayIn);
        Object deserialized = in.readObject();
        if (serializable.equals(deserialized) == false) {
            fail("Reconstituted object is expected to be equal to serialized");
        }
    } catch (IOException e) {
        fail(e.getMessage());
    } catch (ClassNotFoundException e) {
        fail(e.getMessage());
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }
}

From source file:com.breadwallet.tools.manager.SharedPreferencesManager.java

@SuppressWarnings("unchecked")
public static Set<CurrencyEntity> getExchangeRates(Activity context) {
    SharedPreferences prefs = context.getSharedPreferences(BRConstants.PREFS_NAME, Context.MODE_PRIVATE);

    byte[] bytes = prefs.getString(BRConstants.EXCHANGE_RATES, "{}").getBytes();
    if (bytes.length == 0) {
        return null;
    }//from   w  ww .j a  v  a  2  s  .  co m
    Set<CurrencyEntity> result = null;
    ByteArrayInputStream byteArray = new ByteArrayInputStream(bytes);
    Base64InputStream base64InputStream = new Base64InputStream(byteArray, Base64.NO_WRAP);
    ObjectInputStream in;
    try {
        in = new ObjectInputStream(base64InputStream);

        result = (Set<CurrencyEntity>) in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:za.co.neilson.alarm.database.Database.java

public static Alarm getAlarm(int id) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { COLUMN_ALARM_ID, COLUMN_ALARM_ACTIVE, COLUMN_ALARM_TIME,
            COLUMN_ALARM_DAYS, COLUMN_ALARM_DIFFICULTY, COLUMN_ALARM_TONE, COLUMN_ALARM_VIBRATE,
            COLUMN_ALARM_NAME };//from   www.j a v a 2 s .  c o  m
    Cursor c = getDatabase().query(ALARM_TABLE, columns, COLUMN_ALARM_ID + "=" + id, null, null, null, null);
    Alarm alarm = null;

    if (c.moveToFirst()) {

        alarm = new Alarm();
        alarm.setId(c.getInt(1));
        alarm.setAlarmActive(c.getInt(2) == 1);
        alarm.setAlarmTime(c.getString(3));
        byte[] repeatDaysBytes = c.getBlob(4);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(repeatDaysBytes);
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Alarm.Day[] repeatDays;
            Object object = objectInputStream.readObject();
            if (object instanceof Alarm.Day[]) {
                repeatDays = (Alarm.Day[]) object;
                alarm.setDays(repeatDays);
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        alarm.setDifficulty(Difficulty.values()[c.getInt(5)]);
        alarm.setAlarmTonePath(c.getString(6));
        alarm.setVibrate(c.getInt(7) == 1);
        alarm.setAlarmName(c.getString(8));
    }
    c.close();
    return alarm;
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.util.SVMHMMUtils.java

/**
 * Loads a serialized BidiMap from file//from ww w  . ja va  2 s.c o m
 *
 * @param inputFile input file
 * @return BidiMap
 * @throws IOException
 */
public static BidiMap loadMapping(File inputFile) throws IOException {
    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(inputFile));

    try {
        return (BidiMap) inputStream.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:edu.iu.daal_qr.QRDaalCollectiveMapper.java

private static DataCollection deserializePartialResult(ByteArray byteArray)
        throws IOException, ClassNotFoundException {
    /* Create an input stream to deserialize the numeric table from the array */
    byte[] buffer = byteArray.get();
    ByteArrayInputStream inputByteStream = new ByteArrayInputStream(buffer);
    ObjectInputStream inputStream = new ObjectInputStream(inputByteStream);

    /* Create a numeric table object */
    DataCollection restoredDataTable = (DataCollection) inputStream.readObject();
    restoredDataTable.unpack(daal_Context);

    return restoredDataTable;
}

From source file:license.TestWakeLicense.java

/**
 * ?//from   ww w .  ja va 2s . c om
 * @return
 * @throws Exception
 */
static PublicKey readPublicKeyFromFile() throws Exception {
    ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(KeyData.publicKey));
    try {
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        return fact.generatePublic(keySpec);
    } finally {
        oin.close();
    }
}