dblevins commented on code in PR #84:
URL: https://github.com/apache/johnzon/pull/84#discussion_r860575280


##########
johnzon-core/src/main/java/org/apache/johnzon/core/Snippet.java:
##########
@@ -228,172 +232,181 @@ private String get() {
         public void close() {
             generator.close();
         }
-    }
-
-    /**
-     * Specialized OutputStream with three internal states:
-     * Writing, Completed, Truncated.
-     *
-     * When there is still space left for more json, the
-     * state will be Writing
-     *
-     * If the last write brought is exactly to the end of
-     * the max length, the state will be Completed.
-     *
-     * If the last write brought us over the max length, the
-     * state will be Truncated.
-     */
-    static class SnippetOutputStream extends OutputStream {
-
-        private final ByteArrayOutputStream buffer;
-        private OutputStream mode;
-
-        public SnippetOutputStream(final int max) {
-            this.buffer = new ByteArrayOutputStream(Math.min(max, 8192));
-            this.mode = new Writing(max, buffer);
-        }
-
-        public String get() {
-            return buffer.toString();
-        }
 
         /**
-         * Calling this method implies the need to continue
-         * writing and a question on if that is ok.
+         * Specialized Writer with three internal states:
+         * Writing, Completed, Truncated.
          *
-         * It impacts internal state in the same way as
-         * calling a write method.
+         * When there is still space left for more json, the
+         * state will be Writing
          *
-         * @return true if no more writes are possible
+         * If the last write brought is exactly to the end of
+         * the max length, the state will be Completed.
+         *
+         * If the last write brought us over the max length, the
+         * state will be Truncated.
          */
-        public boolean terminate() {
-            if (mode instanceof Truncated) {
-                return true;
+        class SnippetWriter extends Writer implements Buffered {
+
+            private final ByteArrayOutputStream buffer;
+            private Mode mode;
+            private Supplier<Integer> bufferSize;
+
+            public SnippetWriter(final int max) {
+                final int size = Math.min(max, 8192);
+
+                this.buffer = new ByteArrayOutputStream(size);
+                this.mode = new Writing(max, new OutputStreamWriter(buffer));
+
+                /*
+                 * The first time the buffer size is requested, disable 
flushing
+                 * as we know our requested buffer size will be respected
+                 */
+                this.bufferSize = () -> {
+                    // disable flushing
+                    flush = () -> {
+                    };
+                    // future calls can just return the size
+                    bufferSize = () -> size;
+                    return size;
+                };
             }
 
-            if (mode instanceof Completed) {
-                mode = new Truncated();
-                return true;
+            public String get() {
+                return buffer.toString();
             }
 
-            return false;
-        }
-
-        public boolean isTruncated() {
-            return mode instanceof Truncated;
-        }
-
-        @Override
-        public void write(final int b) throws IOException {
-            mode.write(b);
-        }
-
-        @Override
-        public void write(final byte[] b) throws IOException {
-            mode.write(b);
-        }
-
-        @Override
-        public void write(final byte[] b, final int off, final int len) throws 
IOException {
-            mode.write(b, off, len);
-        }
+            @Override
+            public int bufferSize() {
+                return bufferSize.get();
+            }
 
-        @Override
-        public void flush() throws IOException {
-            mode.flush();
-        }
+            /**
+             * Calling this method implies the need to continue
+             * writing and a question on if that is ok.
+             *
+             * It impacts internal state in the same way as
+             * calling a write method.
+             *
+             * @return true if no more writes are possible
+             */
+            public boolean terminate() {
+                if (mode instanceof Truncated) {
+                    return true;
+                }
 
-        @Override
-        public void close() throws IOException {
-            mode.close();
-        }
+                if (mode instanceof Completed) {
+                    mode = new Truncated();
+                    return true;
+                }
 
-        class Writing extends OutputStream {
-            private final int max;
-            private int count;
-            private final OutputStream out;
+                return false;
+            }
 
-            public Writing(final int max, final OutputStream out) {
-                this.max = max;
-                this.out = out;
+            public boolean isTruncated() {
+                return mode instanceof Truncated;
             }
 
             @Override
-            public void write(final int b) throws IOException {
-                if (++count < max) {
-                    out.write(b);
-                } else {
-                    maxReached(new Truncated());
-                }
+            public void write(final char[] cbuf, final int off, final int len) 
throws IOException {
+                mode.write(cbuf, off, len);
             }
 
             @Override
-            public void write(final byte[] b) throws IOException {
-                write(b, 0, b.length);
+            public void flush() throws IOException {
+                mode.flush();
             }
 
             @Override
-            public void write(final byte[] b, final int off, final int len) 
throws IOException {
-                final int remaining = max - count;
+            public void close() throws IOException {
+                mode.close();
+            }
 
-                if (remaining <= 0) {
+            abstract class Mode extends Writer {
+                @Override
+                public void flush() throws IOException {

Review Comment:
   Added!  I'll make sure to do this any future PRs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to