/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hive.common.io;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.IOException;
import java.io.OutputStream;

/**
 * A thread-not-safe version of ByteArrayOutputStream, which removes all
 * synchronized modifiers.
 */
public class NonSyncByteArrayOutputStream extends ByteArrayOutputStream {
  public NonSyncByteArrayOutputStream(int size) {
    super(size);
  }

  public NonSyncByteArrayOutputStream() {
    super();
  }

  public byte[] getData() {
    return buf;
  }

  public int getLength() {
    return count;
  }

  public void setWritePosition(int writePosition) {
    count = writePosition;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void reset() {
    count = 0;
  }

  public void write(DataInput in, int length) throws IOException {
    enLargeBuffer(length);
    in.readFully(buf, count, length);
    count += length;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void write(int b) {
    enLargeBuffer(1);
    buf[count] = (byte) b;
    count += 1;
  }

  private int enLargeBuffer(int increment) {
    int temp = count + increment;
    int newLen = temp;
    if (temp > buf.length) {
      if ((buf.length << 1) > temp) {
        newLen = buf.length << 1;
      }
      byte newbuf[] = new byte[newLen];
      System.arraycopy(buf, 0, newbuf, 0, count);
      buf = newbuf;
    }
    return newLen;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void write(byte b[], int off, int len) {
    if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length)
        || ((off + len) < 0)) {
      throw new IndexOutOfBoundsException();
    } else if (len == 0) {
      return;
    }
    enLargeBuffer(len);
    System.arraycopy(b, off, buf, count, len);
    count += len;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void writeTo(OutputStream out) throws IOException {
    out.write(buf, 0, count);
  }
}

@@ -962,16 +962,41 @@
 tStream;
+%0Aimport java.util.Arrays;
 %0A%0A/**%0A *
@@ -1171,16 +1171,309 @@
 tream %7B%0A
+%0A  /**%0A   * The maximum size of array to allocate.%0A   * Some VMs reserve some header words in an array.%0A   * Attempts to allocate larger arrays may result in%0A   * OutOfMemoryError: Requested array size exceeds VM limit%0A   */%0A  private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;%0A%0A
   public
@@ -2176,19 +2176,20 @@
 private 
-int
+void
  enLarge
@@ -2195,16 +2195,22 @@
 eBuffer(
+final 
 int incr
@@ -2226,26 +2226,56 @@
     
-int temp = 
+final int requestCapacity = Math.addExact(
 count
- +
+,
  inc
@@ -2284,252 +2284,439 @@
 ment
+)
 ;%0A    
-int newLen = temp;%0A    if (temp %3E buf.length) %7B%0A      if ((buf.length %3C%3C 1) %3E temp) %7B
+final int currentCapacity = buf.length;%0A%0A    if (requestCapacity %3E currentCapacity) %7B%0A      // Increase size by a factor of 1.5x
 %0A      
- 
+int
  new
-Len = buf.length %3C%3C 1;%0A      %7D%0A      byte newbuf%5B%5D = new byte%5BnewLen%5D;%0A      System.arraycopy(buf, 0, newbuf, 0, count);%0A      buf = newbuf;%0A
+Capacity = currentCapacity + (currentCapacity %3E%3E 1);%0A%0A      // Check for overflow scenarios%0A      if (newCapacity %3C 0 %7C%7C newCapacity %3E MAX_ARRAY_SIZE) %7B%0A        newCapacity = MAX_ARRAY_SIZE;%0A      %7D else if (newCapacity %3C requestCapacity) %7B%0A        newCapacity = requestCapacity;%0A  
     
@@ -2725,22 +2725,54 @@
     
+ 
-return newLen;
+ buf = Arrays.copyOf(buf, newCapacity);%0A    %7D
 %0A  %7D
@@ -3023,29 +3023,28 @@
 ion();%0A    %7D
- else
+%0A   
  if (len == 

Make NonSyncByteArrayOutputStream Overflow Conscious (David Mollitor, reviewed by Gopal V)