public static ReadOnlyMemorySlice <byte> LockExtractMemory(IBinaryReadStream stream, long min, long max) { lock (stream) { stream.Position = min; var size = checked ((int)(max - min)); if (stream.IsPersistantBacking) { return(stream.ReadMemory(size)); } else { byte[] data = new byte[size]; stream.Read(data); return(data); } } }