001package io.avaje.json.stream;
002
003import java.io.Closeable;
004import java.io.IOException;
005import java.io.OutputStream;
006
007/**
008 * Output that can be aware of server content chunking.
009 * <p>
010 * Typically, for HTTP servers that can send output using fixed length or chunked.
011 */
012public interface JsonOutput extends Closeable {
013
014  /**
015   * Create as a simple wrapper for OutputStream.
016   */
017  static JsonOutput of(OutputStream outputStream) {
018    return new DJsonOutput(outputStream);
019  }
020
021  /**
022   * Write the content to the underlying output stream.
023   */
024  void write(byte[] content, int offset, int length) throws IOException;
025
026  /**
027   * Write the last content to the underlying output stream.
028   * <p>
029   * Given that this is known to be the last content written an implementation can make
030   * use of this to optimise for sending as fixed length content.
031   */
032  default void writeLast(byte[] content, int offset, int length) throws IOException {
033    write(content, offset, length);
034  }
035
036  /**
037   * Flush the underlying OutputStream.
038   */
039  void flush() throws IOException;
040
041  /**
042   * Return the underlying OutputStream.
043   * <p>
044   * This is used for Jsonb adapters (Jackson) that can't support writeLast() semantics.
045   */
046  OutputStream unwrapOutputStream();
047}