Example usage for org.apache.commons.codec.binary Base64 encodeBase64Chunked

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64Chunked

Introduction

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

Prototype

public static byte[] encodeBase64Chunked(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks

Usage

From source file:com.basho.riak.bench.GetArgs.java

/**
 * @return the key
 */
public String getKey() {
    return CharsetUtils.asString(Base64.encodeBase64Chunked(key), CharsetUtils.ISO_8859_1);
}

From source file:net.bpelunit.framework.control.deploy.activebpel.BPRDeployRequestEntity.java

@Override
protected void populateMessage(SOAPMessage message) throws SOAPException, IOException {
    SOAPElement xmlDeployBpr = addRootElement(message, new QName(ACTIVEBPEL_ELEMENT_DEPLOYBPR));

    // Add filename
    SOAPElement xmlBprFilename = xmlDeployBpr.addChildElement(ACTIVEBPEL_ELEMENT_ABPRFILENAME);
    xmlBprFilename.addAttribute(new QName(ActiveBPELRequestEntityBase.NS_XMLSCHEMA_INSTANCE, "type"),
            XSD_STRING);//  www .j a  v a2  s. c o  m
    xmlBprFilename.setTextContent(FilenameUtils.getName(file.toString()));

    // Add data
    SOAPElement xmlBase64File = xmlDeployBpr.addChildElement(ACTIVEBPEL_ELEMENT_ABASE64FILE);
    xmlBase64File.addAttribute(new QName(ActiveBPELRequestEntityBase.NS_XMLSCHEMA_INSTANCE, "type"),
            XSD_STRING);

    StringBuilder content = new StringBuilder();
    byte[] arr = FileUtils.readFileToByteArray(file);
    byte[] encoded = Base64.encodeBase64Chunked(arr);
    for (int i = 0; i < encoded.length; i++) {
        content.append((char) encoded[i]);
    }
    xmlBase64File.setTextContent(content.toString());
}

From source file:net.bpelunit.framework.control.deploy.ode.ODERequestEntityFactory.java

private void prepareDeploySOAP(File file) throws IOException, SOAPException {
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage message = mFactory.createMessage();
    SOAPBody body = message.getSOAPBody();

    SOAPElement xmlDeploy = body.addChildElement(ODE_ELEMENT_DEPLOY);
    SOAPElement xmlZipFilename = xmlDeploy.addChildElement(ODE_ELEMENT_ZIPNAME);
    xmlZipFilename.setTextContent(FilenameUtils.getName(file.toString()).split("\\.")[0]);

    SOAPElement xmlZipContent = xmlDeploy.addChildElement(ODE_ELEMENT_PACKAGE);
    SOAPElement xmlBase64ZipFile = xmlZipContent.addChildElement(ODE_ELEMENT_ZIP, "dep", NS_DEPLOY_SERVICE);

    xmlBase64ZipFile.addAttribute(new QName(NS_XML_MIME, CONTENT_TYPE_STRING), ZIP_CONTENT_TYPE);

    StringBuilder content = new StringBuilder();
    byte[] arr = FileUtils.readFileToByteArray(file);
    byte[] encoded = Base64.encodeBase64Chunked(arr);
    for (int i = 0; i < encoded.length; i++) {
        content.append((char) encoded[i]);
    }//from ww  w  .  j a v  a 2  s . com

    xmlBase64ZipFile.setTextContent(content.toString());

    ByteArrayOutputStream b = new ByteArrayOutputStream();
    message.writeTo(b);
    fContent = b.toString();
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

public static String mergeHeaderPixelData(byte[] header, List<byte[]> images) throws IOException {
    // 1. read in header
    DicomObject dcmObj = byteArrayToDicomObject(header);

    // 2. Add pixel data to DicomObject
    if (images != null && !images.isEmpty()) {
        if (images.size() > 1) {
            DicomElement dicomElement = dcmObj.putFragments(Tag.PixelData, VR.OB, dcmObj.bigEndian(),
                    images.size());/*from  ww w .  j ava2 s .co  m*/

            for (byte[] image : images) {
                dicomElement.addFragment(image);
            }

            dcmObj.add(dicomElement);
        } else {
            dcmObj.putBytes(Tag.PixelData, VR.OB, images.get(0));
        }
    }

    return new String(Base64.encodeBase64Chunked(dicomObjectToByteArray(dcmObj)));
}

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

@Override
public String fromXML(String source) throws SerializerException {
    if (source == null || source.length() == 0) {
        return StringUtils.EMPTY;
    }/*from   w  w w .  jav a 2s. co  m*/

    try {
        // re-parse the xml to Mirth format
        Document document = documentSerializer.fromXML(source);
        Element element = document.getDocumentElement();
        Node node = element.getChildNodes().item(0);

        // change back to <attr> tag for all tags under <dicom> tag
        while (node != null) {
            renameTagToAttr(document, node);
            node = node.getNextSibling();
        }

        NodeList items = document.getElementsByTagName("item");

        // change back to <attr> tag for all tags under <item> tags
        if (items != null) {
            for (int i = 0; i < items.getLength(); i++) {
                Node itemNode = items.item(i);

                if (itemNode.getChildNodes() != null) {
                    NodeList itemNodes = itemNode.getChildNodes();

                    for (int j = 0; j < itemNodes.getLength(); j++) {
                        Node nodeItem = itemNodes.item(j);
                        renameTagToAttr(document, nodeItem);
                    }
                }
            }
        }

        // find the charset
        String charset = null;
        Element charsetElement = (Element) document.getElementsByTagName("tag00080005").item(0);

        if (charsetElement != null) {
            charset = charsetElement.getNodeValue();
        } else {
            charset = "utf-8";
        }

        // parse the Document into a DicomObject
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        DicomObject dicomObject = new BasicDicomObject();
        ContentHandlerAdapter contentHandler = new ContentHandlerAdapter(dicomObject);
        byte[] documentBytes = documentSerializer.toXML(document).trim().getBytes(charset);
        parser.parse(new InputSource(new ByteArrayInputStream(documentBytes)), contentHandler);
        return new String(Base64.encodeBase64Chunked(DICOMUtil.dicomObjectToByteArray(dicomObject)));
    } catch (Exception e) {
        throw new SerializerException(e);
    }
}

From source file:com.occamlab.te.parsers.ImageParser.java

private static String getBase64Data(BufferedImage buffImage, String formatName, Node node) throws Exception {
    int numBytes = buffImage.getWidth() * buffImage.getHeight() * 4;
    ByteArrayOutputStream baos = new ByteArrayOutputStream(numBytes);
    ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
    boolean status = ImageIO.write(buffImage, formatName, ios);
    if (!status) {
        throw new Exception("No suitable ImageIO writer found by ImageParser.getBase64Data() for image format "
                + formatName);//from   w  ww  .ja  v  a2  s  .c om
    }
    byte[] imageData = baos.toByteArray();
    // String base64String = Base64.encodeBase64String(imageData); this is
    // unchunked (no line breaks) which is unwieldy
    byte[] base64Data = Base64.encodeBase64Chunked(imageData);
    // 2011-11-15 PwD uncomment for Java 1.7 String base64String = new
    // String(base64Data, StandardCharsets.UTF_8);
    String base64String = new String(base64Data, "UTF-8"); // 2011-11-15 PwD
                                                           // for Java 1.6;
                                                           // remove for
                                                           // Java 1.7
    return base64String;
}

From source file:com.mirth.connect.server.util.DICOMUtil.java

private static String returnOtherImageFormat(MessageObject message, String format, boolean autoThreshold) {
    // use new method for jpegs
    if (format.equalsIgnoreCase("jpg") || format.equalsIgnoreCase("jpeg")) {
        return new String(Base64.encodeBase64Chunked(dicomToJpg(1, message, autoThreshold)));
    }//  ww w.  j a va2 s . c  om

    byte[] rawImage = Base64.decodeBase64(getDICOMRawData(message).getBytes());
    ByteArrayInputStream bais = new ByteArrayInputStream(rawImage);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        DICOM dicom = new DICOM(bais);
        dicom.run(message.getType());
        BufferedImage image = new BufferedImage(dicom.getWidth(), dicom.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.createGraphics();
        graphics.drawImage(dicom.getImage(), 0, 0, null);
        graphics.dispose();
        ImageIO.write(image, format, baos);
        return new String(Base64.encodeBase64Chunked(baos.toByteArray()));
    } catch (IOException e) {
        logger.error("Error Converting DICOM image", e);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(baos);
    }

    return StringUtils.EMPTY;
}

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

@Override
public String toXML(String source) throws SerializerException {
    try {//from  w  w  w .  j a  v a  2s. com
        DicomObject dicomObject = DICOMUtil.byteArrayToDicomObject(Base64.decodeBase64(source));
        // read in header and pixel data
        pixelData = extractPixelDataFromDicomObject(dicomObject);
        byte[] decodedMessage = DICOMUtil.dicomObjectToByteArray(dicomObject);
        rawData = new String(Base64.encodeBase64Chunked(decodedMessage));

        StringWriter output = new StringWriter();
        DicomInputStream dis = new DicomInputStream(new ByteArrayInputStream(decodedMessage));

        try {
            SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
            TransformerHandler handler = factory.newTransformerHandler();
            handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "no");
            handler.setResult(new StreamResult(output));

            final SAXWriter writer = new SAXWriter(handler, null);
            dis.setHandler(writer);
            dis.readDicomObject(new BasicDicomObject(), -1);
            String serializedDicomObject = output.toString();

            // rename the "attr" element to the tag ID
            Document document = documentSerializer.fromXML(serializedDicomObject);
            NodeList attrElements = document.getElementsByTagName("attr");

            for (int i = 0; i < attrElements.getLength(); i++) {
                Element attrElement = (Element) attrElements.item(i);
                renameAttrToTag(document, attrElement);
            }

            return documentSerializer.toXML(document);
        } catch (Exception e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(dis);
            IOUtils.closeQuietly(output);

            if (dis != null) {
                dis.close();
            }
        }
    } catch (Exception e) {
        throw new SerializerException(e);
    }
}

From source file:com.basho.riak.pbc.RiakClient.java

/**
 * helper method to use a reasonable default client id
 * beware, it caches the client id. If you call it multiple times on the same client
 * you get the *same* id (not good for reusing a client with different ids)
 * //from w ww  .j  a  v a  2 s  . com
 * @throws IOException
 */
public void prepareClientID() throws IOException {
    Preferences prefs = Preferences.userNodeForPackage(RiakClient.class);

    String clid = prefs.get("client_id", null);
    if (clid == null) {
        SecureRandom sr;
        try {
            sr = SecureRandom.getInstance("SHA1PRNG");
            // Not totally secure, but doesn't need to be
            // and 100% less prone to 30 second hangs on linux jdk5
            sr.setSeed(UUID.randomUUID().getLeastSignificantBits() + new Date().getTime());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] data = new byte[6];
        sr.nextBytes(data);
        clid = CharsetUtils.asString(Base64.encodeBase64Chunked(data), CharsetUtils.ISO_8859_1);
        prefs.put("client_id", clid);
        try {
            prefs.flush();
        } catch (BackingStoreException e) {
            throw new IOException(e.toString());
        }
    }

    setClientID(clid);
}

From source file:com.mirth.connect.server.util.DICOMMessageUtil.java

public static String convertDICOM(String imageType, ImmutableConnectorMessage message, int sliceIndex,
        boolean autoThreshold) {
    byte[] bytes = convertDICOMToByteArray(imageType, message, sliceIndex, autoThreshold);

    if (bytes != null) {
        return new String(Base64.encodeBase64Chunked(bytes));
    }// w ww.  j  a v  a  2 s . c  o m

    return "";
}