Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / file

BufferedFileReader & BufferedFileWriter: extending BinaryReader and BinaryWriter using BufferedStream

5.00/5 (3 votes)
17 Dec 2011CPOL 10.4K  
Hi Olivier,First, thank you for your helpful post here and I'd like introduce some suggestion that might help to increase the performance of the Read & Write methods (of arrays such as ReadIntArray, ReadFloatArray and WriteIntArray, .... etc.). My suggestion is to make use of the...

Hi Olivier,
First, thank you for your helpful post here and I'd like introduce some suggestion that might help to increase the performance of the Read & Write methods (of arrays such as ReadIntArray, ReadFloatArray and WriteIntArray, .... etc.). My suggestion is to make use of the System.Buffer.BlockCopy() that can be used to copy the items from some numeric type array to another one. For example, your ReadIntArray() code might be replaced by:
 

public int[] ReadIntArray(int length)
{
    int[] array = new int[length];
    byte[] bytes = ReadBytes(length * 4);
    System.Buffer.BlockCopy(bytes, 0, array, 0, bytes.Length);
    return array;
}

 

 

and the WriteIntArray() should be replaced by:

 

 

 

 

 

 

 

 

 

 

 

public void Write(int[] array)
{
    byte[] bytes = new byte[4 * array.Length];
    System.Buffer.BlockCopy(bytes, 0, bytes, 0, bytes.Length);
    Write(bytes);
}

 

 

I hope that will help... and thanks again for your great post.
 

 

 

 

 

 

 

 

License

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