Search in sources :

Example 1 with ProgressMonitorInputStream

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);
    }
}
Also used : ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 2 with ProgressMonitorInputStream

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");
}
Also used : ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) BasicContentHandlerFactory(org.apache.tika.sax.BasicContentHandlerFactory) Metadata(org.apache.tika.metadata.Metadata) TikaInputStream(org.apache.tika.io.TikaInputStream) IOException(java.io.IOException) RecursiveParserWrapper(org.apache.tika.parser.RecursiveParserWrapper) BoilerpipeContentHandler(org.apache.tika.parser.html.BoilerpipeContentHandler) TeeContentHandler(org.apache.tika.sax.TeeContentHandler) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) StringWriter(java.io.StringWriter) ParseContext(org.apache.tika.parser.ParseContext) TeeContentHandler(org.apache.tika.sax.TeeContentHandler)

Example 3 with ProgressMonitorInputStream

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));
    }
}
Also used : ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) DigestInputStream(java.security.DigestInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ProgressMonitorInputStream(javax.swing.ProgressMonitorInputStream) DigestInputStream(java.security.DigestInputStream) InputStream(java.io.InputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Aggregations

IOException (java.io.IOException)3 ProgressMonitorInputStream (javax.swing.ProgressMonitorInputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 DigestInputStream (java.security.DigestInputStream)2 OS_TYPE (com.vaklinov.zcashui.OSUtil.OS_TYPE)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 TikaInputStream (org.apache.tika.io.TikaInputStream)1 Metadata (org.apache.tika.metadata.Metadata)1 ParseContext (org.apache.tika.parser.ParseContext)1