1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 23:58:48 +00:00

Add IEnumerable<byte>.Chunkify(int)

This commit is contained in:
Oliver Booth 2019-12-24 16:16:40 +00:00
parent ec3ee98c85
commit 05edb47f92
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0

View File

@ -24,6 +24,28 @@
return BitConverter.ToString(bytes.ToArray());
}
/// <summary>
/// Splits <paramref name="bytes"/> into chunks of size <paramref name="chunkSize"/>.
/// </summary>
/// <param name="bytes">The collection to split.</param>
/// <param name="chunkSize">The maximum length of the nested <see cref="Byte"/> collection.</param>
/// <returns>Returns an <see cref="IEnumerable{T}"/> of <see cref="IEnumerable{T}"/> of <see cref="Byte"/>
/// values.</returns>
public static IEnumerable<IEnumerable<byte>> Chunkify(this IEnumerable<byte> bytes, int chunkSize)
{
IEnumerable<byte> enumerable = bytes as byte[] ?? bytes.ToArray();
long count = enumerable.LongCount();
List<IEnumerable<byte>> chunks = new List<IEnumerable<byte>>();
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();
}
/// <summary>
/// Converts the <see cref="Byte"/>[] to an <see cref="Int16"/>.
/// </summary>