Search in sources :

Example 36 with NonBlockingByteArrayOutputStream

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

the class TempSharedFileInputStreamTest method testGetTempSharedFileInputStream.

@Test
public void testGetTempSharedFileInputStream() throws Exception {
    final String inData = "123456";
    try (final InputStream is = new NonBlockingByteArrayInputStream(inData.getBytes());
        final SharedFileInputStream sis = TempSharedFileInputStream.getTempSharedFileInputStream(is, "myName");
        final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream()) {
        StreamHelper.copyInputStreamToOutputStream(sis, baos);
        final String res = baos.getAsString(StandardCharsets.ISO_8859_1);
        assertEquals("read the data", inData, res);
        sis.close();
    }
}
Also used : NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) SharedFileInputStream(javax.mail.util.SharedFileInputStream) SharedFileInputStream(javax.mail.util.SharedFileInputStream) NonBlockingByteArrayInputStream(com.helger.commons.io.stream.NonBlockingByteArrayInputStream) InputStream(java.io.InputStream) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Test(org.junit.Test)

Example 37 with NonBlockingByteArrayOutputStream

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

the class BCCryptoHelperTest method testSign_Binary.

@Test
public void testSign_Binary() throws Exception {
    final MimeBodyPart aPart = new MimeBodyPart();
    aPart.setText("Hello world", StandardCharsets.ISO_8859_1.name());
    final MimeBodyPart aSigned = AS2Helper.getCryptoHelper().sign(aPart, (X509Certificate) PKE.getCertificate(), PKE.getPrivateKey(), ECryptoAlgorithmSign.DIGEST_SHA_256, false, false, false, EContentTransferEncoding.BINARY);
    assertNotNull(aSigned);
    final String sBoundary = AS2HttpHelper.parseContentType(aSigned.getContentType()).getParameter("boundary");
    assertNotNull(sBoundary);
    final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream();
    aSigned.writeTo(aBAOS);
    final String sExpectedStart = "Content-Type: multipart/signed; protocol=\"application/pkcs7-signature\"; micalg=sha-256; \r\n" + "\tboundary=\"" + sBoundary + "\"\r\n" + "\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "Hello world\r\n" + "--" + sBoundary + "\r\n" + "Content-Type: application/pkcs7-signature; name=smime.p7s; smime-type=signed-data\r\n" + "Content-Transfer-Encoding: binary\r\n" + "Content-Disposition: attachment; filename=\"smime.p7s\"\r\n" + "Content-Description: S/MIME Cryptographic Signature\r\n" + "\r\n";
    final String sExpectedEnd = "\r\n" + "--" + sBoundary + "--\r\n";
    final String sReal = aBAOS.getAsString(StandardCharsets.ISO_8859_1);
    assertTrue(sReal.startsWith(sExpectedStart));
    assertTrue(sReal.endsWith(sExpectedEnd));
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) Test(org.junit.Test)

Example 38 with NonBlockingByteArrayOutputStream

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

the class HttpClientHelper method createParameterEntity.

@Nullable
public static HttpEntity createParameterEntity(@Nullable final Map<String, String> aMap, @Nonnull final Charset aCharset) {
    ValueEnforcer.notNull(aCharset, "Charset");
    if (aMap == null || aMap.isEmpty())
        return null;
    try (final NonBlockingByteArrayOutputStream aBAOS = new NonBlockingByteArrayOutputStream(1024)) {
        final URLCodec aURLCodec = new URLCodec();
        boolean bFirst = true;
        for (final Map.Entry<String, String> aEntry : aMap.entrySet()) {
            if (bFirst)
                bFirst = false;
            else
                aBAOS.write('&');
            // Key must be present
            final String sKey = aEntry.getKey();
            aURLCodec.encode(sKey.getBytes(aCharset), aBAOS);
            // Value is optional
            final String sValue = aEntry.getValue();
            if (StringHelper.hasText(sValue)) {
                aBAOS.write('=');
                aURLCodec.encode(sValue.getBytes(aCharset), aBAOS);
            }
        }
        return new InputStreamEntity(aBAOS.getAsInputStream());
    }
}
Also used : URLCodec(com.helger.commons.codec.URLCodec) NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) Map(java.util.Map) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Nullable(javax.annotation.Nullable)

Example 39 with NonBlockingByteArrayOutputStream

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

the class StreamingFuncTest method _newShortRequest.

private static byte[] _newShortRequest() throws IOException {
    try (final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream();
        final OutputStreamWriter osw = new OutputStreamWriter(baos, StandardCharsets.US_ASCII)) {
        osw.write(_getHeader("field"));
        osw.write("123");
        osw.write("\r\n");
        osw.write(_getFooter());
        osw.flush();
        return baos.toByteArray();
    }
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) OutputStreamWriter(java.io.OutputStreamWriter)

Example 40 with NonBlockingByteArrayOutputStream

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

the class SizesFuncTest method testFileUpload.

/**
 * Runs a test with varying file sizes.
 *
 * @throws IOException
 *         In case of error
 * @throws FileUploadException
 *         In case of error
 */
@Test
public void testFileUpload() throws IOException, FileUploadException {
    try (final NonBlockingByteArrayOutputStream baos = new NonBlockingByteArrayOutputStream()) {
        int add = 16;
        int num = 0;
        for (int i = 0; i < 16384; i += add) {
            if (++add == 32) {
                add = 16;
            }
            final String header = "-----1234\r\n" + "Content-Disposition: form-data; name=\"field" + (num++) + "\"\r\n" + "\r\n";
            baos.write(header.getBytes(StandardCharsets.US_ASCII));
            for (int j = 0; j < i; j++) {
                baos.write((byte) j);
            }
            baos.write("\r\n".getBytes(StandardCharsets.US_ASCII));
        }
        baos.write("-----1234--\r\n".getBytes(StandardCharsets.US_ASCII));
        final ICommonsList<IFileItem> fileItems = parseUpload(baos.toByteArray());
        final Iterator<IFileItem> fileIter = fileItems.iterator();
        add = 16;
        num = 0;
        for (int i = 0; i < 16384; i += add) {
            if (++add == 32) {
                add = 16;
            }
            final IFileItem item = fileIter.next();
            assertEquals("field" + (num++), item.getFieldName());
            final byte[] bytes = item.directGet();
            assertEquals(i, bytes.length);
            for (int j = 0; j < i; j++) {
                assertEquals((byte) j, bytes[j]);
            }
        }
        assertFalse(fileIter.hasNext());
    }
}
Also used : NonBlockingByteArrayOutputStream(com.helger.commons.io.stream.NonBlockingByteArrayOutputStream) IFileItem(com.helger.web.fileupload.IFileItem) 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