Make ByteArrayOutputStream.ensureCapacity(int) protected

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Component/s: core-libs
    • None
    • source, binary, behavioral
    • low
    • Hide
      We are preserving the internal behavior of the ensureCapacity (now ensureCapacityInternal) method. The new protected method does not overload any other methods. The new protected method is not final, so any existing user subclasses which happen to already have a method with the same signature will simply override the new protected method.
      Show
      We are preserving the internal behavior of the ensureCapacity (now ensureCapacityInternal) method. The new protected method does not overload any other methods. The new protected method is not final, so any existing user subclasses which happen to already have a method with the same signature will simply override the new protected method.
    • 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) {
      +        if (minCapacity > 0) {
      +            ensureCapacityInternal(minCapacity);
      +        }
      +    }
      +
      +    /**
      +     * 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.
            *
           * @param  minCapacity the desired minimum capacity.
           * @throws OutOfMemoryError if {@code minCapacity < 0} and
           * {@code minCapacity - buf.length > 0}.  This is interpreted as a
           * request for the unsatisfiably large capacity.
           * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
           */
      -    private void ensureCapacity(int minCapacity) {
      +    private void ensureCapacityInternal(int minCapacity) {
              // overflow-conscious code
              int oldCapacity = buf.length;
              int minGrowth = minCapacity - oldCapacity;
              if (minGrowth > 0) {
                  buf = Arrays.copyOf(buf, ArraysSupport.newLength(oldCapacity,
                          minGrowth, oldCapacity /* preferred growth */));
              }
          }
      
           @Override
           public synchronized void write(int b) {
      -        ensureCapacity(count + 1);
      +        ensureCapacityInternal(count + 1);
               buf[count] = (byte) b;
               count += 1;
           }
      
           @Override
           public synchronized void write(byte[] b, int off, int len) {
               Objects.checkFromIndexSize(off, len, b.length);
      -        ensureCapacity(count + len);
      +        ensureCapacityInternal(count + len);
               System.arraycopy(b, off, buf, count, len);
               count += len;
           }
      

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

              Created:
              Updated: