Search in sources :

Example 1 with IOContext

use of com.fasterxml.jackson.core.io.IOContext 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 IOContext

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

the class TextualTSFactory method createParser.

@Override
public JsonParser createParser(ObjectReadContext readCtxt, String content) throws IOException {
    final int strLen = content.length();
    // Actually, let's use this for medium-sized content, up to 64kB chunk (32kb char)
    if ((_inputDecorator != null) || (strLen > 0x8000) || !canUseCharArrays()) {
        // is too long for us to copy it over
        return createParser(readCtxt, new StringReader(content));
    }
    IOContext ioCtxt = _createContext(content, true);
    char[] buf = ioCtxt.allocTokenBuffer(strLen);
    content.getChars(0, strLen, buf, 0);
    return _createParser(readCtxt, ioCtxt, buf, 0, strLen, true);
}
Also used : IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 3 with IOContext

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

the class BinaryTSFactory method createParser.

/*
    /**********************************************************
    /* Factory methods: parsers
    /**********************************************************
     */
@Override
public JsonParser createParser(ObjectReadContext readCtxt, File f) throws IOException {
    // true, since we create InputStream from File
    IOContext ioCtxt = _createContext(f, true);
    InputStream in = new FileInputStream(f);
    return _createParser(readCtxt, ioCtxt, _decorate(ioCtxt, in));
}
Also used : IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 4 with IOContext

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

the class UTF8DataInputJsonParserTest method test_handleAposThrowsIOException.

@Test
public void test_handleAposThrowsIOException() {
    IOContext ioContext = new IOContext(new BufferRecycler(), this, false);
    byte[] byteArray = new byte[7];
    byteArray[0] = (byte) (-80);
    InputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
    ByteQuadsCanonicalizer byteQuadsCanonicalizer = ByteQuadsCanonicalizer.createRoot();
    UTF8DataInputJsonParser uTF8DataInputJsonParser = new UTF8DataInputJsonParser(ObjectReadContext.empty(), ioContext, 3, dataInputStream, byteQuadsCanonicalizer, 1);
    try {
        uTF8DataInputJsonParser._handleApos();
        fail("Expecting exception: IOException");
    } catch (IOException e) {
        assertEquals(JsonParser.class.getName(), e.getStackTrace()[0].getClassName());
    }
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) 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 5 with IOContext

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

the class UTF8DataInputJsonParserTest method testGetValueAsIntTakingInt.

@Test
public void testGetValueAsIntTakingInt() throws IOException {
    byte[] byteArray = new byte[5];
    IOContext ioContext = new IOContext(new BufferRecycler(), byteArray, false);
    InputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
    DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
    UTF8DataInputJsonParser uTF8DataInputJsonParser = new UTF8DataInputJsonParser(ObjectReadContext.empty(), ioContext, 500, dataInputStream, ByteQuadsCanonicalizer.createRoot(), 1);
    assertEquals(466, uTF8DataInputJsonParser.getValueAsInt(466));
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext) BaseTest(com.fasterxml.jackson.core.BaseTest) Test(org.junit.Test)

Aggregations

IOContext (com.fasterxml.jackson.core.io.IOContext)41 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)28 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 BsonBinaryReader (org.bson.BsonBinaryReader)5 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)5 BsonWriter (org.bson.BsonWriter)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 InputStream (java.io.InputStream)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 ReaderBasedJsonParser (com.fasterxml.jackson.core.json.ReaderBasedJsonParser)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 UnsafeByteArrayInputStream (de.undercouch.bson4jackson.io.UnsafeByteArrayInputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 BsonDocument (org.bson.BsonDocument)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1