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();
}
}
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));
}
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());
}
}
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();
}
}
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());
}
}
Aggregations