use of javax.swing.ProgressMonitorInputStream in project zencash-swing-wallet-ui by ZencashOfficial.
the class ProvingKeyFetcher method verifyOrFetch.
private void verifyOrFetch(StartupProgressDialog parent) throws IOException {
OS_TYPE ost = OSUtil.getOSType();
File zCashParams = null;
// TODO: isolate getting ZcashParams in a utility method
if (ost == OS_TYPE.WINDOWS) {
zCashParams = new File(System.getenv("APPDATA") + "/ZcashParams");
} else if (ost == OS_TYPE.MAC_OS) {
File userHome = new File(System.getProperty("user.home"));
zCashParams = new File(userHome, "Library/Application Support/ZcashParams");
}
zCashParams = zCashParams.getCanonicalFile();
boolean needsFetch = false;
if (!zCashParams.exists()) {
needsFetch = true;
zCashParams.mkdirs();
}
// verifying key is small, always copy it
File verifyingKeyFile = new File(zCashParams, "sprout-verifying.key");
FileOutputStream fos = new FileOutputStream(verifyingKeyFile);
InputStream is = ProvingKeyFetcher.class.getClassLoader().getResourceAsStream("keys/sprout-verifying.key");
copy(is, fos);
fos.close();
is = null;
File provingKeyFile = new File(zCashParams, "sprout-proving.key");
provingKeyFile = provingKeyFile.getCanonicalFile();
if (!provingKeyFile.exists()) {
needsFetch = true;
} else if (provingKeyFile.length() != PROVING_KEY_SIZE) {
needsFetch = true;
}
if (!needsFetch) {
return;
}
JOptionPane.showMessageDialog(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.message"));
parent.setProgressText(langUtil.getString("proving.key.fetcher.option.pane.verify.progress.text"));
provingKeyFile.delete();
OutputStream os = new BufferedOutputStream(new FileOutputStream(provingKeyFile));
URL keyURL = new URL(pathURL);
URLConnection urlc = keyURL.openConnection();
urlc.setRequestProperty("User-Agent", "Wget/1.17.1 (linux-gnu)");
try {
is = urlc.getInputStream();
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.progress.monitor.text"), is);
pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
pmis.getProgressMonitor().setMillisToPopup(10);
copy(pmis, os);
os.close();
} finally {
try {
if (is != null)
is.close();
} catch (IOException ignore) {
}
}
parent.setProgressText(langUtil.getString("proving.key.fetcher.option.pane.verify.key.text"));
if (!checkSHA256(provingKeyFile, parent)) {
JOptionPane.showMessageDialog(parent, langUtil.getString("proving.key.fetcher.option.pane.verify.key.failed.text"));
System.exit(-4);
}
}
use of javax.swing.ProgressMonitorInputStream in project tika by apache.
the class TikaGUI method handleStream.
private void handleStream(InputStream input, Metadata md) throws Exception {
StringWriter htmlBuffer = new StringWriter();
StringWriter textBuffer = new StringWriter();
StringWriter textMainBuffer = new StringWriter();
StringWriter xmlBuffer = new StringWriter();
StringBuilder metadataBuffer = new StringBuilder();
ContentHandler handler = new TeeContentHandler(getHtmlHandler(htmlBuffer), getTextContentHandler(textBuffer), getTextMainContentHandler(textMainBuffer), getXmlContentHandler(xmlBuffer));
context.set(DocumentSelector.class, new ImageDocumentSelector());
input = TikaInputStream.get(new ProgressMonitorInputStream(this, "Parsing stream", input));
if (input.markSupported()) {
int mark = -1;
if (input instanceof TikaInputStream) {
if (((TikaInputStream) input).hasFile()) {
mark = (int) ((TikaInputStream) input).getLength();
}
}
if (mark == -1) {
mark = MAX_MARK;
}
input.mark(mark);
}
parser.parse(input, handler, md, context);
String[] names = md.names();
Arrays.sort(names);
for (String name : names) {
for (String val : md.getValues(name)) {
metadataBuffer.append(name);
metadataBuffer.append(": ");
metadataBuffer.append(val);
metadataBuffer.append("\n");
}
}
String name = md.get(Metadata.RESOURCE_NAME_KEY);
if (name != null && name.length() > 0) {
setTitle("Apache Tika: " + name);
} else {
setTitle("Apache Tika: unnamed document");
}
setText(metadata, metadataBuffer.toString());
setText(xml, xmlBuffer.toString());
setText(text, textBuffer.toString());
setText(textMain, textMainBuffer.toString());
setText(html, htmlBuffer.toString());
if (!input.markSupported()) {
setText(json, "InputStream does not support mark/reset for Recursive Parsing");
layout.show(cards, "metadata");
return;
}
boolean isReset = false;
try {
input.reset();
isReset = true;
} catch (IOException e) {
setText(json, "Error during stream reset.\n" + "There's a limit of " + MAX_MARK + " bytes for this type of processing in the GUI.\n" + "Try the app with command line argument of -J.");
}
if (isReset) {
RecursiveParserWrapper wrapper = new RecursiveParserWrapper(parser, new BasicContentHandlerFactory(BasicContentHandlerFactory.HANDLER_TYPE.BODY, -1));
wrapper.parse(input, null, new Metadata(), new ParseContext());
StringWriter jsonBuffer = new StringWriter();
JsonMetadataList.setPrettyPrinting(true);
JsonMetadataList.toJson(wrapper.getMetadata(), jsonBuffer);
setText(json, jsonBuffer.toString());
}
layout.show(cards, "metadata");
}
use of javax.swing.ProgressMonitorInputStream in project zencash-swing-wallet-ui by ZencashOfficial.
the class ProvingKeyFetcher method checkSHA256.
private static boolean checkSHA256(File provingKey, Component parent) throws IOException {
MessageDigest sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException impossible) {
throw new IOException(impossible);
}
try (InputStream is = new BufferedInputStream(new FileInputStream(provingKey))) {
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(parent, LanguageUtil.instance().getString("proving.key.fetcher.option.pane.verify.progress.monitor.text"), is);
pmis.getProgressMonitor().setMaximum(PROVING_KEY_SIZE);
pmis.getProgressMonitor().setMillisToPopup(10);
DigestInputStream dis = new DigestInputStream(pmis, sha256);
byte[] temp = new byte[0x1 << 13];
while (dis.read(temp) >= 0) ;
byte[] digest = sha256.digest();
return SHA256.equalsIgnoreCase(DatatypeConverter.printHexBinary(digest));
}
}
Aggregations