try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(o);
objectOutputStream.flush();
} catch (IOException e) {
throw new CredentialStoreException("Error serializing object.", e);
} finally {
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
log.error("Error occurred while closing object output stream", e);
}
}
}
// encrypt the byte array
if (encrypt()) {
byte[] array = byteArrayOutputStream.toByteArray();
try {
return SecurityUtil.encrypt(this.keyStorePath, this.secretKeyAlias, this.keyStorePasswordCallback, array);
} catch (GeneralSecurityException e) {
throw new CredentialStoreException("Error encrypting data", e);
} catch (IOException e) {
throw new CredentialStoreException("Error encrypting data. IO exception.", e);
}
} else {
return byteArrayOutputStream.toByteArray();
}
}