Example usage for org.apache.commons.codec.binary Base64InputStream Base64InputStream

List of usage examples for org.apache.commons.codec.binary Base64InputStream Base64InputStream

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64InputStream Base64InputStream.

Prototype

public Base64InputStream(InputStream in) 

Source Link

Usage

From source file:cloudeventbus.cli.Certs.java

public static void main(String[] args) throws Exception {
    final JCommander commander = new JCommander();

    final DefaultOptions options = new DefaultOptions();
    final CreateAuthorityCommand createAuthorityCommand = new CreateAuthorityCommand();
    final CreateClientCommand createClientCommand = new CreateClientCommand();
    final CreateServerCommand createServerCommand = new CreateServerCommand();
    final ChainCertificateCommand chainCertificateCommand = new ChainCertificateCommand();
    final ShowCertificateCommand showCertificateCommand = new ShowCertificateCommand();
    final ImportCertificatesCommand importCertificatesCommand = new ImportCertificatesCommand();
    final RemoveAuthorityCommand removeAuthorityCommand = new RemoveAuthorityCommand();
    final ValidateCommand validateCommand = new ValidateCommand();
    commander.addObject(options);/*from  w  ww .  ja  va 2 s .co m*/
    commander.addCommand(createAuthorityCommand);
    commander.addCommand(createClientCommand);
    commander.addCommand(createServerCommand);
    commander.addCommand(chainCertificateCommand);
    commander.addCommand(new ListAuthorities());
    commander.addCommand(showCertificateCommand);
    commander.addCommand(importCertificatesCommand);
    commander.addCommand(removeAuthorityCommand);
    commander.addCommand(validateCommand);

    commander.setProgramName("eventbus-certs");

    try {
        commander.parse(args);

        final String command = commander.getParsedCommand();
        if (command == null) {
            commander.usage();
        } else {
            final TrustStore trustStore = CertificateUtils.loadTrustStore(options.trustStore);
            switch (command) {
            case CREATE_AUTHORITY: {
                final KeyPair keyPair = CertificateUtils.generateKeyPair();
                CertificateUtils.savePrivateKey(keyPair.getPrivate(), createAuthorityCommand.privateKey);
                final Certificate certificate = CertificateUtils.generateSelfSignedCertificate(keyPair,
                        getExpirationDate(createAuthorityCommand.expirationDate),
                        Subject.list(createAuthorityCommand.subscribePermissions),
                        Subject.list(createAuthorityCommand.publishPermissions),
                        createAuthorityCommand.comment);
                trustStore.add(certificate);

                CertificateUtils.saveCertificates(options.trustStore, trustStore);
                System.out.println("Created authority certificate.");
                break;
            }
            case LIST_AUTHORITIES: {
                displayCertificates(trustStore);
                break;
            }
            case CREATE_CLIENT:
                createCertificate(trustStore, Certificate.Type.CLIENT, createClientCommand);
                break;
            case CREATE_SERVER:
                createCertificate(trustStore, Certificate.Type.SERVER, createServerCommand);
                break;
            case SHOW_CERTIFICATE: {
                final CertificateChain certificates = CertificateUtils
                        .loadCertificateChain(showCertificateCommand.certificate);
                displayCertificates(certificates);
                break;
            }
            case CHAIN_CERTIFICATE:
                chainCertificate(chainCertificateCommand);
                break;
            case VALIDATE_CERTIFICATE: {
                final CertificateChain certificates = CertificateUtils
                        .loadCertificateChain(validateCommand.certificate);
                trustStore.validateCertificateChain(certificates);
                System.out.println(validateCommand.certificate + " is valid.");
                break;
            }
            case IMPORT_CERTIFICATES: {
                final Path path = Paths.get(importCertificatesCommand.certificate);
                try (final InputStream fileIn = Files.newInputStream(path);
                        final InputStream in = new Base64InputStream(fileIn)) {
                    final Collection<Certificate> certificates = new ArrayList<>();
                    CertificateStoreLoader.load(in, certificates);
                    for (Certificate certificate : certificates) {
                        trustStore.add(certificate);
                    }
                    CertificateUtils.saveCertificates(options.trustStore, trustStore);
                }
                break;
            }
            case REMOVE_AUTHORITY:
                if (trustStore.remove(removeAuthorityCommand.serialNumber)) {
                    System.err.println("Removed certificate from trust store.");
                    CertificateUtils.saveCertificates(options.trustStore, trustStore);
                } else {
                    System.err.println("Certificate with serial number " + removeAuthorityCommand.serialNumber
                            + " not found.");
                }
            }
        }
    } catch (ParameterException e) {
        System.err.println(e.getMessage());
        commander.usage(commander.getParsedCommand());
        System.exit(1);
    }
}

From source file:com.mirth.connect.model.converters.DICOMConverter.java

public static DicomObject byteArrayToDicomObject(byte[] bytes, boolean decodeBase64) throws IOException {
    DicomObject basicDicomObject = new BasicDicomObject();
    DicomInputStream dis = null;//from   w  ww. j  a  va 2  s .  co  m

    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        InputStream inputStream;
        if (decodeBase64) {
            inputStream = new BufferedInputStream(new Base64InputStream(bais));
        } else {
            inputStream = bais;
        }
        dis = new DicomInputStream(inputStream);
        /*
         * This parameter was added in dcm4che 2.0.28. We use it to retain the memory allocation
         * behavior from 2.0.25.
         * http://www.mirthcorp.com/community/issues/browse/MIRTH-2166
         * http://www.dcm4che.org/jira/browse/DCM-554
         */
        dis.setAllocateLimit(-1);
        dis.readDicomObject(basicDicomObject, -1);
    } catch (IOException e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(dis);
    }

    return basicDicomObject;
}

From source file:com.gc.iotools.fmt.decoders.Base64Decoder.java

/**
 * {@inheritDoc}//from www.ja  va 2  s .co m
 */
public InputStream decode(final InputStream istream) {
    final InputStream decoded = new Base64InputStream(istream);
    return decoded;
}

From source file:fr.dutra.confluence2wordpress.util.CodecUtils.java

public static String decodeAndExpand(String encoded) throws IOException {
    GZIPInputStream gzis = new GZIPInputStream(
            new Base64InputStream(new ByteArrayInputStream(encoded.getBytes(UTF_8))));
    try {//from w ww  .ja va  2s .  com
        return IOUtils.toString(gzis, UTF_8);
    } finally {
        gzis.close();
    }
}

From source file:com.mirth.connect.plugins.pdfviewer.PDFViewer.java

public void viewAttachments(String channelId, Long messageId, String attachmentId) {

    try {//from   w  w w  . j ava  2 s.com
        Attachment attachment = parent.mirthClient.getAttachment(channelId, messageId, attachmentId);
        byte[] rawData = attachment.getContent();
        Base64InputStream in = new Base64InputStream(new ByteArrayInputStream(rawData));

        File temp = File.createTempFile(attachment.getId(), ".pdf");
        temp.deleteOnExit();

        OutputStream out = new FileOutputStream(temp);
        IOUtils.copy(in, out);
        out.close();

        new MirthPDFViewer(true, temp);

    } catch (Exception e) {
        parent.alertThrowable(parent, e);
    }
}

From source file:com.mirth.connect.plugins.dicomviewer.DICOMViewer.java

public void viewAttachments(String channelId, Long messageId, String attachmentId) {
    // do viewing code
    try {/*from  ww  w.ja  v  a2  s . c  om*/
        ConnectorMessage message = parent.messageBrowser.getSelectedConnectorMessage();
        byte[] rawImage = StringUtils.getBytesUsAscii(parent.mirthClient.getDICOMMessage(message));
        ByteArrayInputStream bis = new ByteArrayInputStream(rawImage);
        DICOM dcm = new DICOM(new Base64InputStream(bis));
        // run() is required to create the dicom object. The argument serves multiple purposes. If it is null or empty, it opens a dialog to select a dicom file.
        // Otherwise, if dicom.show() is called, it is the title of the dialog. Since we are showing the dialog here, we pass the string we want to use as the title.
        dcm.run("DICOM Image Viewer");
        dcm.show();

        ImageWindow window = dcm.getWindow();

        if (window != null) {
            Dimension dlgSize = window.getSize();
            Dimension frmSize = parent.getSize();
            Point loc = parent.getLocation();

            if ((frmSize.width == 0 && frmSize.height == 0) || (loc.x == 0 && loc.y == 0)) {
                dcm.getWindow().setLocationRelativeTo(null);
            } else {
                dcm.getWindow().setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
            }
        } else {
            parent.alertError(parent, "Unable to load DICOM attachment.");
        }
    } catch (Exception e) {
        parent.alertThrowable(parent, e);
    }

}

From source file:eu.peppol.document.PayloadDigestCalculator.java

public static MessageDigestResult calcDigest(String algorithm, StandardBusinessDocumentHeader sbdh,
        InputStream inputStream) {
    MessageDigest messageDigest;/*from   w  ww  .  j ava2  s  .  com*/

    try {
        messageDigest = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(
                "Unknown digest algorithm " + OxalisConstant.DEFAULT_DIGEST_ALGORITHM + " " + e.getMessage(),
                e);
    }

    InputStream inputStreamToCalculateDigestFrom = null;

    ManifestItem manifestItem = SbdhFastParser.searchForAsicManifestItem(sbdh);
    if (manifestItem != null) {
        // creates an FilterInputStream, which will extract the ASiC in binary format.
        inputStreamToCalculateDigestFrom = new Base64InputStream(new AsicFilterInputStream(inputStream));
    } else
        inputStreamToCalculateDigestFrom = inputStream;

    DigestInputStream digestInputStream = new DigestInputStream(
            new BufferedInputStream(inputStreamToCalculateDigestFrom), messageDigest);
    try {
        IOUtils.copy(digestInputStream, new NullOutputStream());
    } catch (IOException e) {
        throw new IllegalStateException("Unable to calculate digest for payload");
    }

    return new MessageDigestResult(messageDigest.digest(), messageDigest.getAlgorithm());
}

From source file:com.jaeksoft.searchlib.streamlimiter.StreamLimiterBase64.java

@Override
protected void loadOutputCache() throws LimitException, IOException {
    InputStream is = null;/*from   w w  w .j a  v  a2  s.  c  o  m*/
    Base64InputStream b64is = null;
    try {
        is = new LargeStringInputString(base64text, 131072);
        b64is = new Base64InputStream(is);
        loadOutputCache(b64is);
    } finally {
        IOUtils.close(b64is, is);
    }
}

From source file:com.mirth.connect.plugins.imageviewer.ImageViewer.java

public void viewAttachments(String channelId, Long messageId, String attachmentId) {

    JFrame frame = new JFrame("Image Viewer");

    try {//from  w w  w. j  a v a 2  s. c o m

        Attachment attachment = parent.mirthClient.getAttachment(channelId, messageId, attachmentId);
        byte[] rawData = attachment.getContent();
        ByteArrayInputStream bis = new ByteArrayInputStream(rawData);

        image = ImageIO.read(new Base64InputStream(bis));

        JScrollPane pictureScrollPane = new JScrollPane(new JLabel(new ImageIcon(image)));
        pictureScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        pictureScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        frame.add(pictureScrollPane);

        frame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                e.getWindow().dispose();
            }
        });

        frame.pack();

        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        // Resize the frame so that it fits and scrolls images larger than
        // 800x600 properly.
        if (imageWidth > 800 || imageHeight > 600) {
            int width = imageWidth;
            int height = imageHeight;
            if (imageWidth > 800) {
                width = 800;
            }
            if (imageHeight > 600) {
                height = 600;
            }

            // Also add the scrollbars to the window width if necessary.
            Integer scrollBarWidth = (Integer) UIManager.get("ScrollBar.width");
            int verticalScrollBar = 0;
            int horizontalScrollBar = 0;

            if (width == 800) {
                horizontalScrollBar = scrollBarWidth.intValue();
            }
            if (height == 600) {
                verticalScrollBar = scrollBarWidth.intValue();
            }

            // Also add the window borders to the width.
            width = width + frame.getInsets().left + frame.getInsets().right + verticalScrollBar;
            height = height + frame.getInsets().top + frame.getInsets().bottom + horizontalScrollBar;

            frame.setSize(width, height);
        }

        Dimension dlgSize = frame.getSize();
        Dimension frmSize = parent.getSize();
        Point loc = parent.getLocation();

        if ((frmSize.width == 0 && frmSize.height == 0) || (loc.x == 0 && loc.y == 0)) {
            frame.setLocationRelativeTo(null);
        } else {
            frame.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                    (frmSize.height - dlgSize.height) / 2 + loc.y);
        }

        frame.setVisible(true);
    } catch (Exception e) {
        parent.alertThrowable(parent, e);
    }
}

From source file:com.asksunny.tool.Base64Tool.java

protected void decode() throws IOException {

    try (FileInputStream fin = new FileInputStream(pathToInput);
            Base64InputStream b64in = new Base64InputStream(fin);
            FileOutputStream fout = new FileOutputStream(this.pathToOutput)) {
        byte[] buf = new byte[BUFFER_SIZE];
        int len = 0;
        while ((len = b64in.read(buf)) != -1) {
            fout.write(buf, 0, len);/*from   w  w  w . ja  va  2s  .  co m*/
        }
        fout.flush();
    }
}