Created
August 31, 2018 00:07
-
-
Save mjs3339/a8a9288f758819a43d0541f59c4607be to your computer and use it in GitHub Desktop.
C# File Helper Class Stream, Action, Copy, Read, Write
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Example: | |
/// var fh = new FileHelperEx(); | |
/// var hash = new SHA512CryptoServiceProvider(); | |
/// hash.Initialize(); | |
/// fh.ReadStream(fileName, buffer => hash.TransformBlock(buffer, 0, buffer.Length, null, 0)); | |
/// hash.Final(); | |
/// var sha512 = new BigInteger(hash.Hash); | |
/// </summary> | |
public class FileHelperEx | |
{ | |
public int BufferSize = 80 * 1024; | |
public void ReadStream(string path, Action<byte[]> action) | |
{ | |
using(var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) | |
{ | |
ProcessStream(stream, action); | |
} | |
} | |
public void WriteStream(string path, Action<byte[]> action) | |
{ | |
using(var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write, 4096, true)) | |
{ | |
ProcessStream(stream, action); | |
} | |
} | |
public void Copy(string InPath, string OutPath) | |
{ | |
using(var ins = new FileStream(InPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true)) | |
{ | |
using(var ops = new FileStream(OutPath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) | |
{ | |
CopyStream(ins, ops); | |
} | |
} | |
} | |
public void ProcessStream(Stream stream, Action<byte[]> action) | |
{ | |
var DataQueue = new ConcurrentQueue<byte[]>(); | |
var DataReady = new AutoResetEvent(false); | |
var DataProcessed = new AutoResetEvent(false); | |
var ReadDataTask = Task.Factory.StartNew(() => | |
{ | |
while(true) | |
{ | |
var Data = new byte[BufferSize]; | |
var BytesRead = stream.Read(Data, 0, Data.Length); | |
if(BytesRead != BufferSize) | |
Data = Data.SubArray(0, BytesRead); | |
DataQueue.Enqueue(Data); | |
DataReady.Set(); | |
if(BytesRead != BufferSize) | |
break; | |
DataProcessed.WaitOne(); | |
} | |
}); | |
var ProcessDataTask = Task.Factory.StartNew(() => | |
{ | |
byte[] Data; | |
do | |
{ | |
DataReady.WaitOne(); | |
DataQueue.TryDequeue(out Data); | |
DataProcessed.Set(); | |
action(Data); | |
if(Data.Length != BufferSize) | |
break; | |
} while(Data.Length == BufferSize); | |
}); | |
ReadDataTask.Wait(); | |
ProcessDataTask.Wait(); | |
} | |
public void CopyStream(Stream InStream, Stream OutStream) | |
{ | |
var DataQueue = new ConcurrentQueue<byte[]>(); | |
var DataReady = new AutoResetEvent(false); | |
var DataProcessed = new AutoResetEvent(false); | |
var ReadDataTask = Task.Factory.StartNew(() => | |
{ | |
while(true) | |
{ | |
var Data = new byte[BufferSize]; | |
var BytesRead = InStream.Read(Data, 0, Data.Length); | |
if(BytesRead != BufferSize) | |
Data = Data.SubArray(0, BytesRead); | |
DataQueue.Enqueue(Data); | |
DataReady.Set(); | |
if(BytesRead != BufferSize) | |
break; | |
DataProcessed.WaitOne(); | |
} | |
}); | |
var ProcessDataTask = Task.Factory.StartNew(() => | |
{ | |
byte[] Data; | |
do | |
{ | |
DataReady.WaitOne(); | |
DataQueue.TryDequeue(out Data); | |
DataProcessed.Set(); | |
OutStream.Write(Data, 0, Data.Length); | |
if(Data.Length != BufferSize) | |
break; | |
} while(Data.Length == BufferSize); | |
}); | |
ReadDataTask.Wait(); | |
ProcessDataTask.Wait(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment