internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes) { Helpers.DebugAssert(buffer != null); Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length); Helpers.DebugAssert(copyFromIndex >= 0); Helpers.DebugAssert(copyBytes >= 0); // try doubling, else match int newLength = buffer.Length * 2; if (newLength < toFitAtLeastBytes) { newLength = toFitAtLeastBytes; } byte[] newBuffer = new byte[newLength]; if (copyBytes > 0) { Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes); } if (buffer.Length == BufferPool.BufferLength) { BufferPool.ReleaseBufferToPool(ref buffer); } buffer = newBuffer; }
/// <summary> /// Releases resources used by the reader, but importantly <b>does not</b> Dispose the /// underlying stream; in many typical use-cases the stream is used for different /// processes, so it is assumed that the consumer will Dispose their stream separately. /// </summary> public void Dispose() { // importantly, this does **not** own the stream, and does not dispose it source = null; model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); }
protected private override void Dispose() { base.Dispose(); // importantly, this does **not** own the stream, and does not dispose it dest = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); }
internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes) { Helpers.DebugAssert(buffer != null); Helpers.DebugAssert(toFitAtLeastBytes > buffer.Length); Helpers.DebugAssert(copyFromIndex >= 0); Helpers.DebugAssert(copyBytes >= 0); // try doubling, else match int newLength = buffer.Length * 2; if (newLength < toFitAtLeastBytes) { newLength = toFitAtLeastBytes; } try { if (copyBytes == 0) { BufferPool.ReleaseBufferToPool(ref buffer); // No need to copy, we can release immediately } byte[] newBuffer = GetCachedBuffer(toFitAtLeastBytes); if (newBuffer == null) { newBuffer = new byte[newLength]; } if (copyBytes > 0) { Helpers.BlockCopy(buffer, copyFromIndex, newBuffer, 0, copyBytes); BufferPool.ReleaseBufferToPool(ref buffer); } buffer = newBuffer; } catch (OutOfMemoryException) { // Low memory situation: flush existing buffers, save current to disk, allocate new one and read data back string tempPath = Path.GetTempFileName(); try { Flush(); File.WriteAllBytes(tempPath, buffer); // Write the current buffer to disk to be able to release the current buffer buffer = null; GC.Collect(); GC.WaitForPendingFinalizers(); buffer = new byte[(newLength + toFitAtLeastBytes) / 2]; // A bit more conservative resizing using (FileStream stream = File.OpenRead(tempPath)) { stream.Read(buffer, 0, copyBytes); // Read data back from disk } } finally { try { File.Delete(tempPath); } // Remove temporary file catch { } } } }
private void Dispose() { // importantly, this does **not** own the stream, and does not dispose it if (dest != null) { Flush(this); dest = null; } model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); }
private void Dispose() { if (this.dest != null) { ProtoWriter.Flush(this); this.dest = null; } this.model = null; BufferPool.ReleaseBufferToPool(ref this.ioBuffer); }
private void Dispose() { if (dest != null) { Flush(this); dest = null; } model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); }
public void Dispose() { this.source = null; this.model = null; BufferPool.ReleaseBufferToPool(ref this.ioBuffer); if (this.stringInterner != null) { this.stringInterner.Clear(); } if (this.netCache != null) { this.netCache.Clear(); } }
public void Dispose() { source = null; model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); if (stringInterner != null) { stringInterner.Clear(); } if (netCache != null) { netCache.Clear(); } }
internal static void Seek(Stream source, int count, byte[] buffer) { if (source.CanSeek) { source.Seek(count, SeekOrigin.Current); count = 0; } else if (buffer != null) { int num; while (count > buffer.Length && (num = source.Read(buffer, 0, buffer.Length)) > 0) { count -= num; } while (count > 0 && (num = source.Read(buffer, 0, count)) > 0) { count -= num; } } else { buffer = BufferPool.GetBuffer(); try { int num2; while (count > buffer.Length && (num2 = source.Read(buffer, 0, buffer.Length)) > 0) { count -= num2; } while (count > 0 && (num2 = source.Read(buffer, 0, count)) > 0) { count -= num2; } } finally { BufferPool.ReleaseBufferToPool(ref buffer); } } if (count <= 0) { return; } throw EoF(null); }
private void Dispose() { // importantly, this does **not** own the stream, and does not dispose it #if OPTIMIZATION_NETWORK if (null != _destBuf) { Flush(this); _destBuf = null; } #else if (dest != null) { Flush(this); dest = null; } #endif model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); }
internal static void Seek(Stream source, int count, byte[] buffer) { if (source.get_CanSeek()) { source.Seek((long)count, 1); count = 0; } else if (buffer != null) { int num; while (count > buffer.Length && (num = source.Read(buffer, 0, buffer.Length)) > 0) { count -= num; } while (count > 0 && (num = source.Read(buffer, 0, count)) > 0) { count -= num; } } else { buffer = BufferPool.GetBuffer(); try { int num2; while (count > buffer.Length && (num2 = source.Read(buffer, 0, buffer.Length)) > 0) { count -= num2; } while (count > 0 && (num2 = source.Read(buffer, 0, count)) > 0) { count -= num2; } } finally { BufferPool.ReleaseBufferToPool(ref buffer); } } if (count > 0) { throw ProtoReader.EoF(null); } }
internal static void Seek(Stream source, int count, byte[] buffer) { if (source.CanSeek) { source.Seek(count, SeekOrigin.Current); count = 0; } else if (buffer != null) { int bytesRead; while (count > buffer.Length && (bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { count -= bytesRead; } while (count > 0 && (bytesRead = source.Read(buffer, 0, count)) > 0) { count -= bytesRead; } } else // borrow a buffer { buffer = BufferPool.GetBuffer(); try { int bytesRead; while (count > buffer.Length && (bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { count -= bytesRead; } while (count > 0 && (bytesRead = source.Read(buffer, 0, count)) > 0) { count -= bytesRead; } } finally { BufferPool.ReleaseBufferToPool(ref buffer); } } if (count > 0) { throw EoF(null); } }
internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes) { int num = buffer.Length * 2; if (num < toFitAtLeastBytes) { num = toFitAtLeastBytes; } byte[] array = new byte[num]; if (copyBytes > 0) { Helpers.BlockCopy(buffer, copyFromIndex, array, 0, copyBytes); } if (buffer.Length == 1024) { BufferPool.ReleaseBufferToPool(ref buffer); } buffer = array; }
private void Dispose() { // importantly, this does **not** own the stream, and does not dispose it if (dest != null) { Flush(this); dest = null; } model = null; BufferPool.ReleaseBufferToPool(ref ioBuffer); if (netCache != null) { netCache.Clear(); } if (recursionStack != null) { recursionStack.Clear(); } Recycle(this); }
internal static void ResizeAndFlushLeft(ref byte[] buffer, int toFitAtLeastBytes, int copyFromIndex, int copyBytes) { int length = (int)buffer.Length * 2; if (length < toFitAtLeastBytes) { length = toFitAtLeastBytes; } byte[] numArray = new byte[length]; if (copyBytes > 0) { Helpers.BlockCopy(buffer, copyFromIndex, numArray, 0, copyBytes); } if ((int)buffer.Length == 1024) { BufferPool.ReleaseBufferToPool(ref buffer); } buffer = numArray; }