Search in sources :

Example 1 with BufferRecycler

use of com.fasterxml.jackson.core.util.BufferRecycler in project jackson-core by FasterXML.

the class TestMergedStream method testSimple.

public void testSimple() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt = new IOContext(rec, null, false);
    // bit complicated; must use recyclable buffer...
    byte[] first = ctxt.allocReadIOBuffer();
    System.arraycopy("ABCDE".getBytes("UTF-8"), 0, first, 99, 5);
    byte[] second = "FGHIJ".getBytes("UTF-8");
    assertNull(ctxt.getSourceReference());
    assertFalse(ctxt.isResourceManaged());
    ctxt.setEncoding(JsonEncoding.UTF8);
    MergedStream ms = new MergedStream(ctxt, new ByteArrayInputStream(second), first, 99, 99 + 5);
    // Ok, first, should have 5 bytes from first buffer:
    assertEquals(5, ms.available());
    // not supported when there's buffered stuff...
    assertFalse(ms.markSupported());
    // so this won't work, but shouldn't throw exception
    ms.mark(1);
    assertEquals((byte) 'A', ms.read());
    assertEquals(3, ms.skip(3));
    byte[] buffer = new byte[5];
    /* Ok, now, code is allowed to return anywhere between 1 and 3,
         * but we now it will return 1...
         */
    assertEquals(1, ms.read(buffer, 1, 3));
    assertEquals((byte) 'E', buffer[1]);
    // So let's read bit more
    assertEquals(3, ms.read(buffer, 0, 3));
    assertEquals((byte) 'F', buffer[0]);
    assertEquals((byte) 'G', buffer[1]);
    assertEquals((byte) 'H', buffer[2]);
    assertEquals(2, ms.available());
    // And then skip the reset
    assertEquals(2, ms.skip(200));
    ms.close();
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext) MergedStream(com.fasterxml.jackson.core.io.MergedStream)

Example 2 with BufferRecycler

use of com.fasterxml.jackson.core.util.BufferRecycler 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("�");
        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("�a");
        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)

Example 3 with BufferRecycler

use of com.fasterxml.jackson.core.util.BufferRecycler 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 ";
    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 4 with BufferRecycler

use of com.fasterxml.jackson.core.util.BufferRecycler 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("😃");
    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 5 with BufferRecycler

use of com.fasterxml.jackson.core.util.BufferRecycler 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 ᫩";
    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)

Aggregations

BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)10 IOContext (com.fasterxml.jackson.core.io.IOContext)8 UTF8Writer (com.fasterxml.jackson.core.io.UTF8Writer)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 MergedStream (com.fasterxml.jackson.core.io.MergedStream)1 SegmentedStringWriter (com.fasterxml.jackson.core.io.SegmentedStringWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1