/**
  * Write the bytes to byte array.
  *
  * @param b the bytes to write
  * @param off The start offset
  * @param len The number of bytes to write
  */
 @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;
   }
   synchronized (this) {
     int newcount = count + len;
     int remaining = len;
     int inBufferPos = count - filledBufferSum;
     while (remaining > 0) {
       int part = Math.min(remaining, currentBuffer.length - inBufferPos);
       System.arraycopy(b, off + len - remaining, currentBuffer, inBufferPos, part);
       remaining -= part;
       if (remaining > 0) {
         needNewBuffer(newcount);
         inBufferPos = 0;
       }
     }
     count = newcount;
   }
 }
 public ByteArrayOutputStream(int size) {
   if (size < 0) {
     throw new IllegalArgumentException("Negative initial size: " + size);
   }
   synchronized (this) {
     needNewBuffer(size);
   }
 }
 public synchronized void write(int b) {
   int inBufferPos = this.count - this.filledBufferSum;
   if (inBufferPos == this.currentBuffer.length) {
     needNewBuffer(this.count + 1);
     inBufferPos = 0;
   }
   this.currentBuffer[inBufferPos] = ((byte) b);
   this.count += 1;
 }
 /**
  * Write a byte to byte array.
  *
  * @param b the byte to write
  */
 @Override
 public synchronized void write(int b) {
   int inBufferPos = count - filledBufferSum;
   if (inBufferPos == currentBuffer.length) {
     needNewBuffer(count + 1);
     inBufferPos = 0;
   }
   currentBuffer[inBufferPos] = (byte) b;
   count++;
 }
 public synchronized int write(InputStream in) throws IOException {
   int readCount = 0;
   int inBufferPos = this.count - this.filledBufferSum;
   int n = in.read(this.currentBuffer, inBufferPos, this.currentBuffer.length - inBufferPos);
   while (n != -1) {
     readCount += n;
     inBufferPos += n;
     this.count += n;
     if (inBufferPos == this.currentBuffer.length) {
       needNewBuffer(this.currentBuffer.length);
       inBufferPos = 0;
     }
     n = in.read(this.currentBuffer, inBufferPos, this.currentBuffer.length - inBufferPos);
   }
   return readCount;
 }