Make ByteArrayOutputStream.ensureCapacity(int) protected

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Component/s: core-libs
    • None
    • source, binary, behavioral
    • low
    • Hide
      We are preserving the internal behavior of the existing ensureCapacity (now ensureCapacityInternal) method. The new method does not overload any other methods. The new method is not final, so existing subclasses which may define a method with the same signature will simply override the new method, and will continue to use the logic in the overriding method. However, existing subclasses which define a method with the same signature but a more restrictive access modifier (private or package-private) will be incompatible.
      Show
      We are preserving the internal behavior of the existing ensureCapacity (now ensureCapacityInternal) method. The new method does not overload any other methods. The new method is not final, so existing subclasses which may define a method with the same signature will simply override the new method, and will continue to use the logic in the overriding method. However, existing subclasses which define a method with the same signature but a more restrictive access modifier (private or package-private) will be incompatible.
    • Java API
    • SE

      Summary

      ByteArrayOutputStream.ensureCapacity(int) is now protected (it was previously private).

      Problem

      ByteArrayOutputStream.ensureCapacity(int) is currently private. It would be useful if it were protected, so that it can be more easily extended by subclasses. ByteArrayOutputStream is designed to be extended (the class is non-final and the instance variables are protected), but subclasses cannot currently easily hook into the existing buffer growth logic.

      Solution

      Make ByteArrayOutputStream.ensureCapacity(int) protected.

      The current private method throws an OutOfMemoryException for negative minCapacity parameters. The protected ensureCapacity method created here takes its cues from equivalent public methods in ArrayList and AbstractStringBuilder, and ignores non-positive parameters.

      In order to avoid any changes in the private method behavior, the existing private method is renamed from ensureCapacity to ensureCapacityInternal, and the non-positive minCapacity check happens only in the protected method. This is the exact pattern used by AbstractStringBuilder.ensureCapacity(int).

      Specification

      public class java.io.ByteArrayOutputStream

           /**
      -     * Increases the capacity if necessary to ensure that it can hold
      -     * at least the number of elements specified by the minimum
      -     * capacity argument.
      +     * Increases the capacity if necessary to ensure that this
      +     * {@code ByteArrayOutputStream} can hold at least the number of
      +     * elements specified by the {@code minCapacity} argument.
      +     * If the {@code minCapacity} argument is nonpositive, this
      +     * method takes no action and simply returns.
      +     *
      +     * @param minCapacity the desired minimum capacity.
      +     */
      +    protected void ensureCapacity(int minCapacity) { .. }
      

            Assignee:
            Daniel Gredler
            Reporter:
            Daniel Gredler
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: