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) {
}
}
}
}
}
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);
}
}
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;
}
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;
}
}
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());
}
}
Aggregations