Search in sources :

Example 26 with NonBlockingByteArrayOutputStream

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

the class XMLListHandlerTest method testWriteInvalid.

@Test
public void testWriteInvalid() {
    final ICommonsList<String> aList = new CommonsArrayList<>();
    try {
        XMLListHandler.writeList(aList, (IHasOutputStream) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLListHandler.writeList(aList, (OutputStream) null);
        fail();
    } catch (final NullPointerException ex) {
    }
    try {
        XMLListHandler.writeList(null, new NonBlockingByteArrayOutputStream());
        fail();
    } catch (final NullPointerException ex) {
    }
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Test(org.junit.Test)

Example 27 with NonBlockingByteArrayOutputStream

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

the class XMLMapHandlerTest method testReadBuildInfo.

@Test
public void testReadBuildInfo() {
    final ICommonsMap<String, String> aMap = new CommonsHashMap<>();
    final IReadableResource aRes = new ClassPathResource("xml/buildinfo.xml");
    assertTrue(XMLMapHandler.readMap(aRes, aMap).isSuccess());
    assertNull(XMLMapHandler.readMap(new ClassPathResource("test1.txt")));
    assertTrue(aMap.containsKey("buildinfo.version"));
    assertEquals("1", aMap.get("buildinfo.version"));
    assertTrue(XMLMapHandler.readMap(aRes).containsKey("buildinfo.version"));
    assertEquals("1", XMLMapHandler.readMap(aRes).get("buildinfo.version"));
    assertTrue(XMLMapHandler.writeMap(aMap, new ByteArrayOutputStreamProvider()).isSuccess());
    assertTrue(XMLMapHandler.writeMap(aMap, new NonBlockingByteArrayOutputStream()).isSuccess());
}
Also used : ByteArrayOutputStreamProvider(com.helger.commons.io.streamprovider.ByteArrayOutputStreamProvider) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) IReadableResource(com.helger.commons.io.resource.IReadableResource) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 28 with NonBlockingByteArrayOutputStream

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

the class ClassPathHelperTest method testPrintClassPathEntries.

/**
 * Test for method printClassPathEntries
 *
 * @throws UnsupportedEncodingException
 *         never
 */
@Test
public void testPrintClassPathEntries() throws UnsupportedEncodingException {
    // Use default separator
    NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
    ClassPathHelper.printClassPathEntries(new PrintStream(baos, false, StandardCharsets.ISO_8859_1.name()));
    assertTrue(baos.getAsString(StandardCharsets.ISO_8859_1).length() > 0);
    assertTrue(baos.getAsString(StandardCharsets.ISO_8859_1).indexOf("\n") > 0);
    StreamHelper.close(baos);
    // Use special separator
    baos = new NonBlockingByteArrayOutputStream();
    ClassPathHelper.printClassPathEntries(new PrintStream(baos, false, StandardCharsets.ISO_8859_1.name()), "$$$");
    assertTrue(baos.getAsString(StandardCharsets.ISO_8859_1).length() > 0);
    assertTrue(baos.getAsString(StandardCharsets.ISO_8859_1).indexOf("$$$") > 0);
    assertTrue(baos.getAsString(StandardCharsets.UTF_8).indexOf("$$$") > 0);
    StreamHelper.close(baos);
}
Also used : PrintStream(java.io.PrintStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Test(org.junit.Test)

Example 29 with NonBlockingByteArrayOutputStream

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

the class JAXBMarshallerHelperTest method testCloseInternalOnWriteToOutputStream.

@Test
public void testCloseInternalOnWriteToOutputStream() {
    final MockMarshallerExternal m = new MockMarshallerExternal();
    final MutableBoolean aClosed = new MutableBoolean(false);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream() {

        @Override
        public void close() {
            super.close();
            aClosed.set(true);
        }
    };
    {
        final MockJAXBArchive aArc = new MockJAXBArchive();
        aArc.setVersion("1.24");
        m.write(aArc, aBAOS);
    }
    LOGGER.info(aBAOS.getAsString(StandardCharsets.UTF_8));
    // Must be closed!
    assertTrue("Not closed!", aClosed.booleanValue());
}
Also used : 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 30 with NonBlockingByteArrayOutputStream

use of com.helger.commons.io.stream.NonBlockingByteArrayOutputStream in project as2-lib 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