Search in sources :

Example 56 with ObjectInputStream

use of java.io.ObjectInputStream in project gephi by gephi.

the class GenericPropertyEditor method setAsText.

@Override
public void setAsText(String text) throws IllegalArgumentException {
    if (!text.equals("null")) {
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            bis = new ByteArrayInputStream(Base64.decodeBase64(text));
            ois = new ObjectInputStream(bis);
            val = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
            ;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException ex) {
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException ex) {
                }
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 57 with ObjectInputStream

use of java.io.ObjectInputStream in project facebook-android-sdk by facebook.

the class TestUtils method serializeAndUnserialize.

public static <T extends Serializable> T serializeAndUnserialize(final T t) {
    try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        new ObjectOutputStream(os).writeObject(t);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        @SuppressWarnings("unchecked") T ret = (T) (new ObjectInputStream(is)).readObject();
        return ret;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 58 with ObjectInputStream

use of java.io.ObjectInputStream in project gitblit by gitblit.

the class DeepCopier method copy.

/**
	 * Produce a deep copy of the given object. Serializes the entire object to
	 * a byte array in memory. Recommended for relatively small objects.
	 */
@SuppressWarnings("unchecked")
public static <T> T copy(T original) {
    T o = null;
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(byteOut);
        oos.writeObject(original);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(byteIn);
        try {
            o = (T) ois.readObject();
        } catch (ClassNotFoundException cex) {
        // actually can not happen in this instance
        }
    } catch (IOException iox) {
        // doesn't seem likely to happen as these streams are in memory
        throw new RuntimeException(iox);
    }
    return o;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 59 with ObjectInputStream

use of java.io.ObjectInputStream in project XobotOS by xamarin.

the class AnnotationMember method rethrowError.

/**
     * Throws contained error (if any) with a renewed stack trace.
     */
public void rethrowError() throws Throwable {
    if (tag == ERROR) {
        // first check for expected types
        if (value instanceof TypeNotPresentException) {
            TypeNotPresentException tnpe = (TypeNotPresentException) value;
            throw new TypeNotPresentException(tnpe.typeName(), tnpe.getCause());
        } else if (value instanceof EnumConstantNotPresentException) {
            EnumConstantNotPresentException ecnpe = (EnumConstantNotPresentException) value;
            throw new EnumConstantNotPresentException(ecnpe.enumType(), ecnpe.constantName());
        } else if (value instanceof ArrayStoreException) {
            ArrayStoreException ase = (ArrayStoreException) value;
            throw new ArrayStoreException(ase.getMessage());
        }
        // got some other error, have to go with deep cloning
        // via serialization mechanism
        Throwable error = (Throwable) value;
        StackTraceElement[] ste = error.getStackTrace();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(ste == null ? 512 : (ste.length + 1) * 80);
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(error);
        oos.flush();
        oos.close();
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        error = (Throwable) ois.readObject();
        ois.close();
        throw error;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 60 with ObjectInputStream

use of java.io.ObjectInputStream in project XobotOS by xamarin.

the class SealedObject method getObject.

/**
     * Returns the wrapped object, decrypting it using the specified key.
     *
     * @param key
     *            the key to decrypt the data with.
     * @return the encapsulated object.
     * @throws IOException
     *             if deserialization fails.
     * @throws ClassNotFoundException
     *             if deserialization fails.
     * @throws NoSuchAlgorithmException
     *             if the algorithm to decrypt the data is not available.
     * @throws InvalidKeyException
     *             if the specified key cannot be used to decrypt the data.
     */
public final Object getObject(Key key) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException {
    if (key == null) {
        throw new InvalidKeyException("key == null");
    }
    try {
        Cipher cipher = Cipher.getInstance(sealAlg);
        if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
            AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
            params.init(encodedParams);
            cipher.init(Cipher.DECRYPT_MODE, key, params);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, key);
        }
        byte[] serialized = cipher.doFinal(encryptedContent);
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
        return ois.readObject();
    } catch (NoSuchPaddingException e) {
        // with existing padding
        throw new NoSuchAlgorithmException(e.toString());
    } catch (InvalidAlgorithmParameterException e) {
        // with correct algorithm parameters
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalBlockSizeException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (BadPaddingException e) {
        // was correctly made
        throw new NoSuchAlgorithmException(e.toString());
    } catch (IllegalStateException e) {
        // should never be thrown because cipher is initialized
        throw new NoSuchAlgorithmException(e.toString());
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameters(java.security.AlgorithmParameters) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20