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.pawlidi.openaletheia.generator.KeyGenerator.java

public static RSAPublicKeySpec readPublicKeySpec(final String directory) {
    if (StringUtils.isBlank(directory) || !new File(directory).isDirectory()) {
        return null;
    }//from www .j a v  a2s.co m
    RSAPublicKeySpec publicKeySpec = null;
    ObjectInputStream inputStream = null;
    try {
        inputStream = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream(new File(directory, PUBLIC_KEYSPEC_FILE))));
        try {
            BigInteger m = (BigInteger) inputStream.readObject();
            BigInteger e = (BigInteger) inputStream.readObject();
            publicKeySpec = new RSAPublicKeySpec(m, e);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return publicKeySpec;
}

From source file:de.pawlidi.openaletheia.generator.KeyGenerator.java

public static RSAPrivateKeySpec readPrivateKeySpec(final String directory) {
    if (StringUtils.isBlank(directory) || !new File(directory).isDirectory()) {
        return null;
    }//from   ww  w.  j av a2  s  . co m
    RSAPrivateKeySpec privateKeySpec = null;
    ObjectInputStream inputStream = null;
    try {
        inputStream = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream(new File(directory, PRIVATE_KEYSPEC_FILE))));
        try {
            BigInteger m = (BigInteger) inputStream.readObject();
            BigInteger e = (BigInteger) inputStream.readObject();
            privateKeySpec = new RSAPrivateKeySpec(m, e);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                inputStream.close();
                inputStream = null;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return privateKeySpec;
}

From source file:gov.va.chir.tagline.dao.FileDao.java

private static Collection<Feature> loadFeatures(final File file) throws IOException, ClassNotFoundException {
    final Collection<Feature> features = new ArrayList<Feature>();
    final ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));

    try {/*from w w  w  .j a  v a  2 s.  c o  m*/
        Object o = in.readObject();

        while (o != null) {
            if (o instanceof Feature) {
                features.add((Feature) o);

                o = in.readObject();
            }
        }
    } catch (EOFException ex) {
        // ignore
    }

    in.close();

    return features;
}

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

public static List<Alarm> getAll() {
    List<Alarm> alarms = new ArrayList<Alarm>();
    Cursor cursor = Database.getCursor();
    if (cursor.moveToFirst()) {

        do {/* w  w  w . ja  va 2 s  . co m*/
            // COLUMN_ALARM_ID,
            // COLUMN_ALARM_ACTIVE,
            // COLUMN_ALARM_TIME,
            // COLUMN_ALARM_DAYS,
            // COLUMN_ALARM_DIFFICULTY,
            // COLUMN_ALARM_TONE,
            // COLUMN_ALARM_VIBRATE,
            // COLUMN_ALARM_NAME

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

            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()[cursor.getInt(4)]);
            alarm.setAlarmTonePath(cursor.getString(5));
            alarm.setVibrate(cursor.getInt(6) == 1);
            alarm.setAlarmName(cursor.getString(7));

            alarms.add(alarm);

        } while (cursor.moveToNext());
    }
    cursor.close();
    return alarms;
}

From source file:com.ligadata.EncryptUtils.EncryptionUtil.java

/**
 * Encrypt the plain text using public key.
 * //from  w w w  .  j av a2s  . c o m
 * @param algorithm
 *          : algorithm used
 * @param text
 *          : original plain text
 * @param publicKeyFile
 *          :The public key file
 * @return Encrypted text
 * @throws java.lang.Exception and exception is thrown
 */
public static byte[] encrypt(String algorithm, String text, String publicKeyFile) throws Exception {
    byte[] cipherText = null;
    try {
        ObjectInputStream inputStream = null;
        // Encrypt the string using the public key
        inputStream = new ObjectInputStream(new FileInputStream(publicKeyFile));
        final PublicKey publicKey = (PublicKey) inputStream.readObject();

        // get a cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(algorithm);
        // encrypt the plain text using the public key
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        cipherText = cipher.doFinal(text.getBytes());
    } catch (Exception e) {
        logger.error("Failed to encrypt given password", e);
        throw e;
    }
    return cipherText;
}

From source file:com.microsoft.azure.management.datalake.store.uploader.UploadMetadata.java

/**
 * Attempts to load an UploadMetadata object from the given file.
 *
 * @param filePath The full path to the file where to load the metadata from
 * @return A deserialized {@link UploadMetadata} object from the file specified.
 * @throws FileNotFoundException Thrown if the filePath is inaccessible or does not exist
 * @throws InvalidMetadataException Thrown if the metadata is not in the expected format.
 *///from   w  w w . j  av  a 2 s. c o m
public static UploadMetadata loadFrom(String filePath) throws FileNotFoundException, InvalidMetadataException {
    if (!new File(filePath).exists()) {
        throw new FileNotFoundException("Could not find metadata file: " + filePath);
    }

    UploadMetadata result = null;
    try {
        FileInputStream fileIn = new FileInputStream(filePath);
        ObjectInputStream in = new ObjectInputStream(fileIn);
        result = (UploadMetadata) in.readObject();
        in.close();
        fileIn.close();
        result.metadataFilePath = filePath;
        return result;
    } catch (Exception ex) {
        throw new InvalidMetadataException("Unable to parse metadata file", ex);
    }
}

From source file:com.joliciel.talismane.extensions.corpus.CorpusStatistics.java

public static CorpusStatistics loadFromFile(File inFile) {
    try {/* w  ww  . j  a v  a 2s  .com*/
        ZipInputStream zis = new ZipInputStream(new FileInputStream(inFile));
        zis.getNextEntry();
        ObjectInputStream in = new ObjectInputStream(zis);
        CorpusStatistics stats = null;
        try {
            stats = (CorpusStatistics) in.readObject();
        } catch (ClassNotFoundException e) {
            LogUtils.logError(LOG, e);
            throw new RuntimeException(e);
        }
        return stats;
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}

From source file:org.activiti.designer.eclipse.navigator.cloudrepo.ActivitiCloudEditorUtil.java

public static CloseableHttpClient getAuthenticatedClient() {

    // Get settings from preferences
    String url = PreferencesUtil.getStringPreference(Preferences.ACTIVITI_CLOUD_EDITOR_URL);
    String userName = PreferencesUtil.getStringPreference(Preferences.ACTIVITI_CLOUD_EDITOR_USERNAME);
    String password = PreferencesUtil.getStringPreference(Preferences.ACTIVITI_CLOUD_EDITOR_PASSWORD);
    String cookieString = PreferencesUtil.getStringPreference(Preferences.ACTIVITI_CLOUD_EDITOR_COOKIE);

    Cookie cookie = null;/*from w  w w . ja  v a  2s  .c om*/
    if (StringUtils.isNotEmpty(cookieString)) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                hexStringToByteArray(cookieString));
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            cookie = (BasicClientCookie) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Build session
    BasicCookieStore cookieStore = new BasicCookieStore();
    CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

    if (cookie == null) {
        try {
            HttpUriRequest login = RequestBuilder.post().setUri(new URI(url + "/rest/app/authentication"))
                    .addParameter("j_username", userName).addParameter("j_password", password)
                    .addParameter("_spring_security_remember_me", "true").build();

            CloseableHttpResponse response = httpClient.execute(login);

            try {
                EntityUtils.consume(response.getEntity());
                List<Cookie> cookies = cookieStore.getCookies();
                if (cookies.isEmpty()) {
                    // nothing to do
                } else {
                    Cookie reponseCookie = cookies.get(0);
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    ObjectOutputStream outputStream = new ObjectOutputStream(os);
                    outputStream.writeObject(reponseCookie);
                    PreferencesUtil.getActivitiDesignerPreferenceStore().setValue(
                            Preferences.ACTIVITI_CLOUD_EDITOR_COOKIE.getPreferenceId(),
                            byteArrayToHexString(os.toByteArray()));
                    InstanceScope.INSTANCE.getNode(ActivitiPlugin.PLUGIN_ID).flush();
                }

            } finally {
                response.close();
            }

        } catch (Exception e) {
            Logger.logError("Error authenticating " + userName, e);
        }

    } else {
        // setting cookie from cache
        cookieStore.addCookie(cookie);
    }

    return httpClient;
}

From source file:com.ligadata.EncryptUtils.EncryptionUtil.java

/**
 * Decrypt text using private key./*from   w  w  w.j a  v a 2 s .c  om*/
 * 
 * @param algorithm
 *          : algorithm used
 * @param text
 *          :encrypted text
 * @param privateKeyFile
 *          :The private key
 * @return plain text
 * @throws java.lang.Exception and exception is thrown
 */
public static String decrypt(String algorithm, byte[] text, String privateKeyFile) throws Exception {
    byte[] dectyptedText = null;
    try {
        ObjectInputStream inputStream = null;
        // Decrypt the cipher text using the private key.
        inputStream = new ObjectInputStream(new FileInputStream(privateKeyFile));
        final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
        // get a cipher object and print the provider
        final Cipher cipher = Cipher.getInstance(algorithm);
        // decrypt the text using the private key
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        dectyptedText = cipher.doFinal(text);

    } catch (Exception e) {
        logger.error("Failed to decrypt given password", e);
        throw e;
    }
    return new String(dectyptedText);
}

From source file:com.linkedin.pinot.core.startree.StarTreeSerDe.java

/**
 * Utility method that de-serializes bytes from inputStream
 * into the V0 version of star tree./*  w w  w.j  ava 2s  .co m*/
 *
 * @param inputStream
 * @return
 * @throws IOException
 * @throws ClassNotFoundException
 */
private static StarTreeInterf fromBytesToOnHeapFormat(InputStream inputStream)
        throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(inputStream);
    return (StarTree) ois.readObject();
}