Search in sources :

Example 21 with NonBlockingByteArrayOutputStream

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

the class Base64 method decode.

/**
 * Decodes data from Base64 notation, automatically detecting gzip-compressed
 * data and decompressing it.
 *
 * @param s
 *        the string to decode
 * @param options
 *        encode options such as URL_SAFE
 * @return the decoded data
 * @throws IOException
 *         if there is an error
 * @throws NullPointerException
 *         if <code>s</code> is null
 * @since 1.4
 */
@Nonnull
@ReturnsMutableCopy
public static byte[] decode(@Nonnull final String s, final int options) throws IOException {
    ValueEnforcer.notNull(s, "InputString");
    byte[] bytes = s.getBytes(PREFERRED_ENCODING);
    // Decode
    bytes = decode(bytes, 0, bytes.length, options);
    // Check to see if it's gzip-compressed
    // GZIP Magic Two-Byte Number: 0x8b1f (35615)
    final boolean dontGunzip = (options & DONT_GUNZIP) != 0;
    if (bytes.length >= 4 && !dontGunzip) {
        final int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        if (GZIPInputStream.GZIP_MAGIC == head) {
            try (final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
                final NonBlockingByteArrayInputStream bais = new NonBlockingByteArrayInputStream(bytes);
                final GZIPInputStream gzis = new GZIPInputStream(bais)) {
                final byte[] buffer = new byte[2048];
                int length;
                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                }
                // No error? Get new bytes.
                bytes = baos.toByteArray();
            } catch (final IOException e) {
            // Just return originally-decoded bytes
            }
        }
    }
    return bytes;
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IOException(java.io.IOException) ReturnsMutableCopy(com.helger.commons.annotation.ReturnsMutableCopy) Nonnull(javax.annotation.Nonnull)

Example 22 with NonBlockingByteArrayOutputStream

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

the class MicroReaderTest method testNamespaces3.

/**
 * Test: declare all namespaces in the root element and use them in nested
 * elements
 */
@Test
public void testNamespaces3() {
    final XMLWriterSettings xs = new XMLWriterSettings();
    xs.setIndent(EXMLSerializeIndent.NONE);
    xs.setUseDoubleQuotesForAttributes(false);
    final String s = "<?xml version=\"1.0\"?>" + "<verrryoot xmlns='uri1' xmlns:a='uri2'>" + "<root>" + "<a:child>" + "<a:child2>Value text - no entities!</a:child2>" + "</a:child>" + "</root>" + "</verrryoot>";
    final IMicroDocument aDoc = MicroReader.readMicroXML(s);
    assertNotNull(aDoc);
    final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    new MicroSerializer(xs).write(aDoc, baos);
    final String sXML = baos.getAsString(StandardCharsets.UTF_8);
    assertEquals("<?xml version='1.0' encoding='UTF-8'?>" + "<verrryoot xmlns='uri1'>" + "<root>" + "<ns0:child xmlns:ns0='uri2'>" + "<ns0:child2>Value text - no entities!</ns0:child2>" + "</ns0:child>" + "</root>" + "</verrryoot>", sXML);
}
Also used : XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Example 23 with NonBlockingByteArrayOutputStream

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

the class MicroWriterTest method testSaveToStream.

@Test
public void testSaveToStream() {
    try {
        MicroWriter.writeToStream(null, new NonBlockingByteArrayOutputStream(), XMLWriterSettings.DEFAULT_XML_SETTINGS);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        MicroWriter.writeToStream(new MicroDocument(), null, XMLWriterSettings.DEFAULT_XML_SETTINGS);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        MicroWriter.writeToStream(new MicroDocument(), new NonBlockingByteArrayOutputStream(), null);
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Test(org.junit.Test)

Example 24 with NonBlockingByteArrayOutputStream

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

the class MicroReaderTest method testNamespaces.

/**
 * Test: use namespaces all over the place and mix them quite complex
 */
@Test
public void testNamespaces() {
    final XMLWriterSettings xs = new XMLWriterSettings();
    xs.setIndent(EXMLSerializeIndent.NONE);
    final String s = "<?xml version=\"1.0\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\" xmlns:a='foo'>" + "<child xmlns=\"\">" + "<a:child2>Value text - no entities!</a:child2>" + "</child>" + "</root>" + "</verrryoot>";
    final IMicroDocument aDoc = MicroReader.readMicroXML(s);
    assertNotNull(aDoc);
    final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    new MicroSerializer(xs).write(aDoc, baos);
    final String sXML = baos.getAsString(StandardCharsets.UTF_8);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\">" + "<ns0:child xmlns:ns0=\"\">" + "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" + "</ns0:child>" + "</root>" + "</verrryoot>", sXML);
}
Also used : XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

Example 25 with NonBlockingByteArrayOutputStream

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

the class MicroReaderTest method testSimple.

@Test
public void testSimple() {
    final String s = "<?xml version=\"1.0\"?>" + "<verrryoot>" + "<root xmlns=\"myuri\">" + "<child xmlns=\"\">" + "<a:child2 xmlns:a=\"foo\">Value text - no entities!</a:child2>" + "</child>" + "</root>" + "</verrryoot>";
    IMicroDocument aDoc = MicroReader.readMicroXML(s);
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringSAXInputSource(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingStringReader(s));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new StringInputStreamProvider(s, StandardCharsets.ISO_8859_1));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(new NonBlockingByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1)));
    assertNotNull(aDoc);
    aDoc = MicroReader.readMicroXML(s, new SAXReaderSettings().setErrorHandler(new LoggingSAXErrorHandler()));
    assertNotNull(aDoc);
    final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    new MicroSerializer().write(aDoc, baos);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + CRLF + "<verrryoot>" + CRLF + INDENT + "<root xmlns=\"myuri\">" + CRLF + INDENT + INDENT + "<ns0:child xmlns:ns0=\"\">" + CRLF + INDENT + INDENT + INDENT + "<ns1:child2 xmlns:ns1=\"foo\">Value text - no entities!</ns1:child2>" + CRLF + INDENT + INDENT + "</ns0:child>" + CRLF + INDENT + "</root>" + CRLF + "</verrryoot>" + CRLF, baos.getAsString(StandardCharsets.UTF_8));
    final String sXHTML = "<content>" + "<div class=\"css1\">" + "<span class=\"css2\">" + "<span>Text1 " + "<span>Text1b</span>" + "</span>" + " " + "<span>Text1c</span>" + "<span class=\"css3\">" + "<span>Text2</span>" + "</span>" + "</span>" + "</div>" + "</content>";
    final IMicroDocument docXHTML = MicroReader.readMicroXML(new NonBlockingStringReader(sXHTML));
    assertNotNull(docXHTML);
    final String sResult = MicroWriter.getNodeAsString(docXHTML, new XMLWriterSettings().setIndent(EXMLSerializeIndent.NONE));
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<content>" + "<div class=\"css1\">" + "<span class=\"css2\"><span>Text1 <span>Text1b</span></span> <span>Text1c</span>" + "<span class=\"css3\"><span>Text2</span></span>" + "</span>" + "</div>" + "</content>", sResult);
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) NonBlockingStringReader(com.helger.commons.io.stream.NonBlockingStringReader) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) StringSAXInputSource(com.helger.xml.sax.StringSAXInputSource) LoggingSAXErrorHandler(com.helger.xml.sax.LoggingSAXErrorHandler) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) StringInputStreamProvider(com.helger.commons.io.streamprovider.StringInputStreamProvider) ISAXReaderSettings(com.helger.xml.serialize.read.ISAXReaderSettings) SAXReaderSettings(com.helger.xml.serialize.read.SAXReaderSettings) IMicroDocument(com.helger.xml.microdom.IMicroDocument) Test(org.junit.Test)

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