0
Answer

Way's to read big files & Performance

Ask a question
biny balan

biny balan

11y
1.1k
1
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();
                }
            }
        }