Click here to Skip to main content
15,881,588 members
Articles / General Programming / File
Alternative
Tip/Trick

BufferedFileReader & BufferedFileWriter: extending BinaryReader and BinaryWriter using BufferedStream

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
17 Dec 2011CPOL 9.9K   1   2
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)


Written By
Team Leader Hawk International for Finance & Construction Ltd.
Yemen Yemen
Bachelor degree of Computer Sciences, Software developer using .NET (Visual Basic & C#).
I like the development of an Artificial Intelligence Systems, GPS Applications, GDI+, APIs, N-Tier Applications, Distributed Systems and Network Monitoring Systems.
Mobile Development (Windows Mobile & Android).

Comments and Discussions

 
GeneralReason for my vote of 5 You are absolutely right. I've never... Pin
Olivier Levrey11-Dec-11 21:49
Olivier Levrey11-Dec-11 21:49 
GeneralRe: Reason for my vote of 5You are absolutely right. I've never... Pin
Ragheed Al-Tayeb18-Mar-23 7:24
Ragheed Al-Tayeb18-Mar-23 7:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.