RFR: 8374610: Make ByteArrayOutputStream.ensureCapacity(int) protected
Alan Bateman
alanb at openjdk.org
Fri Jan 16 12:34:31 UTC 2026
On Fri, 16 Jan 2026 12:01:18 GMT, Daniel Gredler <dgredler at openjdk.org> wrote:
> By this do you mean that subclassing BAOS is not encouraged? In my experience this is relatively common, and the class design seems to encourage it (the class is not final, instance variables are protected).
Many JDK 1.0/1.1 were designed for subclassing. The java.io package has many non-final/sealed classes with protected fields so the subclass can access the byte[], position, mark, etc. If designed today then I'm sure they would look differently. OutputStream would probably be an interface and there would be factory methods, would not be synchronized, there would be a lot less decorator pattern.
I don't object to adding an ensureCapacity method but we need to think about whether to re-specify the existing methods writeXXX methods to invoke it. With the current proposal I can extend BAOS, override ensureCapacity, but it wouldn't be called when using the existing methods to write.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/29180#issuecomment-3759825378
More information about the core-libs-dev
mailing list