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