Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
i want to read 1 KB data each time from byte array of 678560 size.

if in end 1 KB is not available then only read remaining bytes.

please help me.
Posted
Updated 18-Nov-13 21:36pm
v3
Comments
♥…ЯҠ…♥ 19-Nov-13 2:13am    
what is the size = 678560b ?? 678560kb ?? 678560mb;
Sergey Alexandrovich Kryukov 19-Nov-13 2:46am    
Help with what? Any particular problem? http://whathaveyoutried.com so far? You cannot expect that someone do all your work for you.
And what does it mean, "each time"? Each time of what?
—SA

Iterate over the source array of bytes, going by steps of 1024 bytes.
For each iteration, copy the data from the current point up until current+1024 into a target array, and while doing this make sure you don't copy past the end of the source buffer.

Using yield return and LINQ this can be achieved in using very little code:

C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace Read1k {
    class Program {

        static IEnumerable<byte[]> SomeReadMethod(byte[] source, int chunkSize) {
            for (var i = 0; i < source.Length; i += chunkSize) {
                yield return source.Skip(i).Take(chunkSize).ToArray();
            }
        }

        static void Main() {
            var source = new byte[678560];
            foreach (var chunk in SomeReadMethod(source, 1024)) {
                Console.WriteLine("Read {0} bytes", chunk.Length);
            }
        }
    }
}


For each of the iterations done in Main, the chunk variable will hold a byte array with either 1024 bytes or whatever is left on the source array.

The LINQ methods Skip and Take will (obviously) skip ahead i bytes and then take the next chunkSize bytes.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
BillWoodruff 19-Nov-13 3:34am    
+5 !
Fredrik's solution is correct, but with just a few changes it can be generalized:
C#
public static class ChunkingExtensionMethod
{
  public static IEnumerable<IEnumerable<T>> Chunkify<T>(this IEnumerable<T> source, int chunkSize)
  {
    if (chunkSize <= 0)
      throw new ArgumentOutOfRangeException("chunkSize", "chunkSize must be greater than zero");
    while (source.Any())
    {
      yield return source.Take(chunkSize);
      source = source.Skip(chunkSize);
    }
  }
}

It will work with any source that implements IEnumerable<T> and doesn't make an array that it might not need.
Each of the elements of the returned enumeration can be converted to another collection type using .ToArray(), .ToList(), etc.

Use this like:
C#
var source = new byte[678560];
foreach (var chunk in source.Chunkify(1024))
{
  Console.WriteLine("Read {0} bytes IEnumerable", chunk.Count());
  Console.WriteLine("Read {0} bytes Array", chunk.ToArray().Length);
  Console.WriteLine("Read {0} bytes List", chunk.ToList().Count);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900