Search in sources :

Example 16 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project bson4jackson by michel-kraemer.

the class BsonFactory method createGenerator.

@Override
public BsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException {
    IOContext ctxt = _createContext(out, true);
    ctxt.setEncoding(enc);
    if (enc == JsonEncoding.UTF8 && _outputDecorator != null) {
        out = _outputDecorator.decorate(ctxt, out);
    }
    BsonGenerator g = new BsonGenerator(_generatorFeatures, _bsonGeneratorFeatures, out);
    ObjectCodec codec = getCodec();
    if (codec != null) {
        g.setCodec(codec);
    }
    if (_characterEscapes != null) {
        g.setCharacterEscapes(_characterEscapes);
    }
    return g;
}
Also used : IOContext(com.fasterxml.jackson.core.io.IOContext) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec)

Example 17 with IOContext

use of com.fasterxml.jackson.core.io.IOContext in project bson4jackson by michel-kraemer.

the class BsonFactory method createParser.

@SuppressWarnings("resource")
@Override
public BsonParser createParser(File f) throws IOException {
    IOContext ctxt = _createContext(f, true);
    InputStream in = new FileInputStream(f);
    if (_inputDecorator != null) {
        in = _inputDecorator.decorate(ctxt, in);
    }
    return _createParser(in, ctxt);
}
Also used : FileInputStream(java.io.FileInputStream) UnsafeByteArrayInputStream(de.undercouch.bson4jackson.io.UnsafeByteArrayInputStream) InputStream(java.io.InputStream) IOContext(com.fasterxml.jackson.core.io.IOContext) FileInputStream(java.io.FileInputStream)

Example 18 with IOContext

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

the class TestIOContext method testAllocations.

public void testAllocations() throws Exception {
    IOContext ctxt = new IOContext(new BufferRecycler(), "N/A", true);
    /* I/O Read buffer */
    // First succeeds:
    assertNotNull(ctxt.allocReadIOBuffer());
    // second fails
    try {
        ctxt.allocReadIOBuffer();
    } catch (IllegalStateException e) {
        verifyException(e, "second time");
    }
    // Also: can't succeed with different buffer
    try {
        ctxt.releaseReadIOBuffer(new byte[1]);
    } catch (IllegalArgumentException e) {
        verifyException(e, "smaller than original");
    }
    // but call with null is a NOP for convenience
    ctxt.releaseReadIOBuffer(null);
    /* I/O Write buffer */
    assertNotNull(ctxt.allocWriteEncodingBuffer());
    try {
        ctxt.allocWriteEncodingBuffer();
    } catch (IllegalStateException e) {
        verifyException(e, "second time");
    }
    try {
        ctxt.releaseWriteEncodingBuffer(new byte[1]);
    } catch (IllegalArgumentException e) {
        verifyException(e, "smaller than original");
    }
    ctxt.releaseWriteEncodingBuffer(null);
    /* Token (read) buffer */
    assertNotNull(ctxt.allocTokenBuffer());
    try {
        ctxt.allocTokenBuffer();
    } catch (IllegalStateException e) {
        verifyException(e, "second time");
    }
    try {
        ctxt.releaseTokenBuffer(new char[1]);
    } catch (IllegalArgumentException e) {
        verifyException(e, "smaller than original");
    }
    ctxt.releaseTokenBuffer(null);
    /* Concat (write?) buffer */
    assertNotNull(ctxt.allocConcatBuffer());
    try {
        ctxt.allocConcatBuffer();
    } catch (IllegalStateException e) {
        verifyException(e, "second time");
    }
    try {
        ctxt.releaseConcatBuffer(new char[1]);
    } catch (IllegalArgumentException e) {
        verifyException(e, "smaller than original");
    }
    ctxt.releaseConcatBuffer(null);
    /* NameCopy (write?) buffer */
    assertNotNull(ctxt.allocNameCopyBuffer(100));
    try {
        ctxt.allocNameCopyBuffer(100);
    } catch (IllegalStateException e) {
        verifyException(e, "second time");
    }
    try {
        ctxt.releaseNameCopyBuffer(new char[1]);
    } catch (IllegalArgumentException e) {
        verifyException(e, "smaller than original");
    }
    ctxt.releaseNameCopyBuffer(null);
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 19 with IOContext

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

the class UTF8WriterTest method testFlushAfterClose.

public void testFlushAfterClose() throws Exception {
    BufferRecycler rec = new BufferRecycler();
    IOContext ctxt = new IOContext(rec, null, false);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    UTF8Writer w = new UTF8Writer(ctxt, out);
    w.write('X');
    char[] ch = { 'Y' };
    w.write(ch);
    w.close();
    assertEquals(2, out.size());
    // and this ought to be fine...
    w.flush();
    // as well as some more...
    w.close();
    w.flush();
}
Also used : UTF8Writer(com.fasterxml.jackson.core.io.UTF8Writer) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext)

Example 20 with IOContext

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

the class TestUtf8Generator method testUtf8Issue462.

public void testUtf8Issue462() throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    IOContext ioc = new IOContext(new BufferRecycler(), bytes, true);
    JsonGenerator gen = new UTF8JsonGenerator(ObjectWriteContext.empty(), ioc, 0, bytes, JsonFactory.DEFAULT_ROOT_VALUE_SEPARATOR, null, null);
    String str = "Natuurlijk is alles gelukt en weer een tevreden klant\uD83D\uDE04";
    int length = 4000 - 38;
    for (int i = 1; i <= length; ++i) {
        gen.writeNumber(1);
    }
    gen.writeString(str);
    gen.flush();
    gen.close();
    // Also verify it's parsable?
    JsonParser p = JSON_F.createParser(ObjectReadContext.empty(), bytes.toByteArray());
    for (int i = 1; i <= length; ++i) {
        assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
        assertEquals(1, p.getIntValue());
    }
    assertToken(JsonToken.VALUE_STRING, p.nextToken());
    assertEquals(str, p.getText());
    assertNull(p.nextToken());
    p.close();
}
Also used : BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) IOContext(com.fasterxml.jackson.core.io.IOContext) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParser(com.fasterxml.jackson.core.JsonParser)

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