public static void ResizeAndDismiss <T>(this IIOStream <T> stream, int length, Func <int, T> constructor) { var initialPosition = stream.Length; stream.Length = length; var buffer = MemoryPool <T> .GetArray(); try { using (var writer = stream.GetWriter()) { writer.Position = initialPosition; var numSlicesToWrite = writer.Length - writer.Position; while (numSlicesToWrite > 0) { var blockSize = Math.Min(StreamUtils.BUFFER_SIZE, numSlicesToWrite); initialPosition = writer.Position; for (int i = 0; i < blockSize; i++) { buffer[i] = constructor(initialPosition + i); } numSlicesToWrite -= writer.Write(buffer, 0, blockSize); } } } finally { MemoryPool <T> .PutArray(buffer); } }
public static void Resize <T>(this IIOStream <T> stream, int length, Func <T> constructor, Action <T> destructor) { var buffer = MemoryPool <T> .GetArray(); try { using (var reader = stream.GetReader()) { reader.Position = length; var numSlicesToRead = reader.Length - reader.Position; while (numSlicesToRead > 0) { var blockSize = reader.Read(buffer, 0, StreamUtils.BUFFER_SIZE); for (int i = 0; i < blockSize; i++) { if (buffer[i] != null) { destructor(buffer[i]); } } numSlicesToRead -= blockSize; } } var initialPosition = stream.Length; stream.Length = length; using (var writer = stream.GetWriter()) { writer.Position = initialPosition; var numSlicesToWrite = writer.Length - writer.Position; while (numSlicesToWrite > 0) { var blockSize = Math.Min(StreamUtils.BUFFER_SIZE, numSlicesToWrite); for (int i = 0; i < blockSize; i++) { buffer[i] = constructor(); } numSlicesToWrite -= writer.Write(buffer, 0, blockSize); } } } finally { MemoryPool <T> .PutArray(buffer); } }
void WriteStream <T>(IInStream <T> source, IIOStream <IInStream <T> > dest, int index) { if (dest.Length < (index + 1)) { dest.Length = index + 1; } using (var w = dest.GetWriter()) { w.Position = index; if (source == null) { w.Write(StreamUtils.GetEmptyStream <T>()); } else { w.Write(source); } } }