Search in sources :

Example 11 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project jackson-core by FasterXML.

the class UTF8DataInputJsonParserTest method testParseEscapedNameThrowsArrayIndexOutOfBoundsException.

@Test
public void testParseEscapedNameThrowsArrayIndexOutOfBoundsException() throws IOException {
    IOContext ioContext = new IOContext((BufferRecycler) null, null, false);
    PipedOutputStream pipedOutputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream, 131);
    DataInputStream dataInputStream = new DataInputStream(pipedInputStream);
    ByteQuadsCanonicalizer byteQuadsCanonicalizer = ByteQuadsCanonicalizer.createRoot();
    UTF8DataInputJsonParser uTF8DataInputJsonParser = new UTF8DataInputJsonParser(ObjectReadContext.empty(), ioContext, 131, dataInputStream, byteQuadsCanonicalizer, (byte) 57);
    int[] intArray = new int[3];
    try {
        uTF8DataInputJsonParser.parseEscapedName(intArray, 56, (byte) 72, (byte) 127, (byte) 57);
        fail("Expecting exception: ArrayIndexOutOfBoundsException");
    } catch (ArrayIndexOutOfBoundsException e) {
        assertEquals(UTF8DataInputJsonParser.class.getName(), e.getStackTrace()[0].getClassName());
    }
}
Also used : IOContext(com.fasterxml.jackson.core.io.IOContext) ByteQuadsCanonicalizer(com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Example 12 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project jackson-core by FasterXML.

the class UTF8WriterTest method testSimple.

public void testSimple() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt = new IOContext(rec, null, false);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    UTF8Writer w = new UTF8Writer(ctxt, out);
    String str = "AB\u00A0\u1AE9\uFFFC";
    char[] ch = str.toCharArray();
    // Let's write 3 times, using different methods
    w.write(str);
    w.append(ch[0]);
    w.write(ch[1]);
    w.write(ch, 2, 3);
    w.flush();
    w.write(str, 0, str.length());
    w.close();
    // and thus should have 3 times contents
    byte[] data = out.toByteArray();
    assertEquals(3 * 10, data.length);
    String act = out.toString("UTF-8");
    assertEquals(15, act.length());
    assertEquals(3 * str.length(), act.length());
    assertEquals(str + str + str, act);
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 13 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project jackson-core by FasterXML.

the class UTF8WriterTest method testSimpleAscii.

public void testSimpleAscii() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt = new IOContext(rec, null, false);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    UTF8Writer w = new UTF8Writer(ctxt, out);
    String str = "abcdefghijklmnopqrst\u00A0";
    char[] ch = str.toCharArray();
    w.write(ch, 0, ch.length);
    // trigger different code path for close
    w.flush();
    w.close();
    byte[] data = out.toByteArray();
    // one 2-byte encoded char
    assertEquals(ch.length + 1, data.length);
    String act = out.toString("UTF-8");
    assertEquals(str, act);
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 14 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project jackson-core by FasterXML.

the class UTF8WriterTest method testSurrogatesOk.

public void testSurrogatesOk() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt = new IOContext(rec, null, false);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    UTF8Writer w = new UTF8Writer(ctxt, out);
    // First, valid case, char by char
    w.write(0xD83D);
    w.write(0xDE03);
    w.close();
    assertEquals(4, out.size());
    final byte[] EXP_SURROGATES = new byte[] { (byte) 0xF0, (byte) 0x9F, (byte) 0x98, (byte) 0x83 };
    Assert.assertArrayEquals(EXP_SURROGATES, out.toByteArray());
    // and then as String
    ctxt = new IOContext(rec, null, false);
    out = new ByteArrayOutputStream();
    w = new UTF8Writer(ctxt, out);
    w.write("\uD83D\uDE03");
    w.close();
    assertEquals(4, out.size());
    Assert.assertArrayEquals(EXP_SURROGATES, out.toByteArray());
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 15 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project jackson-core by FasterXML.

the class UTF8WriterTest method testSurrogatesFail.

@SuppressWarnings("resource")
public void testSurrogatesFail() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt;
    ByteArrayOutputStream out;
    UTF8Writer w;
    ctxt = new IOContext(rec, null, false);
    out = new ByteArrayOutputStream();
    w = new UTF8Writer(ctxt, out);
    try {
        w.write(0xDE03);
        fail("should not pass");
    } catch (IOException e) {
        verifyException(e, "Unmatched second part");
    }
    ctxt = new IOContext(rec, null, false);
    out = new ByteArrayOutputStream();
    w = new UTF8Writer(ctxt, out);
    w.write(0xD83D);
    try {
        w.write('a');
        fail("should not pass");
    } catch (IOException e) {
        verifyException(e, "Broken surrogate pair");
    }
    ctxt = new IOContext(rec, null, false);
    out = new ByteArrayOutputStream();
    w = new UTF8Writer(ctxt, out);
    try {
        w.write("\uDE03");
        fail("should not pass");
    } catch (IOException e) {
        verifyException(e, "Unmatched second part");
    }
    ctxt = new IOContext(rec, null, false);
    out = new ByteArrayOutputStream();
    w = new UTF8Writer(ctxt, out);
    try {
        w.write("\uD83Da");
        fail("should not pass");
    } catch (IOException e) {
        verifyException(e, "Broken surrogate pair");
    }
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Aggregations

IOContext (com.fasterxml.jackson.core.io.IOContext)35 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)23 BaseTest (com.fasterxml.jackson.core.BaseTest)18 Test (org.junit.Test)18 ByteQuadsCanonicalizer (com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer)8 UTF8Writer (com.fasterxml.jackson.core.io.UTF8Writer)5 InputStream (java.io.InputStream)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 ReaderBasedJsonParser (com.fasterxml.jackson.core.json.ReaderBasedJsonParser)2 UnsafeByteArrayInputStream (de.undercouch.bson4jackson.io.UnsafeByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 ObjectCodec (com.fasterxml.jackson.core.ObjectCodec)1 MergedStream (com.fasterxml.jackson.core.io.MergedStream)1 UTF8StreamJsonParser (com.fasterxml.jackson.core.json.UTF8StreamJsonParser)1 ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1