Last active
December 29, 2015 08:59
-
-
Save omerfarukz/7646814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.IO; | |
namespace Generators.RandomStream.Demo | |
{ | |
class Program | |
{ | |
static Random _random = new Random(); | |
static void Main(string[] args) | |
{ | |
string filePathFormat = @"c:\tests\file_{0}_{1}.bin"; | |
foreach (var f in Directory.EnumerateFiles(Path.GetDirectoryName(string.Format(filePathFormat, 0, 0)))) | |
{ | |
File.Delete(f); | |
} | |
//Generate 0-1mb random file 20 file | |
for (int i = 0; i < 20; i++) | |
{ | |
var sizeOfFile = _random.Next(0, 1048576); | |
using (var orginal = new FileStream(string.Format(filePathFormat, i, 0), FileMode.OpenOrCreate, FileAccess.Write)) | |
{ | |
for (int j = 0; j < sizeOfFile; j++) | |
{ | |
orginal.WriteByte((byte)_random.Next(0, 255)); | |
} | |
} | |
var modifiedFileSize = _random.Next(sizeOfFile / 2, sizeOfFile * 2); | |
using (var orginal = new FileStream(string.Format(filePathFormat, i, 0), FileMode.Open, FileAccess.Read)) | |
{ | |
using (var modified = new FileStream(string.Format(filePathFormat, i, 1), FileMode.CreateNew, FileAccess.Write)) | |
{ | |
var b = 0; | |
var k = 0; | |
while ((--modifiedFileSize) > 0) | |
{ | |
b = orginal.ReadByte(); | |
if (modifiedFileSize < k) | |
break; | |
var rnd = _random.Next(0, 100); | |
if (rnd % 17 == 0) | |
modified.WriteByte((byte)_random.Next(0, 255)); | |
else if (rnd > 80) | |
continue; | |
else | |
modified.WriteByte((byte)b); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment