/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.util.io;

import com.intellij.util.ArrayUtil;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;

public class UnsyncByteArrayOutputStream extends OutputStream {
  protected byte[] myBuffer;
  protected int myCount;

  public UnsyncByteArrayOutputStream() {
    this(32);
  }

  public UnsyncByteArrayOutputStream(int size) {
    myBuffer = new byte[size];
  }

  @Override
  public void write(int b) {
    int newcount = myCount + 1;
    if (newcount > myBuffer.length) {
      myBuffer = Arrays.copyOf(myBuffer, Math.max(myBuffer.length << 1, newcount));
    }
    myBuffer[myCount] = (byte)b;
    myCount = newcount;
  }

  @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;
    }
    int newcount = myCount + len;
    if (newcount > myBuffer.length) {
      myBuffer = Arrays.copyOf(myBuffer, Math.max(myBuffer.length << 1, newcount));
    }
    System.arraycopy(b, off, myBuffer, myCount, len);
    myCount = newcount;
  }

  public void writeTo(OutputStream out) throws IOException {
    out.write(myBuffer, 0, myCount);
  }

  public void reset() {
    myCount = 0;
  }

  public byte[] toByteArray() {
    if (myBuffer.length == myCount) {
      byte[] buffer = myBuffer;
      myBuffer = ArrayUtil.EMPTY_BYTE_ARRAY;
      return buffer;
    }
    return Arrays.copyOf(myBuffer, myCount);
  }

  public int size() {
    return myCount;
  }

  public String toString() {
    return new String(myBuffer, 0, myCount);
  }
}

@@ -862,16 +862,46 @@
 myCount;
+%0A  private boolean myIsShared;
 %0A%0A  publ
@@ -1138,32 +1138,46 @@
  myBuffer.length
+ %7C%7C myIsShared
 ) %7B%0A      myBuff
@@ -1196,32 +1196,61 @@
 copyOf(myBuffer,
+ newcount %3E myBuffer.length ?
  Math.max(myBuff
@@ -1266,33 +1266,75 @@
  %3C%3C 1, newcount)
-)
+:myBuffer.length);%0A      myIsShared = false
 ;%0A    %7D%0A    myBu
@@ -1720,16 +1720,30 @@
 r.length
+ %7C%7C myIsShared
 ) %7B%0A    
@@ -1778,16 +1778,45 @@
 yBuffer,
+ newcount %3E myBuffer.length ?
  Math.ma
@@ -1848,17 +1848,60 @@
 ewcount)
-)
+: myBuffer.length);%0A      myIsShared = false
 ;%0A    %7D%0A
@@ -2212,77 +2212,25 @@
     
-byte%5B%5D buffer = myBuffer;%0A      myBuffer = ArrayUtil.EMPTY_BYTE_ARRAY
+myIsShared = true
 ;%0A  
@@ -2240,17 +2240,19 @@
  return 
-b
+myB
 uffer;%0A 

set shared flag when toByteArray is called and if it is set then copy byte buffer on write