66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
|
|
namespace Converting
|
|
{
|
|
public class Conv
|
|
{
|
|
public delegate void PositionEventHandler(object sender, int pos);
|
|
public event PositionEventHandler OnNewPosition;
|
|
public delegate void FinishEventHandler(object sender, int exitcode);
|
|
public event FinishEventHandler OnFinish;
|
|
public void ConvertFile(String input, String output, bool encode)
|
|
{
|
|
BinaryReader reader = new BinaryReader(File.Open(input, FileMode.Open));
|
|
BinaryWriter writer = new BinaryWriter(File.Open(output, FileMode.Create));
|
|
Converter conv = new Converter();
|
|
int i = 0;
|
|
try
|
|
{
|
|
while (true)
|
|
{
|
|
byte inp = reader.ReadByte();
|
|
byte oup = 0;
|
|
if (encode)
|
|
{
|
|
oup = conv.encoding((byte)i++, inp);
|
|
}
|
|
else
|
|
{
|
|
oup = conv.decoding((byte)i++, inp);
|
|
}
|
|
if (this.OnNewPosition != null)
|
|
{
|
|
this.OnNewPosition(this, i);
|
|
}
|
|
writer.Write(oup);
|
|
//System.Threading.Thread.Sleep(1);
|
|
}
|
|
}
|
|
catch (EndOfStreamException) { }
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
if (this.OnFinish != null)
|
|
{
|
|
this.OnFinish(this, 1);
|
|
reader.Close();
|
|
writer.Close();
|
|
return;
|
|
}
|
|
}
|
|
if (this.OnFinish != null)
|
|
{
|
|
this.OnFinish(this, 0);
|
|
reader.Close();
|
|
writer.Close();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|