//public bool put(byte element) //{ // bool result = false; // lock(this) // { // if (!flipped) // { // if (writePos == capacity) // { // writePos = 0; // flipped = true; // if (writePos < readPos) // { // elements[writePos++] = element; // result = true; // } // else // { // result = false; // } // } // else // { // elements[writePos++] = element; // result = true; // } // } // else // { // if (writePos < readPos) // { // elements[writePos++] = element; // result = true; // } // else // { // result = false; // } // } // if (result) // _semaphore.Release(1); // } // return result; //} public int Put(byte[] newElements, int length) { int newElementsReadPos = 0; int sizeReadPos = 0; // write length into first 4 bytes int actualLength = length + SIZE; lock (this) { if (RemainingCapacity() < length) { return(0); } //logger.Trace("Put. remCap = " + remainingCapacity()); if (!flipped) { //readPos lower than writePos - free sections are: //1) from writePos to capacity //2) from 0 to readPos if (actualLength <= capacity - writePos) { //new elements fit into top of elements array - copy directly // Size Header ByteTools.Int32Bytes(this.elements, this.writePos, length); this.writePos += SIZE; // Actual data for (; newElementsReadPos < length; newElementsReadPos++) { this.elements[this.writePos++] = newElements[newElementsReadPos]; } } else { //new elements must be divided between top and bottom of elements array //writing to top for (; this.writePos < capacity; this.writePos++) { this.elements[this.writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; } //writing to bottom this.writePos = 0; this.flipped = true; int endPos = Math.Min(this.readPos, length - newElementsReadPos + SIZE - sizeReadPos); for (; this.writePos < endPos; this.writePos++) { this.elements[writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; } } } else { //readPos higher than writePos - free sections are: //1) from writePos to readPos int endPos = Math.Min(this.readPos, this.writePos + length + SIZE); for (; this.writePos < endPos; this.writePos++) { this.elements[this.writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; } } //_semaphore.Release(newElementsReadPos); _semaphore.Release(1); } return(newElementsReadPos); }
public int Put(byte[] newElements, int length) { int newElementsReadPos = 0; int sizeReadPos = 0; // write length into first 4 bytes int actualLength = length + SIZE; lock (this) { // Check enought space if (actualLength > RemainingCapacity()) { Console.WriteLine("No space left"); return(0); } if (!flipped) { //readPos lower than writePos - free sections are: //1) from writePos to capacity //2) from 0 to readPos if (actualLength <= capacity - writePos) { //new elements fit into top of elements array - copy directly // Size Header ByteTools.Int32Bytes(this.elements, this.writePos, length); this.writePos += SIZE; // Actual data // Copy Array and update indexes Array.Copy(newElements, newElementsReadPos, this.elements, this.writePos, length); this.writePos += length; newElementsReadPos += length; //for (; newElementsReadPos < length; newElementsReadPos++) //{ // this.elements[this.writePos++] = newElements[newElementsReadPos]; //} } else { //new elements must be divided between top and bottom of elements array // SIZE will be copied by for loop // SIZE does not fit in top part if (this.writePos + SIZE > capacity) { //writing to top for (; this.writePos < capacity; this.writePos++) { this.elements[this.writePos] = (byte)(length >> 8 * sizeReadPos++); } //writing to bottom. Flipped this.writePos = 0; this.flipped = true; //writing to botton remaining SIZE for (; sizeReadPos < SIZE; this.writePos++) { this.elements[this.writePos] = (byte)(length >> 8 * sizeReadPos++); } // Copy data by ArrayCopy // Copy Array and update indexes Array.Copy(newElements, newElementsReadPos, this.elements, this.writePos, length); this.writePos += length; newElementsReadPos += length; } else { // SIZE Fits in top int endPos = this.writePos + SIZE; //writing to top for (; this.writePos < endPos; this.writePos++) { this.elements[this.writePos] = (byte)(length >> 8 * sizeReadPos++); } // Copy Array and update indexes int copylengthTop = capacity - this.writePos; Array.Copy(newElements, newElementsReadPos, this.elements, this.writePos, copylengthTop); this.writePos = 0; this.flipped = true; newElementsReadPos += copylengthTop; int copylengthDown = length - copylengthTop; Array.Copy(newElements, newElementsReadPos, this.elements, this.writePos, copylengthDown); this.writePos += copylengthDown; newElementsReadPos += copylengthDown; } ////writing to top //for (; this.writePos < capacity; this.writePos++) //{ // this.elements[this.writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; //} ////writing to bottom //this.writePos = 0; //this.flipped = true; //int endPos = Math.Min(this.readPos, length - newElementsReadPos + SIZE - sizeReadPos); //for (; this.writePos < endPos; this.writePos++) //{ // this.elements[writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; //} } } else { //readPos higher than writePos - free sections are: //1) from writePos to readPos // Size Header ByteTools.Int32Bytes(this.elements, this.writePos, length); this.writePos += SIZE; // Actual data // Copy Array and update indexes Array.Copy(newElements, newElementsReadPos, this.elements, this.writePos, length); this.writePos += length; newElementsReadPos += length; //for (; this.writePos < endPos; this.writePos++) //{ // this.elements[this.writePos] = sizeReadPos <= 3 ? (byte)(length >> 8 * sizeReadPos++) : newElements[newElementsReadPos++]; //} } _semaphore.Release(1); } return(newElementsReadPos); }