use of java.security.KeyException in project android by cSploit.
the class UpdateService method downloadFile.
/**
* download mCurrentTask.url to mCurrentTask.path
*
* @throws KeyException when MD5 or SHA1 sum fails
* @throws IOException when IOError occurs
* @throws NoSuchAlgorithmException when required digest algorithm is not available
* @throws CancellationException when user cancelled the download via notification
*/
private void downloadFile() throws SecurityException, KeyException, IOException, NoSuchAlgorithmException, CancellationException {
if (mCurrentTask.url == null || mCurrentTask.path == null)
return;
File file = null;
FileOutputStream writer = null;
InputStream reader = null;
HttpURLConnection connection = null;
boolean exitForError = true;
try {
MessageDigest md5, sha1;
URL url;
byte[] buffer;
int read;
short percentage, previous_percentage;
long downloaded, total;
mBuilder.setContentTitle(getString(R.string.downloading_update)).setContentText(getString(R.string.connecting)).setSmallIcon(android.R.drawable.stat_sys_download).setProgress(100, 0, true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
md5 = (mCurrentTask.md5 != null ? MessageDigest.getInstance("MD5") : null);
sha1 = (mCurrentTask.sha1 != null ? MessageDigest.getInstance("SHA-1") : null);
buffer = new byte[4096];
file = new File(mCurrentTask.path);
if (file.exists() && file.isFile())
//noinspection ResultOfMethodCallIgnored
file.delete();
HttpURLConnection.setFollowRedirects(true);
url = new URL(mCurrentTask.url);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
writer = new FileOutputStream(file);
reader = connection.getInputStream();
total = connection.getContentLength();
read = connection.getResponseCode();
if (read != 200)
throw new IOException(String.format("cannot download '%s': responseCode: %d", mCurrentTask.url, read));
downloaded = 0;
previous_percentage = -1;
mBuilder.setContentText(file.getName());
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Logger.info(String.format("downloading '%s' to '%s'", mCurrentTask.url, mCurrentTask.path));
while (mRunning && (read = reader.read(buffer)) != -1) {
writer.write(buffer, 0, read);
if (md5 != null)
md5.update(buffer, 0, read);
if (sha1 != null)
sha1.update(buffer, 0, read);
if (total >= 0) {
downloaded += read;
percentage = (short) (((double) downloaded / total) * 100);
if (percentage != previous_percentage) {
mBuilder.setProgress(100, percentage, false).setContentInfo(percentage + "%");
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
previous_percentage = percentage;
}
}
}
if (!mRunning)
throw new CancellationException("download cancelled");
Logger.info("download finished successfully");
if (md5 != null || sha1 != null) {
if (md5 != null && !mCurrentTask.md5.equals(digest2string(md5.digest()))) {
throw new KeyException("wrong MD5");
} else if (sha1 != null && !mCurrentTask.sha1.equals(digest2string(sha1.digest()))) {
throw new KeyException("wrong SHA-1");
}
} else if (mCurrentTask.archiver != null) {
verifyArchiveIntegrity();
}
exitForError = false;
} finally {
if (exitForError && file != null && file.exists() && !file.delete())
Logger.error(String.format("cannot delete file '%s'", mCurrentTask.path));
try {
if (writer != null)
writer.close();
if (reader != null)
reader.close();
if (connection != null)
connection.disconnect();
} catch (IOException e) {
System.errorLogging(e);
}
}
}
use of java.security.KeyException in project graylog2-server by Graylog2.
the class PemReader method readPrivateKey.
static byte[] readPrivateKey(Path path) throws KeyException {
final byte[] bytes;
try {
bytes = Files.readAllBytes(path);
} catch (IOException e) {
throw new KeyException("Couldn't read private key from file: " + path, e);
}
final String content = new String(bytes, StandardCharsets.US_ASCII);
final Matcher m = KEY_PATTERN.matcher(content);
if (!m.find()) {
throw new KeyException("No private key found in file: " + path);
}
final String s = CharMatcher.breakingWhitespace().removeFrom(m.group(1));
byte[] base64 = s.getBytes(StandardCharsets.US_ASCII);
return Base64.getDecoder().decode(base64);
}
use of java.security.KeyException in project robovm by robovm.
the class KeyExceptionTest method testKeyException01.
/**
* Test for <code>KeyException()</code> constructor Assertion: constructs
* KeyException with no detail message
*/
public void testKeyException01() {
KeyException tE = new KeyException();
assertNull("getMessage() must return null.", tE.getMessage());
assertNull("getCause() must return null", tE.getCause());
}
use of java.security.KeyException in project robovm by robovm.
the class KeyExceptionTest method testKeyException08.
/**
* Test for <code>KeyException(String, Throwable)</code> constructor
* Assertion: constructs KeyException when <code>cause</code> is not null
* <code>msg</code> is null
*/
public void testKeyException08() {
KeyException tE = new KeyException(null, tCause);
if (tE.getMessage() != null) {
String toS = tCause.toString();
String getM = tE.getMessage();
assertTrue("getMessage() must should ".concat(toS), (getM.indexOf(toS) != -1));
}
assertNotNull("getCause() must not return null", tE.getCause());
assertEquals("getCause() must return ".concat(tCause.toString()), tE.getCause(), tCause);
}
use of java.security.KeyException in project robovm by robovm.
the class KeyExceptionTest method testKeyException02.
/**
* Test for <code>KeyException(String)</code> constructor Assertion:
* constructs KeyException with detail message msg. Parameter
* <code>msg</code> is not null.
*/
public void testKeyException02() {
KeyException tE;
for (int i = 0; i < msgs.length; i++) {
tE = new KeyException(msgs[i]);
assertEquals("getMessage() must return: ".concat(msgs[i]), tE.getMessage(), msgs[i]);
assertNull("getCause() must return null", tE.getCause());
}
}
Aggregations