diff --git a/X10D/src/ByteExtensions.cs b/X10D/src/ByteExtensions.cs index 7f03889..0469cb4 100644 --- a/X10D/src/ByteExtensions.cs +++ b/X10D/src/ByteExtensions.cs @@ -24,6 +24,28 @@ return BitConverter.ToString(bytes.ToArray()); } + /// + /// Splits into chunks of size . + /// + /// The collection to split. + /// The maximum length of the nested collection. + /// Returns an of of + /// values. + public static IEnumerable> Chunkify(this IEnumerable bytes, int chunkSize) + { + IEnumerable enumerable = bytes as byte[] ?? bytes.ToArray(); + long count = enumerable.LongCount(); + List> chunks = new List>(); + chunkSize = chunkSize.Clamp(1, enumerable.Count()); + + for (int i = 0; i < (int) (count / chunkSize); i++) + { + chunks.Add(enumerable.Skip(i * chunkSize).Take(chunkSize)); + } + + return chunks.ToArray(); + } + /// /// Converts the [] to an . ///