/*
/////////////////////////////////////////////////////////////////////
Buffer recycling:
/////////////////////////////////////////////////////////////////////
14315
public char[] allocSmallCBuffer(int minSize)
    {
        if (mCurrRecycler != null) {
            char[] result = mCurrRecycler.getSmallCBuffer(minSize);
            if (result != null) {
                return result;
            }
        }
        // Nope; no recycler, or it has no suitable buffers, let's create:
        return new char[minSize];
    }
1154766
function _bufferToString(buffer) {
        var str = '';
        var uint16Array = new Uint16Array(buffer);

        try {
            str = String.fromCharCode.apply(null, uint16Array);
        } catch (e) {
            // This is a fallback implementation in case the first one does
            // not work. This is required to get the phantomjs passing...
            for (var i = 0; i < uint16Array.length; i++) {
                str += String.fromCharCode(uint16Array[i]);
            }
        }

        return str;
    }
637740
function infoBuffer(buf, cb) {
  try {
    buf = bufferUtils.toBuffer(buf);
    info(buf, cb);
  } catch(err) {
    cb(err);
  }
}
935214
public static void bufferedToGray(DataBufferUShort buffer , WritableRaster src, GrayI16 dst) {
		short[] srcData = buffer.getData();

		int numBands = src.getNumBands();

		int size = dst.getWidth() * dst.getHeight();

		int srcStride = stride(src);
		int srcOffset = getOffset(src);
		int srcStrideDiff = srcStride-src.getNumDataElements()*dst.width;

		if (numBands == 1) {
			if (dst.startIndex == 0 && dst.width == dst.stride && srcStrideDiff == 0 && srcOffset == 0 )
				System.arraycopy(srcData, 0, dst.data, 0, size);
			else {
				//CONCURRENT_BELOW BoofConcurrency.loopFor(0, dst.height, y -> {
				for (int y = 0; y < dst.height; y++) {
					int indexDst = dst.startIndex + dst.stride * y;
					int indexSrc = srcOffset + srcStride * y;

					System.arraycopy(srcData, indexSrc, dst.data, indexDst, dst.width);
				}
				//CONCURRENT_ABOVE });
			}
		} else {
			throw new RuntimeException("Only single band images are currently support for 16bit");
		}
	}
507063
function bufferFromArrayBuffer(arrayBuffer) {
  const buffer = Buffer.alloc(arrayBuffer.byteLength);
  const view = new Uint8Array(arrayBuffer);

  for (let i = 0; i < buffer.length; ++i) {
    buffer[i] = view[i];
  }

  return buffer;
}
741871
function __deserializeArrayBuffer(buffer, start, options) {
	let end = start + 4;
	let [length] = new Uint32Array(buffer.slice(start, end));
	
	end = end + length;
	return {anchor:end, value:buffer.slice(start+4, end)};
}
701372
function joinBuffers(buffers) {
		var
			i = -1, j = -1,
			num = buffers.length,
			totalBytes = 0,
			bytesWritten = 0,
			buff,
			superBuff
		;
		while (++i < num) {
			totalBytes += buffers[i].length;
		}
		superBuff = new Buffer(totalBytes);
		while (++j < num) {
			buff = buffers[j];
			buff.copy(superBuff, bytesWritten, 0);
			bytesWritten += buff.length;
		}
		return superBuff;
	}
84691
def consume_remaining_buffer(stack, buf)
      return if buf.length == 0
      stack << buf.dup
      buf.replace('')
    end
154889
function buffer(patch) {
		var mw = (patch || core.base());
		mw.lines = [];
		mw.enabled = true;
		mw.writeln = function (line) {
			if (mw.enabled) {
				mw.lines.push(String(line));
			}
		};
		//TODO add wordwrap?
		mw.concat = function (seperator, indent, sepAfter) {
			if (mw.lines.length > 0) {
				sepAfter = (typeof sepAfter !== 'undefined' ? sepAfter : true);
				seperator = (typeof seperator !== 'undefined' ? seperator : '\n');
				indent = (typeof indent !== 'undefined' ? indent : '');
				return indent + mw.lines.join(seperator + indent) + (sepAfter ? seperator : '');
			}
			return '';
		};
		mw.clear = function () {
			mw.lines.length = 0;
		};
		if (!patch) {
			mw.toString = function () {
				return '<miniwrite-buffer>';
			};
		}
		return mw;
	}
661875
function bufferToArrayBuffer(buf) {
  const arr = new ArrayBuffer(buf.length);
  const view = new Uint8Array(arr);
  for (let i = 0; i < buf.length; ++i) {
    view[i] = buf[i];
  }
  return arr;
}
1085313
function RingBuffer(maxSize){
    var data = [];
    var cursor = 0;
    var isFull = false;
    this.append = function(x){
        if (isFull){
            data[cursor] = x;
            cursor = (cursor + 1) % maxSize;
        }
        else{
            data.push(x);
            cursor++;
            if (data.length === maxSize){
                cursor = 0;
                isFull = true;
            }
        }
    };
    this.avg = function(){
        var sum = data.reduce(function(a, b){ return a + b });
        return sum / (isFull ? maxSize : cursor);
    };
    this.size = function(){
        return isFull ? maxSize : cursor;
    };
    this.clear = function(){
        data = [];
        cursor = 0;
        isFull = false;
    };
}
249604
function zeroBuffer(buf) {
  for (var i = 0; i < buf.length; i++) {
    buf[i] = 0;
  }
  return buf;
}
181540
function Buffering(options) {
    events.EventEmitter.call(this);

    options = options || {};

    if (options.useUnique) {
        var newOptions = {};
        Object.keys(options).forEach(function(key) {
            if (key != 'useUnique') newOptions[key] = options[key];
        });
        return new UniqueBuffering(newOptions);
    }

    this._timeThreshold = (typeof options.timeThreshold === 'undefined') ? -1 : options.timeThreshold;
    this._sizeThreshold = (typeof options.sizeThreshold === 'undefined') ? -1 : options.sizeThreshold;
    this._data = [];
    this._flushTimer = null;
    this._paused = false;
    this._resumeTimer = null;
    this._flushingBySize = false;
}
1371177
function bufferReviver(k, v) {
    if (
        v !== null &&
        typeof v === 'object' &&
        'type' in v &&
        v.type === 'Buffer' &&
        'data' in v &&
        Array.isArray(v.data)) {
        return new Buffer(v.data);
    }
    return v;
}
262682
private static boolean directByteBufferEquals(ByteBuffer buffa, ByteBuffer buffb)
    {
        if ( buffa == buffb )
        {
            return true;
        }
        
        if ( buffa ==null || buffb == null )
        {
            return false;
        }
        
        buffa.rewind();
        buffb.rewind();
        
        int length = buffa.remaining();
        
        if ( buffb.remaining() != length )
        {
            return false;
        }
        
        for ( int i = 0; i < length; i++ )
        {
            if ( buffa.get() != buffb.get() )
            {
                return false;
            }
        }

        return true;
    }
596302
function copyBuffer(buf, pos, len) {
  var copy = utils.newBuffer(len);
  buf.copy(copy, 0, pos, pos + len);
  return copy;
}
1164041
function getBufferPadded(buffer) {
    const boundary = 4;
    const byteLength = buffer.length;
    const remainder = byteLength % boundary;
    if (remainder === 0) {
        return buffer;
    }
    const padding = (remainder === 0) ? 0 : boundary - remainder;
    const emptyBuffer = Buffer.alloc(padding);
    return Buffer.concat([buffer, emptyBuffer]);
}