public WriterBasedGenerator(IOContext ctxt, int features, Writer w) { super(features); _ioContext = ctxt; _writer = w; _outputBuffer = ctxt.allocConcatBuffer(); _outputEnd = _outputBuffer.length; }
@Override protected void _releaseBuffers() { char[] buf = _outputBuffer; if (buf != null) { _outputBuffer = null; _ioContext.releaseConcatBuffer(buf); } }
private void freeMergedBuffer() { byte[] buf = _buffer; if (buf != null) { _buffer = null; if (_context != null) { _context.releaseReadIOBuffer(buf); } } }
/** * Method for constructing JSON generator for writing JSON content using specified output stream. * Encoding to use must be specified, and needs to be one of available types (as per JSON * specification). * * <p>Underlying stream <b>is NOT owned</b> by the generator constructed, so that generator will * NOT close the output stream when {@link JsonGenerator#close} is called (unless auto-closing * feature, {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is * enabled). Using application needs to close it explicitly if this is the case. * * <p>Note: there are formats that use fixed encoding (like most binary data formats) and that * ignore passed in encoding. * * @param out OutputStream to use for writing JSON content * @param enc Character encoding to use * @since 2.1 */ public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException { // false -> we won't manage the stream unless explicitly directed to IOContext ctxt = _createContext(out, false); ctxt.setEncoding(enc); if (enc == JsonEncoding.UTF8) { // [JACKSON-512]: allow wrapping with _outputDecorator if (_outputDecorator != null) { out = _outputDecorator.decorate(ctxt, out); } return _createUTF8Generator(out, ctxt); } Writer w = _createWriter(out, enc, ctxt); // [JACKSON-512]: allow wrapping with _outputDecorator if (_outputDecorator != null) { w = _outputDecorator.decorate(ctxt, w); } return _createGenerator(w, ctxt); }
/** * Method for constructing JSON generator for writing JSON content to specified file, overwriting * contents it might have (or creating it if such file does not yet exist). Encoding to use must * be specified, and needs to be one of available types (as per JSON specification). * * <p>Underlying stream <b>is owned</b> by the generator constructed, i.e. generator will handle * closing of file when {@link JsonGenerator#close} is called. * * @param f File to write contents to * @param enc Character encoding to use * @since 2.1 */ public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException { OutputStream out = new FileOutputStream(f); // true -> yes, we have to manage the stream since we created it IOContext ctxt = _createContext(out, true); ctxt.setEncoding(enc); if (enc == JsonEncoding.UTF8) { // [JACKSON-512]: allow wrapping with _outputDecorator if (_outputDecorator != null) { out = _outputDecorator.decorate(ctxt, out); } return _createUTF8Generator(out, ctxt); } Writer w = _createWriter(out, enc, ctxt); // [JACKSON-512]: allow wrapping with _outputDecorator if (_outputDecorator != null) { w = _outputDecorator.decorate(ctxt, w); } return _createGenerator(w, ctxt); }
@Override public void close() throws IOException { _flushBuffer(); /* 25-Nov-2008, tatus: As per [JACKSON-16] we are not to call close() * on the underlying Reader, unless we "own" it, or auto-closing * feature is enabled. * One downside: when using UTF8Writer, underlying buffer(s) * may not be properly recycled if we don't close the writer. */ if (_ioContext.isResourceManaged() || isFeatureEnabled(Feature.AUTO_CLOSE_TARGET)) { _writer.close(); } else { // If we can't close it, we should at least flush _writer.flush(); } // Internal buffer(s) generator has can now be released as well _releaseBuffers(); }