Repository: apache/dubbo
File: dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/LengthFieldStreamingDecoder.java (Java)

Code before:
        public final InputStream inputStream;
        public final int length;

        public MessageStream(InputStream inputStream, int length) {
            this.inputStream = inputStream;
            this.length = length;
        }
    }

    /**
     * A bounded InputStream that reads at most 'limit' bytes from the source stream.
     * Extends BufferedInputStream to support mark/reset, which is required by
     * deserializers like Hessian2.
     */
    private static class BoundedInputStream extends BufferedInputStream {

        private final int limit;
        private int remaining;
        private int markedRemaining;

        public BoundedInputStream(InputStream source, int limit) {
            super(source, limit);
            this.limit = limit;
            this.remaining = limit;
            this.markedRemaining = limit;
        }

        @Override
        public int read() throws IOException {
            if (remaining <= 0) {
                return -1;
            }
            int result = super.read();
            if (result != -1) {
                remaining--;
            }
            return result;
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            if (remaining <= 0) {
                return -1;
            }
            int toRead = Math.min(len, remaining);
            int result = super.read(b, off, toRead);
            if (result > 0) {
                remaining -= result;
            }
            return result;
        }

Review comment (performance):
The BoundedInputStream constructor uses the limit as the buffer size for BufferedInputStream (line 297). For large messages, this could allocate unnecessarily large buffers. Consider using a more reasonable buffer size (e.g., Math.min(limit, 8192)) to avoid excessive memory allocation while still providing good buffering performance.

Code after:
        public final InputStream inputStream;
        public final int length;

        public MessageStream(InputStream inputStream, int length) {
            this.inputStream = inputStream;
            this.length = length;
        }
    }

    /**
     * A bounded InputStream that reads at most 'limit' bytes from the source stream.
     * Extends BufferedInputStream to support mark/reset, which is required by
     * deserializers like Hessian2.
     */
    private static class BoundedInputStream extends BufferedInputStream {

        private final int limit;
        private int remaining;
        private int markedRemaining;

        public BoundedInputStream(InputStream source, int limit) {
            super(source, limit);
            this.limit = limit;
            this.remaining = limit;
            this.markedRemaining = limit;
        }

        @Override
        public synchronized int read() throws IOException {
            if (remaining <= 0) {
                return -1;
            }
            int result = super.read();
            if (result != -1) {
                remaining--;
            }
            return result;
        }

        @Override
        public synchronized int read(byte[] b, int off, int len) throws IOException {
            if (remaining <= 0) {
                return -1;
            }
            int toRead = Math.min(len, remaining);
            int result = super.read(b, off, toRead);
            if (result > 0) {
                remaining -= result;
            }
            return result;
        }