0

I'm working on C# project and I want to read a single file from multiple threads using streams in the following manner:

  1. A file is logically divided into "chunks" of fixed size.
  2. Each thread gets it's own stream representing a "chunk".

The problem that I want use a Stream interface and I want to limit the size of each chunk so that the corresponding stream "ends" when it reaches the chunk size.

Is there something available in standard library or my only option is to write my own implementation of Stream?

Phoenix
  • 1,045
  • 1
  • 14
  • 22
Anton
  • 1
  • 2
  • You could use [`MemoryMappedFiles`](http://msdn.microsoft.com/en-us/library/vstudio/system.io.memorymappedfiles.memorymappedfile) (_"Memory-mapped files can also be shared across multiple processes."_). Read: [When to use memory-mapped files?](http://stackoverflow.com/questions/1859213/when-to-use-memory-mapped-files) – Tim Schmelter Jun 18 '14 at 09:05
  • Depending on usage it might be easier to override either `BinaryReader` or `StreamReader` – James Jun 18 '14 at 09:07

1 Answers1

1

There is an overload in the Streamreader class for Streamreader.Read which allows you to limit the amount of characters read. An example can be found here: http://msdn.microsoft.com/en-us/library/9kstw824.aspx

The line you are looking for is sr.Read(c, 0, c.Length); You simply set up a char array and decide on the maximum amount of characters that are going to be read (the third argument).

Haris
  • 778
  • 2
  • 9
  • 20