protected final void ensureSpaceInBufferForFill(int desiredSpace) { // calculate the total unused space in the buffer. // this includes any space at the end of the buffer and any free // space at the beginning resulting from bytes already read. int currentAvailableSpace = (buffer_.length - count_) + pos_; // check to see if there is enough free space. if (currentAvailableSpace < desiredSpace) { // there is not enough free space so we need more storage. // we are going to double the buffer unless that happens to still be too small. // if more than double the buffer is needed, use the smallest amount over this as possible. int doubleBufferSize = (2 * buffer_.length); int minumNewBufferSize = (desiredSpace - currentAvailableSpace) + buffer_.length; int newsz = minumNewBufferSize <= doubleBufferSize ? doubleBufferSize : minumNewBufferSize; byte[] newBuffer = new byte[newsz]; // shift everything from the old buffer to the new buffer shiftBuffer(newBuffer); } else { // there is enough free space in the buffer but let's make sure it is all at the end. // this is also important because if we are going to do a read, it would be nice // to get as much data as possible and making room at the end if the buffer helps to // ensure this. if (pos_ != 0) { shiftBuffer(buffer_); } } }
protected final void ensureSpaceInBufferForFill(int desiredSpace) { // calculate the total unused space in the buffer. // this includes any space at the end of the buffer and any free // space at the beginning resulting from bytes already read. int currentAvailableSpace = (buffer_.length - count_) + pos_; // check to see if there is enough free space. if (currentAvailableSpace < desiredSpace) { // there is not enough free space so we need more storage. // we are going to double the buffer unless that happens to still be too small. // if more than double the buffer is needed, use the smallest amount over this as possible. int doubleBufferSize = (2 * buffer_.length); int minumNewBufferSize = (desiredSpace - currentAvailableSpace) + buffer_.length; int newsz = minumNewBufferSize <= doubleBufferSize ? doubleBufferSize : minumNewBufferSize; byte[] newBuffer = new byte[newsz]; // shift everything from the old buffer to the new buffer shiftBuffer(newBuffer); } else { // there is enough free space in the buffer but let's make sure it is all at the end. // this is also important because if we are going to do a read, it would be nice // to get as much data as possible and making room at the end if the buffer helps to // ensure this. if (pos_ != 0) { shiftBuffer(buffer_); } } }