public InputStreamByteBuffer(Stream stream) { Fx.Assert(stream.CanRead, "Stream is not readable"); this.inputStream = stream; this.buffer = BufferPool.TakeBuffer(512); this.capacity = this.buffer.Length; }
/// <summary> /// Copies binary image of object that implements <see cref="ISupportBinaryImage"/> to a <see cref="Stream"/>. /// </summary> /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param> /// <param name="stream">Destination <see cref="Stream"/>.</param> /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception> public static void CopyBinaryImageToStream(this ISupportBinaryImage imageSource, Stream stream) { if ((object)imageSource == null) { throw new ArgumentNullException("imageSource"); } int length = imageSource.BinaryLength; byte[] buffer = BufferPool.TakeBuffer(length); try { // Copy generated binary image to buffer int writeCount = imageSource.GenerateBinaryImage(buffer, 0); // Write buffer bytes to stream, if any were generated if (writeCount > 0) { stream.Write(buffer, 0, writeCount); } } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
/// <summary> /// Parses binary image of object that implements <see cref="ISupportBinaryImage"/> from a <see cref="Stream"/>. /// </summary> /// <param name="imageSource"><see cref="ISupportBinaryImage"/> source.</param> /// <param name="stream">Source <see cref="Stream"/>.</param> /// <returns>The number of bytes parsed from the <paramref name="stream"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="imageSource"/> cannot be null.</exception> public static int ParseBinaryImageFromStream(this ISupportBinaryImage imageSource, Stream stream) { if ((object)imageSource == null) { throw new ArgumentNullException("imageSource"); } int length = imageSource.BinaryLength; byte[] buffer = BufferPool.TakeBuffer(length); try { // Read buffer bytes from stream int readCount = stream.Read(buffer, 0, length); // Parse binary image from buffer bytes read from stream return(imageSource.ParseBinaryImage(buffer, 0, readCount)); } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
/// <summary>Reads a new <see cref="WaveFormatChunk"/> from the specified stream.</summary> /// <param name="preRead">Pre-parsed <see cref="RiffChunk"/> header.</param> /// <param name="source">Source stream to read data from.</param> /// <exception cref="InvalidOperationException">WAVE format or extra parameters section too small, wave file corrupted.</exception> /// <exception cref="InvalidDataException">Invalid bit rate encountered - wave file bit rates must be a multiple of 8.</exception> public WaveFormatChunk(RiffChunk preRead, Stream source) : base(preRead, RiffTypeID) { int length = ChunkSize; byte[] buffer = BufferPool.TakeBuffer(length); try { int bytesRead = source.Read(buffer, 0, length); // Initialize class from buffer ParseBinaryImage(buffer, 0, bytesRead); // Read extra parameters, if any if (m_extraParametersSize > 0) { m_extraParameters = new byte[m_extraParametersSize]; bytesRead = source.Read(m_extraParameters, 0, m_extraParametersSize); if (bytesRead < m_extraParametersSize) { throw new InvalidOperationException("WAVE extra parameters section too small, wave file corrupted"); } } } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
// Static Methods /// <summary> /// Attempts to read the next RIFF chunk from the <paramref name="source"/> stream. /// </summary> /// <param name="source">Source stream for next RIFF chunk.</param> /// <returns>Next RIFF chunk read from the <paramref name="source"/> stream.</returns> /// <exception cref="InvalidOperationException">RIFF chunk too small, media file corrupted.</exception> public static RiffChunk ReadNext(Stream source) { RiffChunk riffChunk = new RiffChunk(); int length = riffChunk.BinaryLength; byte[] buffer = BufferPool.TakeBuffer(length); try { int bytesRead = source.Read(buffer, 0, length); if (bytesRead < length) { throw new InvalidOperationException("RIFF chunk too small, media file corrupted"); } riffChunk.TypeID = Encoding.ASCII.GetString(buffer, 0, 4); riffChunk.ChunkSize = EndianOrder.LittleEndian.ToInt32(buffer, 4); return(riffChunk); } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
public ReadFileToDatabase(BufferPool bufferPool, ITransactionalStorage storage, Stream inputStream, string filename) { this.bufferPool = bufferPool; this.inputStream = inputStream; this.storage = storage; this.filename = filename; buffer = bufferPool.TakeBuffer(StorageConstants.MaxPageSize); md5Hasher = Encryptor.Current.CreateHash(); }
public ReadFileToDatabase(BufferPool bufferPool, ITransactionalStorage storage, OrderedPartCollection <AbstractFilePutTrigger> putTriggers, Stream inputStream, string filename, RavenJObject headers) { this.bufferPool = bufferPool; this.inputStream = inputStream; this.storage = storage; this.putTriggers = putTriggers; this.filename = filename; this.headers = headers; buffer = bufferPool.TakeBuffer(StorageConstants.MaxPageSize); md5Hasher = Encryptor.Current.CreateHash(); }
public override void EnsureSize(int size) { if (this.Size < size) { if (this.autoGrow) { byte[] newBuffer = BufferPool.TakeBuffer(this.capacity * 2); System.Buffer.BlockCopy(this.buffer, this.start, newBuffer, 0, this.Length); BufferPool.ReturnBuffer(this.buffer); this.buffer = newBuffer; this.capacity = newBuffer.Length; } else { throw new InvalidOperationException("EnsureSize"); } } }
/// <summary> /// Establishes (or reestablishes) a receive buffer of a given size. /// </summary> /// <param name="size">Desired minimum size of receive buffer.</param> /// <returns>New receive buffer.</returns> public byte[] SetReceiveBuffer(int size) { if (size != m_receiveBufferSize) { if (m_receiveBuffer != null) { BufferPool.ReturnBuffer(m_receiveBuffer); } // Take a buffer from the pool of the desired size m_receiveBuffer = BufferPool.TakeBuffer(size); // The buffer returned may be bigger than the requested size, but only the specified size will be usable m_receiveBufferSize = size; } return(m_receiveBuffer); }
/// <summary>Reads a new WAVE format section from the specified stream.</summary> /// <param name="preRead">Pre-parsed <see cref="RiffChunk"/> header.</param> /// <param name="source">Source stream to read data from.</param> /// <param name="waveFormat">Format of the data section to be parsed.</param> /// <exception cref="InvalidOperationException">WAVE format or extra parameters section too small, wave file corrupted.</exception> public WaveDataChunk(RiffChunk preRead, Stream source, WaveFormatChunk waveFormat) : base(preRead, RiffTypeID) { m_waveFormat = waveFormat; m_sampleBlocks = new List <LittleBinaryValue[]>(); m_chunkSize = -1; int blockSize = waveFormat.BlockAlignment; int sampleSize = waveFormat.BitsPerSample / 8; int channels = waveFormat.Channels; TypeCode sampleTypeCode = m_waveFormat.GetSampleTypeCode(); LittleBinaryValue[] sampleBlock; byte[] buffer = BufferPool.TakeBuffer(blockSize); try { int bytesRead = source.Read(buffer, 0, blockSize); while (bytesRead == blockSize) { // Create a new sample block, one little-endian formatted binary sample value for each channel sampleBlock = new LittleBinaryValue[channels]; for (int x = 0; x < channels; x++) { sampleBlock[x] = new LittleBinaryValue(sampleTypeCode, buffer, x * sampleSize, sampleSize); } m_sampleBlocks.Add(sampleBlock); bytesRead = source.Read(buffer, 0, blockSize); } } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
/// <summary>Reads a new RIFF header from the specified stream.</summary> /// <param name="preRead">Pre-parsed <see cref="RiffChunk"/> header.</param> /// <param name="source">Source stream to read data from.</param> /// <param name="format">Expected RIFF media format (e.g., "WAVE").</param> /// <exception cref="ArgumentNullException"><paramref name="format"/> cannot be null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="format"/> must be extactly 4 characters in length.</exception> public RiffHeaderChunk(RiffChunk preRead, Stream source, string format) : base(preRead, RiffTypeID) { if (string.IsNullOrWhiteSpace(format)) { throw new ArgumentNullException("value"); } if (format.Length != 4) { throw new ArgumentOutOfRangeException("value", "Format must be exactly 4 characters in length"); } byte[] buffer = BufferPool.TakeBuffer(4); try { int bytesRead = source.Read(buffer, 0, 4); if (bytesRead < 4) { throw new InvalidOperationException("RIFF format section too small, media file corrupted"); } // Read format stored in RIFF section m_format = Encoding.ASCII.GetString(buffer, 0, 4); if (m_format != format) { throw new InvalidDataException(string.Format("{0} format expected but got {1}, this does not appear to be a valid {0} file", format, m_format)); } } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
public static void CopyStream(this Stream source, Stream destination) { byte[] buffer = BufferPool.TakeBuffer(BufferSize); try { int bytesRead = source.Read(buffer, 0, BufferSize); while (bytesRead > 0) { destination.Write(buffer, 0, bytesRead); bytesRead = source.Read(buffer, 0, BufferSize); } } finally { if ((object)buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
/// <summary> /// Parses the object implementing the <see cref="ISupportBinaryImage"/> interface. /// </summary> /// <param name="image">Object to be parsed that implements the <see cref="ISupportBinaryImage"/> interface.</param> /// <remarks> /// This function takes the binary image from <see cref="ISupportBinaryImage"/> and writes the buffer to the <see cref="BinaryImageParserBase"/> stream for parsing. /// </remarks> public virtual void Parse(ISupportBinaryImage image) { int length = image.BinaryLength; byte[] buffer = BufferPool.TakeBuffer(length); try { // Generate the binary image image.GenerateBinaryImage(buffer, 0); // Write the buffer to the parsing queue Write(buffer, 0, length); } finally { if (buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
public void TestArrayOfIntCompressionOnRandomData() { StringBuilder results = new StringBuilder(); MemoryStream buffer = new MemoryStream(); Random rnd = new Random(); uint value; for (int i = 0; i < TotalTestSampleSize; i++) { value = (uint)(rnd.NextDouble() * 100000); buffer.Write(BitConverter.GetBytes(value), 0, 4); } // Add one byte of extra space to accommodate compression algorithm buffer.WriteByte(0xFF); byte[] arrayOfInts = buffer.ToArray(); byte[] copy = arrayOfInts.BlockCopy(0, arrayOfInts.Length); int bufferLen = arrayOfInts.Length; int dataLen = bufferLen - 1; int gzipLen = arrayOfInts.Compress().Length; int compressedLen, decompressedLen, maxDecompressedLen; Ticks compressTime, decompressTime; Ticks stopTime, startTime; bool lossless; // Make sure a buffer exists in the buffer pool so that operation time will not be skewed by buffer initialization: startTime = DateTime.UtcNow.Ticks; BufferPool.ReturnBuffer(BufferPool.TakeBuffer(dataLen + TotalTestSampleSize)); stopTime = DateTime.UtcNow.Ticks; results.AppendFormat("Buffer Pool initial take time: {0}\r\n", (stopTime - startTime).ToElapsedTimeString(4)); startTime = DateTime.UtcNow.Ticks; BufferPool.ReturnBuffer(BufferPool.TakeBuffer(dataLen + TotalTestSampleSize)); stopTime = DateTime.UtcNow.Ticks; results.AppendFormat("Buffer Pool cached take time: {0}\r\n\r\n", (stopTime - startTime).ToElapsedTimeString(4)); startTime = DateTime.UtcNow.Ticks; compressedLen = PatternCompressor.CompressBuffer(arrayOfInts, 0, dataLen, bufferLen, 31); stopTime = DateTime.UtcNow.Ticks; compressTime = stopTime - startTime; maxDecompressedLen = PatternDecompressor.MaximumSizeDecompressed(compressedLen); if (arrayOfInts.Length < maxDecompressedLen) { byte[] temp = new byte[maxDecompressedLen]; Buffer.BlockCopy(arrayOfInts, 0, temp, 0, compressedLen); arrayOfInts = temp; } startTime = DateTime.UtcNow.Ticks; decompressedLen = PatternDecompressor.DecompressBuffer(arrayOfInts, 0, compressedLen, maxDecompressedLen); stopTime = DateTime.UtcNow.Ticks; decompressTime = stopTime - startTime; lossless = decompressedLen == dataLen; for (int i = 0; lossless && i < Math.Min(decompressedLen, dataLen); i++) { lossless = arrayOfInts[i] == copy[i]; } // Publish results to debug window results.AppendFormat("Results of floating point compression algorithm over sequential data:\r\n\r\n"); results.AppendFormat("Total number of samples: \t{0:#,##0}\r\n", TotalTestSampleSize); results.AppendFormat("Total number of bytes: \t{0:#,##0}\r\n", dataLen); results.AppendFormat("Total compression time: \t{0}\r\n", compressTime.ToElapsedTimeString(4)); results.AppendFormat("Compression speed: \t{0:#,##0.0000} MB/sec\r\n", (dataLen / (double)SI2.Mega) / compressTime.ToSeconds()); results.AppendFormat("Total decompression time: \t{0}\r\n", decompressTime.ToElapsedTimeString(4)); results.AppendFormat("Decompression speed: \t{0:#,##0.0000} MB/sec\r\n", (dataLen / (double)SI2.Mega) / decompressTime.ToSeconds()); results.AppendFormat("Compression results: \t{0:0.00%}\r\n", (dataLen - compressedLen) / (double)dataLen); results.AppendFormat("Standard gzip results: \t{0:0.00%}\r\n", (dataLen - gzipLen) / (double)dataLen); Debug.WriteLine(results.ToString()); Assert.AreEqual(dataLen, decompressedLen); Assert.IsTrue(lossless); }
// Handles the dump timer's Tick event. private void DumpTimer_Tick(object sender, EventArgs e) { byte[] buffer = BufferPool.TakeBuffer(65536); try { List <IMeasurement[]> dumpMeasurements = new List <IMeasurement[]>(); IMeasurement dequeuedMeasurement; int count = 0; // Remove all the measurements from the buffer and place them in the list of samples. while (m_buffer.Count > m_numChannels) { IMeasurement[] sample = new IMeasurement[m_numChannels]; for (int i = 0; i < m_numChannels; i++) { Guid id; int index; m_buffer.TryDequeue(out dequeuedMeasurement); id = dequeuedMeasurement.ID; index = m_channelIndexes[id]; sample[index] = dequeuedMeasurement; } dumpMeasurements.Add(sample); } foreach (IMeasurement[] sample in dumpMeasurements) { // Put the sample in the buffer. for (int i = 0; i < m_numChannels; i++) { byte[] channelValue; // Assuming 16-bit integer samples for WAV files if (sample[i] != null) { channelValue = LittleEndian.GetBytes((short)sample[i].Value); } else { channelValue = new byte[2]; } Buffer.BlockCopy(channelValue, 0, buffer, count, 2); count += 2; } // If the buffer is full, send it to the // sound card and start a new buffer. if (count + (m_numChannels * 2) > buffer.Length) { m_waveProvider.AddSamples(buffer, 0, count); count = 0; } // Notify the user interface of new samples. if (OnSample != null) { const float volume = 0.000035F; float left = (sample[0] != null) ? (float)sample[0].Value : 0.0F; float right = m_numChannels > 1 ? ((sample[1] != null) ? (float)sample[1].Value : 0.0F) : left; OnSample(this, new SampleEventArgs(left * volume, right * volume)); } } // Send remaining samples to the sound card. if (count > 0) { m_waveProvider.AddSamples(buffer, 0, count); } // If the buffer was empty, we're missing a full quarter second of data! // Go back to buffering, stop the dump timer, and start the timeout timer. //if (dumpMeasurements.Count == 0 && m_dumpTimer != null) //{ // OnStateChanged(PlaybackState.Buffering); // m_dumpTimer.Stop(); // m_timeoutTimer.Start(); //} } finally { if ((object)buffer != null) { BufferPool.ReturnBuffer(buffer); } } }
public ReadFileToDatabase(BufferPool bufferPool, ITransactionalStorage storage, Stream inputStream, string filename) { this.bufferPool = bufferPool; this.inputStream = inputStream; this.storage = storage; this.filename = filename; buffer = bufferPool.TakeBuffer(StorageConstants.MaxPageSize); md5Hasher = new MD5CryptoServiceProvider(); }