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