/// <summary> /// Method for the background thread that generates random text strings /// </summary> /// <remarks> /// <para> /// The RandomTextInputStream is often used to generate /// large texts or strings. There's an advantage to using a /// background thread to generating those texts while the /// foreground is reading from the text /// </para> /// </remarks> private void _Producer(object state) { int nowServing = 0; try { do { _randomText[nowServing] = _rtg.Generate(_maxChunkSize); _slurps++; _needData.WaitOne(); if (_isDisposed) { break; } _readyIndex = nowServing; nowServing = 1 - nowServing; _haveData.Set(); } while (true); } catch (System.Exception) { } _producerDone.Set(); }
internal static void CreateAndFillFileText(string filename, Int64 size, System.Action <Int64> update) { Int64 bytesRemaining = size; if (size > 128 * 1024) { RandomTextGenerator rtg = new RandomTextGenerator(); // fill the file with text data, selecting large blocks at a time using (StreamWriter sw = File.CreateText(filename)) { do { string generatedText = rtg.Generate(32 * 1024); sw.Write(generatedText); sw.Write("\n\n"); bytesRemaining -= (generatedText.Length + 2); if (update != null) { update(size - bytesRemaining); } } while (bytesRemaining > 0); } } else { // fill the file with text data, selecting one word at a time using (StreamWriter sw = File.CreateText(filename)) { do { // pick a word at random string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)]; if (bytesRemaining < selectedWord.Length + 1) { sw.Write(selectedWord.Substring(0, (int)bytesRemaining)); bytesRemaining = 0; } else { sw.Write(selectedWord); sw.Write(" "); bytesRemaining -= (selectedWord.Length + 1); } if (update != null) { update(size - bytesRemaining); } } while (bytesRemaining > 0); sw.Close(); } } }
private byte[] GetNewText() { _gnt++; int nowServing = _rnd.Next(_chunks); if (_randomText[nowServing] == null) { _randomText[nowServing] = _encoding.GetBytes(_rtg.Generate(_chunkSize)); } return(_randomText[nowServing]); }
public void SortedSave() { var rtg = new RandomTextGenerator(); WriteDelegate writer = (name, stream) => { byte[] buffer = System.Text.Encoding.ASCII.GetBytes(rtg.Generate(_rnd.Next(2000) + 200)); stream.Write(buffer, 0, buffer.Length); }; int numEntries = _rnd.Next(256) + 48; // Two trials, one with sorted output, and the other with non-sorted output. for (int m = 0; m < 2; m++) { string zipFileToCreate = Path.Combine(TopLevelDir, String.Format("SortedSave-{0}.zip", m)); using (var zip = new ZipFile()) { for (int i = 0; i < numEntries; i++) { // I need the randomness in the first part, to force the sort. string filename = String.Format("{0}-{1:000}.txt", TestUtilities.GenerateRandomAsciiString(6), i); zip.AddEntry(filename, writer); } zip.SortEntriesBeforeSaving = (m == 1); zip.Save(zipFileToCreate); } using (var zip = FileSystemZip.Read(zipFileToCreate)) { bool sorted = true; for (int i = 0; i < zip.Entries.Count - 1 && sorted; i++) { for (int j = i; j < zip.Entries.Count && sorted; j++) { if (String.Compare(zip[i].FileName, zip[j].FileName, StringComparison.OrdinalIgnoreCase) > 0) { sorted = false; } } } Assert.IsTrue((((m == 1) && sorted) || ((m == 0) && !sorted)), "Unexpected sort results"); } } }
internal static void CreateAndFillFileText(string filename, Int64 size, Action <Int64> update) { Int64 bytesRemaining = size; if (size > 128 * 1024) { var rnd = new System.Random(); RandomTextGenerator rtg = new RandomTextGenerator(); int chunkSize = 48 * 1024; int variationSize = 2 * 1024; var newLinePair = System.Text.Encoding.ASCII.GetBytes("\n\n"); int nCycles = 0; // fill the file with text data, selecting large blocks at a time var fodder = new byte[32][]; using (var fs = File.Create(filename)) { do { int n = rnd.Next(fodder.Length); if (fodder[n] == null) { string generatedText = rtg.Generate(chunkSize); fodder[n] = System.Text.Encoding.ASCII.GetBytes(generatedText); } var bytes = fodder[n]; int len = bytes.Length - rnd.Next(variationSize); fs.Write(bytes, 0, len); bytesRemaining -= len; fs.Write(newLinePair, 0, newLinePair.Length); bytesRemaining -= newLinePair.Length; nCycles++; if ((nCycles % 1024) == 0) { if (update != null) { update(size - bytesRemaining); } } } while (bytesRemaining > 0); } } else { // fill the file with text data, selecting one word at a time using (StreamWriter sw = File.CreateText(filename)) { do { // pick a word at random string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)]; if (bytesRemaining < selectedWord.Length + 1) { sw.Write(selectedWord.Substring(0, (int)bytesRemaining)); bytesRemaining = 0; } else { sw.Write(selectedWord); sw.Write(" "); bytesRemaining -= (selectedWord.Length + 1); } if (update != null) { update(size - bytesRemaining); } } while (bytesRemaining > 0); sw.Close(); } } }
internal static void CreateAndFillFileText(string filename, Int64 size, Action<Int64> update) { Int64 bytesRemaining = size; if (size > 128 * 1024) { var rnd = new System.Random(); RandomTextGenerator rtg = new RandomTextGenerator(); int chunkSize = 48 * 1024; int variationSize = 2 * 1024; var newLinePair = Encoding.ASCII.GetBytes("\n\n"); int nCycles = 0; // fill the file with text data, selecting large blocks at a time var fodder = new byte[32][]; using (var fs = File.Create(filename)) { do { int n = rnd.Next(fodder.Length); if (fodder[n] == null) { string generatedText = rtg.Generate(chunkSize); fodder[n] = Encoding.ASCII.GetBytes(generatedText); } var bytes = fodder[n]; int len = bytes.Length - rnd.Next(variationSize); fs.Write(bytes,0,len); bytesRemaining -= len; fs.Write(newLinePair, 0, newLinePair.Length); bytesRemaining -= newLinePair.Length; nCycles++; if ((nCycles % 1024) == 0) { if (update != null) update(size - bytesRemaining); } } while (bytesRemaining > 0); } } else { // fill the file with text data, selecting one word at a time using (StreamWriter sw = File.CreateText(filename)) { do { // pick a word at random string selectedWord = LoremIpsumWords[_rnd.Next(LoremIpsumWords.Length)]; if (bytesRemaining < selectedWord.Length + 1) { sw.Write(selectedWord.Substring(0, (int)bytesRemaining)); bytesRemaining = 0; } else { sw.Write(selectedWord); sw.Write(" "); bytesRemaining -= (selectedWord.Length + 1); } if (update != null) update(size - bytesRemaining); } while (bytesRemaining > 0); sw.Close(); } } }