Search in sources :

Example 51 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project ph-commons by phax.

the class JAXBMarshallerHelperTest method testCloseExternalOnWriteToOutputStream.

@Test
public void testCloseExternalOnWriteToOutputStream() {
    final MockMarshallerExternal m = new MockMarshallerExternal();
    assertNotNull(m.toString());
    final MutableBoolean aClosed = new MutableBoolean(false);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream() {

        @Override
        public void close() {
            super.close();
            aClosed.set(true);
        }
    };
    {
        final com.helger.jaxb.mock.external.MockJAXBArchive aArc = new com.helger.jaxb.mock.external.MockJAXBArchive();
        aArc.setVersion("1.23");
        m.write(aArc, aBAOS);
    }
    LOGGER.info(aBAOS.getAsString(StandardCharsets.UTF_8));
    // Must be closed!
    assertTrue("Not closed!", aClosed.booleanValue());
}
Also used : MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) MutableBoolean(com.helger.commons.mutable.MutableBoolean) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) MockMarshallerExternal(com.helger.jaxb.mock.MockMarshallerExternal) Test(org.junit.Test)

Example 52 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project ph-commons by phax.

the class JAXBMarshallerHelperTest method testGetAsBytes.

@Test
public void testGetAsBytes() {
    final MockMarshallerExternal m = new MockMarshallerExternal();
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    byte[] aDirectBytes;
    {
        final MockJAXBArchive aArc = new MockJAXBArchive();
        aArc.setVersion("1.24");
        for (int i = 0; i < 100; ++i) {
            final MockJAXBCollection aCollection = new MockJAXBCollection();
            aCollection.setDescription("Internal bla foo");
            aCollection.setID(i);
            aArc.getCollection().add(aCollection);
        }
        m.write(aArc, aBAOS);
        aDirectBytes = m.getAsBytes(aArc);
    }
    assertArrayEquals(aBAOS.toByteArray(), aDirectBytes);
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MockJAXBArchive(com.helger.jaxb.mock.external.MockJAXBArchive) MockJAXBCollection(com.helger.jaxb.mock.external.MockJAXBCollection) MockMarshallerExternal(com.helger.jaxb.mock.MockMarshallerExternal) Test(org.junit.Test)

Example 53 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project ph-commons by phax.

the class MicroWriter method getNodeAsBytes.

/**
 * Convert the passed micro node to an XML byte array using the provided
 * settings.
 *
 * @param aNode
 *        The node to be converted to a byte array. May not be
 *        <code>null</code> .
 * @param aSettings
 *        The XML writer settings to use. May not be <code>null</code>.
 * @return The byte array representation of the passed node.
 * @since 8.6.3
 */
@Nullable
public static byte[] getNodeAsBytes(@Nonnull final IMicroNode aNode, @Nonnull final IXMLWriterSettings aSettings) {
    ValueEnforcer.notNull(aNode, "Node");
    ValueEnforcer.notNull(aSettings, "Settings");
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(50 * CGlobal.BYTES_PER_KILOBYTE)) {
        // start serializing
        if (writeToStream(aNode, aBAOS, aSettings).isSuccess())
            return aBAOS.toByteArray();
    } catch (final Exception ex) {
        if (LOGGER.isErrorEnabled())
            LOGGER.error("Error serializing MicroDOM with settings " + aSettings.toString(), ex);
    }
    return null;
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Nullable(javax.annotation.Nullable)

Example 54 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project ph-commons by phax.

the class XMLWriterTest method testGetXHTMLString.

@Test
public void testGetXHTMLString() {
    final String sSPACER = " ";
    final String sINDENT = XMLWriterSettings.DEFAULT_INDENTATION_STRING;
    final String sTAGNAME = "notext";
    // Java 1.6 JAXP handles things differently
    final String sSerTagName = "<" + sTAGNAME + "></" + sTAGNAME + ">";
    final Document doc = XMLFactory.newDocument("html", DOCTYPE_XHTML10_QNAME, DOCTYPE_XHTML10_URI);
    final Element aHead = (Element) doc.getDocumentElement().appendChild(doc.createElementNS(DOCTYPE_XHTML10_URI, "head"));
    aHead.appendChild(doc.createTextNode("Hallo"));
    final Element aNoText = (Element) doc.getDocumentElement().appendChild(doc.createElementNS(DOCTYPE_XHTML10_URI, sTAGNAME));
    aNoText.appendChild(doc.createTextNode(""));
    // test including doc type
    {
        final String sResult = XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML());
        assertEquals("<!DOCTYPE html PUBLIC \"" + DOCTYPE_XHTML10_QNAME + "\"" + sSPACER + "\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + "<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + sINDENT + "<head>Hallo</head>" + CRLF + sINDENT + sSerTagName + CRLF + "</html>" + CRLF, sResult);
        assertEquals(sResult, XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML()));
    }
    // test without doc type
    {
        final String sResult = XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML().setSerializeDocType(EXMLSerializeDocType.IGNORE));
        assertEquals("<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + sINDENT + "<head>Hallo</head>" + CRLF + sINDENT + sSerTagName + CRLF + "</html>" + CRLF, sResult);
    }
    {
        final String sResult = XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML().setSerializeDocType(EXMLSerializeDocType.IGNORE).setIndent(EXMLSerializeIndent.NONE));
        assertEquals("<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\"><head>Hallo</head>" + sSerTagName + "</html>", sResult);
        assertEquals(sResult, XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML().setSerializeDocType(EXMLSerializeDocType.IGNORE).setIndent(EXMLSerializeIndent.NONE)));
    }
    // add text element
    aNoText.appendChild(doc.createTextNode("Hallo "));
    final Element b = (Element) aNoText.appendChild(doc.createElementNS(DOCTYPE_XHTML10_URI, "strong"));
    b.appendChild(doc.createTextNode("Welt"));
    aNoText.appendChild(doc.createCDATASection("!!!"));
    aNoText.appendChild(doc.createComment("No"));
    // test without doc type
    {
        final String sResult = XMLWriter.getNodeAsString(doc, XMLWriterSettings.createForXHTML().setSerializeDocType(EXMLSerializeDocType.IGNORE).setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN));
        assertEquals("<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + sINDENT + "<head>Hallo</head>" + CRLF + sINDENT + "<notext>Hallo <strong>Welt</strong><![CDATA[!!!]]><!--No--></notext>" + CRLF + "</html>" + CRLF, sResult);
    }
    // test as XML (with doc type and indent)
    {
        final String sResult = XMLWriter.getNodeAsString(doc);
        assertEquals("<?xml version=\"1.0\" encoding=\"" + StandardCharsets.UTF_8.name() + "\" standalone=\"no\"?>" + CRLF + "<!DOCTYPE html PUBLIC \"" + DOCTYPE_XHTML10_QNAME + "\"" + sSPACER + "\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + "<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + sINDENT + "<head>Hallo</head>" + CRLF + sINDENT + "<notext>Hallo <strong>Welt</strong><![CDATA[!!!]]><!--No--></notext>" + CRLF + "</html>" + CRLF, sResult);
    }
    // test as XML (without doc type and comments but indented)
    {
        final String sResult = XMLWriter.getNodeAsString(doc, new XMLWriterSettings().setSerializeDocType(EXMLSerializeDocType.IGNORE).setSerializeComments(EXMLSerializeComments.IGNORE).setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN));
        assertEquals("<?xml version=\"1.0\" encoding=\"" + StandardCharsets.UTF_8.name() + "\" standalone=\"no\"?>" + CRLF + "<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">" + CRLF + sINDENT + "<head>Hallo</head>" + CRLF + sINDENT + "<notext>Hallo <strong>Welt</strong><![CDATA[!!!]]></notext>" + CRLF + "</html>" + CRLF, sResult);
    }
    // test as XML (without doc type and comments but indented) with different
    // newline String
    {
        final String sResult = XMLWriter.getNodeAsString(doc, new XMLWriterSettings().setSerializeDocType(EXMLSerializeDocType.IGNORE).setSerializeComments(EXMLSerializeComments.IGNORE).setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN).setNewLineMode(ENewLineMode.UNIX));
        assertEquals("<?xml version=\"1.0\" encoding=\"" + StandardCharsets.UTF_8.name() + "\" standalone=\"no\"?>\n" + "<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">\n" + sINDENT + "<head>Hallo</head>\n" + sINDENT + "<notext>Hallo <strong>Welt</strong><![CDATA[!!!]]></notext>\n" + "</html>\n", sResult);
    }
    // test as XML (without doc type and comments but indented) with different
    // newline String and different indent
    {
        final String sResult = XMLWriter.getNodeAsString(doc, new XMLWriterSettings().setSerializeDocType(EXMLSerializeDocType.IGNORE).setSerializeComments(EXMLSerializeComments.IGNORE).setIndent(EXMLSerializeIndent.INDENT_AND_ALIGN).setNewLineMode(ENewLineMode.UNIX).setIndentationString("\t"));
        assertEquals("<?xml version=\"1.0\" encoding=\"" + StandardCharsets.UTF_8.name() + "\" standalone=\"no\"?>\n" + "<html xmlns=\"" + DOCTYPE_XHTML10_URI + "\">\n" + "\t<head>Hallo</head>\n" + "\t<notext>Hallo <strong>Welt</strong><![CDATA[!!!]]></notext>\n" + "</html>\n", sResult);
    }
    assertTrue(XMLWriter.writeToStream(doc, new NonBlockingByteArrayOutputStream()).isSuccess());
}
Also used : Element(org.w3c.dom.Element) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 55 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-server by phax.

the class ByteCoder method decode.

@Nonnull
public static String decode(@Nonnull final String inStr) {
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(inStr.length() / 3)) {
        final Matcher aMatcher = RegExHelper.getMatcher(".[0-9]+.", inStr);
        while (aMatcher.find()) {
            final String sMatch = aMatcher.group();
            // Ensure unsigned int
            final byte me = (byte) (Integer.parseInt(sMatch.substring(1, sMatch.length() - 1)) & 0xff);
            aBAOS.write(me);
        }
        return aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    }
}
Also used : Matcher(java.util.regex.Matcher) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Nonnull(javax.annotation.Nonnull)

Aggregations

NonBlockingByteArrayOutputStream (com.helger.commons.io.stream.NonBlockingByteArrayOutputStream)55 Test (org.junit.Test)30 IOException (java.io.IOException)12 MimeBodyPart (javax.mail.internet.MimeBodyPart)12 NonBlockingByteArrayInputStream (com.helger.commons.io.stream.NonBlockingByteArrayInputStream)8 InputStream (java.io.InputStream)7 Nonnull (javax.annotation.Nonnull)7 MessagingException (javax.mail.MessagingException)7 IMicroDocument (com.helger.xml.microdom.IMicroDocument)4 AS2Exception (com.helger.as2lib.exception.AS2Exception)3 IMessageMDN (com.helger.as2lib.message.IMessageMDN)3 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)3 ByteArrayOutputStreamProvider (com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider)3 MockMarshallerExternal (com.helger.jaxb.mock.MockMarshallerExternal)3 MockJAXBArchive (com.helger.jaxb.mock.external.MockJAXBArchive)3 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)3 Nullable (javax.annotation.Nullable)3 SMIMEException (org.bouncycastle.mail.smime.SMIMEException)3 AS2DispositionException (com.helger.as2lib.disposition.AS2DispositionException)2 WrappedAS2Exception (com.helger.as2lib.exception.WrappedAS2Exception)2