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