TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
biny balan
NA
3
0
Way's to read big files & Performance
Feb 25 2013 1:11 AM
I am using the following code to read and write file. I am dividing the entire byte to small chunks. But my problems is that it will take more time. Let me know is there any way to increes the performance
public static byte[] ReadDocumentAsByteChunks(string documentPath)
{
StreamReader streamReader = null;
byte[] documentBytes = null;
try
{
streamReader = new StreamReader(documentPath);
documentBytes = new byte[(int)streamReader.BaseStream.Length];
long documentByteLength = documentBytes.Length;
byte[] documentBytesChunk = new byte[ChunkSize];
int offset = 0;
int actualLength;
int currentStreamLength;
while ((currentStreamLength = streamReader.BaseStream.Read(documentBytesChunk, 0, ChunkSize)) > 0)
{
if (documentByteLength > ChunkSize)
actualLength = ChunkSize;
else
actualLength = (int)documentByteLength;
Array.Copy(documentBytesChunk, 0, documentBytes, offset, actualLength);
documentByteLength -= currentStreamLength;
offset = offset + currentStreamLength;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (streamReader != null)
{
streamReader.Close();
streamReader.Dispose();
}
}
return documentBytes;
}
public static void WriteDocumentChunks(Object fileStreamOrLocation, byte[] documentBytes)
{
StreamWriter streamWriter = null;
try
{
if (fileStreamOrLocation.GetType().ToString() == "System.IO.FileStream")
{
streamWriter = new StreamWriter((FileStream)fileStreamOrLocation);
}
else if (fileStreamOrLocation.GetType().ToString() == "System.String")
{
streamWriter = new StreamWriter((string)fileStreamOrLocation);
}
int offset = 0;
int totalBytes = (int)documentBytes.Length;
while (totalBytes > 0)
{
int actualChunkSize = Math.Min(ChunkSize, totalBytes);
streamWriter.BaseStream.Write(documentBytes, offset, actualChunkSize);
totalBytes -= actualChunkSize;
offset += actualChunkSize;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter.Dispose();
}
}
}
Reply
Answers (
0
)
What is iPhone SDK?
How to create a customized calendar with no Sat and Sun?