Skip to content

Commit

Permalink
Flush writer after each emit
Browse files Browse the repository at this point in the history
OutputStreamWriter from JDK buffers outgoing bytes with a 8k
buffer, which causes some small document emits to never make it
into the outgoing stream unless that stream gets flushed or
closed.
  • Loading branch information
headius committed Feb 25, 2021
1 parent d2f5e1d commit 5d734aa
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ext/java/org/jruby/ext/psych/PsychEmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ private void emit(ThreadContext context, Event event) {
if (emitter == null) throw context.runtime.newRuntimeError("uninitialized emitter");

emitter.emit(event);

// flush writer after each emit
writer.flush();
} catch (IOException ioe) {
throw context.runtime.newIOErrorFromException(ioe);
} catch (EmitterException ee) {
Expand All @@ -349,10 +352,12 @@ private void initEmitter(ThreadContext context, IRubyObject _encoding) {
Encoding encoding = PsychLibrary.YAMLEncoding.values()[(int)_encoding.convertToInteger().getLongValue()].encoding;
Charset charset = context.runtime.getEncodingService().charsetForEncoding(encoding);

emitter = new Emitter(new OutputStreamWriter(new IOOutputStream(io, encoding), charset), options);
writer = new OutputStreamWriter(new IOOutputStream(io, encoding), charset);
emitter = new Emitter(writer, options);
}

Emitter emitter;
Writer writer;
DumperOptions options = new DumperOptions();
IRubyObject io;

Expand Down

0 comments on commit 5d734aa

Please sign in to comment.