I have to monitor an url that keeps streaming status of the system.
If i issue following command from Windows CMD curl.exe http://localhost:8081/statusstream, I keep getting output from stream:
[02:10:51.021] Starting to execute testcase
[02:10:51.021] Test01: Preparing
[02:11:03.089] Test01: Executing 1/10
[02:11:11.109] Test01: Executing 2/10
[02:11:19.126] Test01: Executing 3/10
[02:11:27.145] Test01: Executing 4/10
[02:11:35.163] Test01: Executing 5/10
[02:11:43.181] Test01: Executing 6/10
[02:11:51.198] Test01: Executing 7/10
[02:11:59.220] Test01: Executing 8/10
[02:12:07.237] Test01: Executing 9/10
[02:12:15.255] Test01: Executing 10/10
[02:12:33.274] Test01: Transferring data for analysis
[02:12:41.562] WARNING: TestSequencer: No data found
[02:12:41.563] Test01: Analyze results
[02:12:42.064] Test01: Finished executing
[02:12:42.064] Finished executing testcase
However when I run the same command using C#, I don't get any standard output, all I get is standard error. Following is my C# code
ProcessStartInfo _processStartInfo = new ProcessStartInfo();
_processStartInfo.RedirectStandardOutput = true;
_processStartInfo.RedirectStandardError = true;
_processStartInfo.UseShellExecute = false;
_processStartInfo.CreateNoWindow = true;
_processStartInfo.FileName = "curl.exe"
_processStartInfo.Arguments = "http://localhost:8081/statusstream";
_process = new Process();
_process.StartInfo = _processStartInfo;
_process.EnableRaisingEvents = true;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.RedirectStandardError = true;
_process.ErrorDataReceived += _process_ErrorDataReceived;
_process.OutputDataReceived += _process_OutputDataReceived;
_process.Exited += _process_Exited;
_process.Start();
_process.BeginOutputReadLine();
_process.BeginErrorReadLine();
The following method is always being hit, but not the the one which is waiting for StandardOutput
private void _process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
this.ConsoleLog = string.Format("[StdErr] {0}",e.Data);
}
Output that i get from running CURL command from C# is:
[StdErr] % Total % Received % Xferd Average Speed Time Time Time Current
[StdErr] Dload Upload Total Spent Left Speed
[StdErr] 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
[StdErr] 100 40 0 40 0 0 33 0 --:--:-- 0:00:01 --:--:-- 33
[StdErr] 100 40 0 40 0 0 18 0 --:--:-- 0:00:02 --:--:-- 18
[StdErr] 100 40 0 40 0 0 12 0 --:--:-- 0:00:03 --:--:-- 12
[StdErr] 100 40 0 40 0 0 9 0 --:--:-- 0:00:04 --:--:-- 9
[StdErr] 100 40 0 40 0 0 7 0 --:--:-- 0:00:05 --:--:-- 7
[StdErr] 100 40 0 40 0 0 6 0 --:--:-- 0:00:06 --:--:-- 0
[StdErr] 100 40 0 40 0 0 5 0 --:--:-- 0:00:07 --:--:-- 0
[StdErr] 100 40 0 40 0 0 4 0 --:--:-- 0:00:08 --:--:-- 0
[StdErr] 100 40 0 40 0 0 4 0 --:--:-- 0:00:09 --:--:-- 0
[StdErr] 100 40 0 40 0 0 3 0 --:--:-- 0:00:10 --:--:-- 0
[StdErr] 100 40 0 40 0 0 3 0 --:--:-- 0:00:11 --:--:-- 0
[StdErr] 100 40 0 40 0 0 3 0 --:--:-- 0:00:12 --:--:-- 0
[StdErr] 100 112 0 112 0 0 16 0 --:--:-- 0:00:06 --:--:-- 16
[StdErr] 100 250 0 250 0 0 32 0 --:--:-- 0:00:07 --:--:-- 47
[StdErr] 100 250 0 250 0 0 29 0 --:--:-- 0:00:08 --:--:-- 47
[StdErr] 100 250 0 250 0 0 26 0 --:--:-- 0:00:09 --:--:-- 47
If I see the received column, its value increases from 40 to 250, that means probably something is being return from the stream.
Issuing other HTTP GET commands from CURL using C# gives proper standardoutput. But other get commands return content immediately and are not streaming continuously.
Can someone please point what I'm doing wrong.
Ok, so I'm not supposed to call cURL.exe from C# code. But is there a way where I can monitor a URL to get continuous stream of data whenever there are updates? I don't want to call HttpClient GetStringAsync in a while loop.
Related
I am trying to distribute a set of items across number of buckets. I am looking for following properties:
Bucket assignment needs to be deterministic. In different runs same
input should end up in the same bucket.
Distribution of data between buckets should be uniform.
This should work for fairly small number of inputs (e.g. if I want
to distribute 50 inputs across 25 buckets ideally each bucket will
have 2 items).
First try was to generate md5 from input data and form bucket from first bytes of md5. I am not too satisfied with uniformity. It works well when input is large but not so well for small input. E.g. distributing 100 items across 64 buckets:
List<string> l = new List<string>();
for (int i = 0; i < 100; i++)
{
l.Add(string.Format("data{0}.txt", i));
}
int[] buckets = new int[64];
var md5 = MD5.Create();
foreach (string str in l)
{
{
byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(str));
uint bucket = BitConverter.ToUInt32(hash, 0) % 64;
buckets[bucket % 64]++;
}
}
Any suggestions what could I do to achieve higher uniformity? Thanks.
Leaving aside the efficiency of using MD5 for this purpose (see the discussion here and in the marked duplicate of that question), basically the answer is that what you have is what a uniform distribution really looks like.
That might seem counter-intuitive, but it's easily demonstrable either mathematically or by experiment.
As a kind of motivating example, consider the task of choosing exactly 64 numbers in the range 0-63. The odds that you will get one per bucket are very close to 0. There are 6464 possible sequences, of which 64! contain all 64 numbers. The odds of getting one of these sequence is about one in 3.1×1026. In fact, the odds of getting a sequence in which no element appears three times is less than one in a thousand (it's about .000658). So it's almost certain that a random uniform sample of 64 numbers in the range 0-63 will have some triplets, and it's pretty likely that there will be some quadruplet. If the sample is 100 numbers, those probabilities just get even bigger.
But the maths are not so easy to compute in general, so here I chose to illustrate by experiment :-), using random.org, which is a pretty reliable source of random numbers. I asked it for 100 numbers in the range 0-63, and counted them (using bash, so my "graph" is not as pretty as yours). Here are two runs:
First run:
Random numbers:
44 17 50 11 16 4 24 29 12 36
27 32 12 63 4 30 19 60 28 39
22 40 19 16 23 2 46 31 52 41
13 2 42 17 29 39 43 9 20 50
45 40 38 33 17 45 28 6 48 12
56 26 34 33 35 40 28 44 22 10
50 55 49 43 63 62 22 50 15 52
48 54 53 26 4 53 13 56 42 60
49 30 14 55 29 62 15 13 35 40
22 38 37 36 10 36 5 41 43 53
Counts:
X X X
X XX X X XX X X X X X
X X X XX XXX X X X XXX X XX XXXXXXXX XXX XX XX X XX
X XXX XXXXXXXXX XX XXX XXXXXXXXXXXXXXXXXXXXX XXX XXXXX X XX
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
Second run:
Random numbers:
41 31 16 40 1 51 17 41 27 46
24 14 21 33 25 43 4 36 1 14
40 22 11 22 30 19 23 63 39 61
8 55 40 6 21 13 55 13 3 52
17 52 53 53 7 21 47 13 45 57
25 27 30 48 38 55 55 22 61 11
11 28 45 63 43 0 41 51 15 2
33 2 46 14 35 41 5 2 11 37
28 56 15 7 18 12 57 36 59 51
42 5 46 32 10 8 0 46 12 9
Counts:
X X X X
X X XX XX XX X X X
XXX X XX XXXXX X XX X XX X X X XX X XX XXX X X X X
XXXXXXXXXXXXXXXXXXXX XXXXX XX XXXX XXXXXXXXX XXXX XXX XXX X X X
----------------------------------------------------------------
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6
0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2 4 6 8 0 2
You could try this with your favourite random number generator, playing around with the size of the distribution. You'll get the same sort of shape.
I am using iTextSharp in my C# application. PDF reader gives error:
The document has no page root (meaning: it's an invalid PDF).
Then we again try to fetch count property using below regex:
using (StreamReader sr = new StreamReader(File.OpenRead(#"C:\test.pdf")))
{
Regex regex = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
Console.WriteLine(matches.Count);
}
But using this regex we get double count..(i.e if actual file has 6 count this regext returns count as 12)
Any solution to get page for this kind of files ?
I can not share file as it is client provided file.
On future verification on file content, i found below matches for regex:
/Type /XObject
/Type /Page
/Type /Catalog
/Type /XObject
/Type /Pages
/Type /Page
/Type /XObject
/Type /Pages
/Type /Page
/Type /XObject
/Type /Pages
/Type /Page
/Type /XObject
/Type /Pages
/Type /Page
/Type /XObject
/Type /Pages
/Type /Page
%PaperPortPDFversion1 0 obj<</Contents 10 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 2 0 R/Resources 9 0 R/Rotate 180/Type/Page>>
12 0 obj<</Contents 20 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 16 0 R/Resources 19 0 R/Rotate 180/Type/Page>>
21 0 obj<</Contents 29 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 25 0 R/Resources 28 0 R/Rotate 180/Type/Page>>
30 0 obj<</Contents 38 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 34 0 R/Resources 37 0 R/Rotate 180/Type/Page>>
39 0 obj<</Contents 47 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 43 0 R/Resources 46 0 R/Rotate 180/Type/Page>>
48 0 obj<</Contents 56 0 R/CropBox[0 0 595 842]/MediaBox[0 0 595 842]/Parent 52 0 R/Resources 55 0 R/Rotate 180/Type/Page>>
Because of last matches it gives double count then actual.(i.e 12 count instead 6 count)
Is there any solution for solving this kind of file,as this is file specific issue not with all file.
Thanks
.NET Framework 4.6, C#.
Using nAudio to read a MIDI file to convert the below into a notation structure that shows a single column with 16th notes and multiple sustain values if the note needs to be a quarter/half/full. So the result would start to look like this to display "C6" as a quarter note:
C6
S
S
S
I don't nuderstand how to read the Velocity and NoteLength properties/fields to get note duration values. Can someone point me in the right direction?
/*
Format 0, Tracks 1, Delta Ticks Per Quarter Note 480
1:1:0 0 MidiChannel
00
1:1:0 0 SequenceTrackName Classic Electric Piano
1:1:0 0 TrackInstrumentName Classic Electric Piano
1:1:0 0 SetTempo 89bpm (666667)
1:1:0 0 TimeSignature 4/4 TicksInClick:24 32ndsInQuarterNote:8
1:1:0 0 KeySignature 0 0
1:1:0 0 SmpteOffset 33:0:0:0:0
1:1:0 0 NoteOn Ch: 1 C6 Vel:87 Len: 167
1:1:240 240 NoteOn Ch: 1 C6 Vel:69 Len: 199
1:2:0 480 NoteOn Ch: 1 G6 Vel:74 Len: 149
1:2:240 720 NoteOn Ch: 1 G6 Vel:49 Len: 191
1:3:0 960 NoteOn Ch: 1 A6 Vel:65 Len: 165
1:3:240 1200 NoteOn Ch: 1 A6 Vel:50 Len: 171
1:4:0 1440 NoteOn Ch: 1 G6 Vel:89 Len: 401
*/
var dede = midiFile.DeltaTicksPerQuarterNote;
var tempolist = new List<TempoEvent>();
foreach (MidiEvent note in midiFile.Events[0])
{
if ( note.CommandCode == MidiCommandCode.NoteOn )
{
var nono = (NoteOnEvent)note;
var noname = nono.NoteName;
var notime= nono.AbsoluteTime;
double realtime = ( note.AbsoluteTime / dede ) * tempo[ tempo.Count() - 1 ].Tempo;
var nonum = nono.NoteNumber;
var nolen = nono.NoteLength;
}
}
Thanks.
I am trying to calculate the frequency of symbols in a binary file in c#, I have already done that in c++ and that works fine and i have switched to c# from c++ because i have to implement the same in c#.
Note: I don't have to use LUTs/Arrays only the linkedlist must be used.
By frequency i mean number of repetitions of symbols and by symbols i mean if we see binary file using xxd -b BinaryFile.bin then we will get lot of 8 bits of combinations of 0 and 1. So how many times each symbol repeats is it's frequency.
Now, How i have tried to do so:
I achieve it in c# by doing mono filename.exe BinaryFile.bin at terminal with code written in filename inside notepad++.
Logic:
I read each symbol in the BinaryFile if it is not repeated then i add it at the tail of linked list and if it is repeated then i increase it's frequency. This i repeat for full binary file.
Code:
Code for c# (full): (which don't work properly, I have pointed the problem containing part in the code, I put full code because may be you will need it):
////Problem containing part starts here ////////
public Huffman(string[] args) //called from MyClass
{
Node tree = null;
int counter = 0;
using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0])))
{
while (stream.BaseStream.Position < stream.BaseStream.Length)
{
int processingValue = stream.ReadByte();
Node ppt, pt, temp;
bool is_there = false;
ppt = pt = tree;
while (pt != null)
{
if (pt.symbol == processingValue)
{
pt.freq++;
is_there = true;
break;
}
ppt = pt;
pt = pt.next;
}
if (is_there == false)
{
temp = new Node();
temp.symbol = processingValue;
temp.freq = 1;
temp.left = null;
temp.right = null;
temp.next = null;
temp.id = (++total_nodes);
temp.is_processed = 0;
if (tree == null)
{
tree = temp;
}
else
{
ppt.next = temp;
}
//The same check/debugging which i was doing in c++ to know what symbol and freq contains but they contains different values.
//And the output of both c#/c++ are different where as it was supposed to be same.
Node chc = tree;
while (chc != null)
{
Console.WriteLine(" sym: " + chc.symbol);
Console.WriteLine(" freq: " + chc.freq);
chc = chc.next;
}
}
}
stream.Close();
}
}
////Problem containing part Ends here ////////
Difference between the output of the c++ and c#:
(1) When i display the output of the c++ it works correctly and when i see the code part which i have written in my code to debug/check the output on the terminal for that shows the correct execution of code. whereas the same debugging code in c# don't show the same output with c++. which it was supposed to be do because both of these code to print "symbol" and "freq" are kept at the same place in program.
(2) The output of c# makes me feel that while loop is executed less number of times in c# then in c++ because in c# output terminal don't show large amount repetition of freq and symbol. Please see the output of both :
c# output at terminal :
hp#ubuntu:~/Desktop/$ mono check1.exe out.bin
sym: 0
freq: 1
sym: 0
freq: 200
sym: 1
freq: 1
sym: 0
freq: 200
sym: 1
freq: 198
sym: 2
freq: 1
sym: 0
freq: 200
sym: 1
freq: 198
sym: 2
freq: 195
sym: 3
freq: 1
sym: 0
freq: 200
sym: 1
freq: 198
sym: 2
freq: 195
sym: 3
freq: 189
sym: 4
freq: 1
hp#ubuntu:~/Desktop/
whereas the output of c++ is : (here the value of counter actually started form "0" (Not "196") but is not able to show the full output because the file is larger so output is big terminal is not able to show all, it just shows the output at the end)
hp#ubuntu:~/Desktop/$ ./filename out.bin
//Counter starts from "0" but terminal is not able to show all.So doing from "196"
counter: 196
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 1
counter: 197
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 2
counter: 198
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 3
counter: 199
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 4
counter: 200
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 5
counter: 201
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 6
counter: 202
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 7
counter: 203
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 8
counter: 204
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 9
counter: 205
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 10
counter: 206
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 11
counter: 207
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 12
counter: 208
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 13
counter: 209
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 14
counter: 210
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 15
counter: 211
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 16
counter: 212
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 17
counter: 213
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 18
counter: 214
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 19
counter: 215
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 20
counter: 216
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 21
counter: 217
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 22
counter: 218
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 23
counter: 219
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 24
counter: 220
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 25
counter: 221
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 26
counter: 222
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 27
counter: 223
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 28
counter: 224
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 29
counter: 225
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 30
counter: 226
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 31
counter: 227
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 32
counter: 228
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 33
counter: 229
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 34
counter: 230
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 35
counter: 231
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 36
counter: 232
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 37
counter: 233
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 38
counter: 234
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 39
counter: 235
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 40
counter: 236
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 41
counter: 237
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 42
counter: 238
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 43
counter: 239
sym: 0
freq: 50
sym: 1
freq: 50
sym: 2
freq: 48
sym: 3
freq: 48
sym: 4
freq: 44
Questions:
(1) Why the output shown by c# code is different from c++ ? Even i have tasted on same BinaryFile (out.bin in my case).
(2) Could you please help me in coming out of this problem ? Big thanks
I might be totally misunderstanding this... Are you just wanting to know the number of occurrences of each byte value (0..255) within a specified file?
If so, a simple way to do it is like this:
var counts = new int[256]; // Assumes files aren't longer than 2GB.
string filename = "<Your filename goes here>";
foreach (byte b in File.ReadAllBytes(filename)) // Will run out of memory
++counts[b]; // for very large files!
for (int i = 0; i < counts.Length; ++i)
Console.WriteLine("Symbol {0} occurred {1} times.", i, counts[i]);
However, this is so much simpler than what you're doing that I feel I must be misunderstanding....
[EDIT]
I can't fix your original code, but here's a sample program that works that uses a linked-list to solve the problem:
using System;
using System.IO;
namespace ConsoleApp1
{
public sealed class Node
{
public byte Symbol { get; set; }
public int Count { get; set; }
public Node Next { get; set; }
}
sealed class Program
{
private void run()
{
var linkedList = new Node();
string filename = #"C:\Test\t.cs";
foreach (byte symbol in File.ReadAllBytes(filename))
addSymbol(symbol, linkedList);
for (int symbol = 0; symbol < 256; ++symbol)
{
int count = countForSymbol((byte)symbol, linkedList);
Console.WriteLine("Symbol {0} occurred {1} times.", symbol, count);
}
}
private static void addSymbol(byte symbol, Node head)
{
Node last = head;
while (head != null)
{
last = head;
if (head.Symbol == symbol)
{
++head.Count;
return;
}
else
{
head = head.Next;
}
}
last.Next = new Node
{
Symbol = symbol,
Count = 1
};
}
private int countForSymbol(byte symbol, Node head)
{
while (head != null)
{
if (head.Symbol == symbol)
return head.Count;
else
head = head.Next;
}
return 0;
}
private static void Main()
{
new Program().run();
}
}
}
So I have something very strange happening in my Asp.Net Website.
I have 2 seperate pages with the same code to download files stored in a SQL database.
One page is an admin version of the page where you can delete and upload files and also see the files ID.
The user end version of this page only displays the download button and the filename. The ID is hidden and used as a DataKey in the grid to select the file from the database.
Both pages work perfectly in development. But when I switched to our production staging server the user end version of the page will turn a simple one line text file into a bunch of gargly-goop PDF encoding when saving and opening it, while the admin version of the page still functions properly. PDF's download and view normal and word documents do the same thing that the text files do.
Here is the code behind for the admin download section:
protected void btnSaveAttachment_Click(object sender, EventArgs e)
{
GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
string AttachmentID = "";
string AttachmentName = "";
if (selectedRow.Cells[1].Text != null && selectedRow.Cells[2].Text != null)
{
AttachmentID = selectedRow.Cells[1].Text;
AttachmentName = selectedRow.Cells[2].Text;
}
byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ClearHeaders();
response.AddHeader("Cache-Control", " no-store, no-cache ");
response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
response.BinaryWrite(objData);
response.Flush();
if (response != null)
{
response.End();
}
}
Here is the code behind for the user download section:
protected void btnSaveAttachment_Click(object sender, EventArgs e)
{
GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
//string AttachmentID = selectedRow.Cells[1].Text;
string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();
string AttachmentName = "";
if (selectedRow.Cells[1].Text != null)
{
AttachmentName = selectedRow.Cells[1].Text;
}
byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ClearHeaders();
response.AddHeader("Cache-Control", " no-store, no-cache ");
response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
response.BinaryWrite(objData);
response.Flush();
//File.Delete(SavePath);
if (response != null)
{
response.End();
}
}
I am at a point where I just dont know what the heck is going on. The only other difference in the pages front end code is the user page load some text from the database into a label and has a telerik textbox control. (I have tried stripping out all other items on the page but still files other than PDF try to encode as a PDF.)
Here is what a 1 line text file looks like when saving and opening it.
%PDF-1.4
%âãÏÓ
9 0 obj<</H[516 160]/Linearized 1/E 5419/L 14363/N 2/O 12/T 14137>>
endobj
xref
9 11
0000000016 00000 n
0000000676 00000 n
0000000516 00000 n
0000000753 00000 n
0000000881 00000 n
0000000976 00000 n
0000001511 00000 n
0000001903 00000 n
0000002142 00000 n
0000002387 00000 n
0000002463 00000 n
trailer
<</Size 20/Prev 14127/Root 10 0 R/Info 8 0 R/ID[<3d8f2faf909b30f75011a461bd4aff97><b62071c85b58ab4d8f0b23af1811506f>]>>
startxref
0
%%EOF
11 0 obj<</Length 82/Filter/FlateDecode/L 90/S 53>>stream
xÚb```f``
‘BVœÀ cf`aàXÀàΰ…Ql
HT ÈAPÌÀàÃÀÃì ,³Ñ†Ë`%·Hˆ…Aý!fb€ O{Ì
endstream
endobj
10 0 obj<</Pages 6 0 R/Type/Catalog/PageLabels 4 0 R/Metadata 7 0 R>>
endobj
12 0 obj<</Contents 19 0 R/Type/Page/Parent 6 0 R/Rotate 0/MediaBox[0 0 612 792]/CropBox[0 0 612 792]/Resources 13 0 R>>
endobj
13 0 obj<</Font<</TT2 14 0 R/TT4 15 0 R>>/ProcSet[/PDF/Text]/ExtGState<</GS1 18 0 R>>>>
endobj
14 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPSMT/FirstChar 32/LastChar 150/Subtype/TrueType/FontDescriptor 16 0 R/Widths[250 0 0 0 0 833 778 0 333 333 0 564 250 333 250 278 500 500 500 500 500 500 500 500 500 500 278 0 0 0 0 0 0 722 667 667 722 611 556 722 0 333 389 0 611 889 722 722 556 722 667 556 611 722 722 944 722 722 0 0 0 0 0 0 0 444 500 444 500 444 333 500 500 278 278 500 278 778 500 500 500 500 333 389 278 500 500 722 500 500 444 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 500]>>
endobj
15 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPS-BoldMT/FirstChar 32/LastChar 121/Subtype/TrueType/FontDescriptor 17 0 R/Widths[250 0 0 0 0 0 0 0 0 0 0 0 0 0 250 0 500 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 722 722 0 0 0 0 389 0 0 0 0 0 778 0 0 0 556 667 722 0 0 0 0 0 0 0 0 0 0 0 500 0 444 0 444 333 500 0 278 0 0 0 833 556 0 0 0 444 389 333 556 500 722 0 500]>>
endobj
16 0 obj<</Type/FontDescriptor/FontBBox[-568 -307 2028 1007]/FontName/TimesNewRomanPSMT/Flags 34/StemV 82/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 400>>
endobj
17 0 obj<</Type/FontDescriptor/FontBBox[-558 -307 2034 1026]/FontName/TimesNewRomanPS-BoldMT/Flags 34/StemV 136/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 700>>
endobj
18 0 obj<</Type/ExtGState/SA false/OP false/SM 0.02/op false/OPM 1>>
endobj
etc....................
endstream
endobj
4 0 obj<</Nums[0 5 0 R]>>
endobj
5 0 obj<</S/D>>
endobj
6 0 obj<</Count 2/Kids[12 0 R 1 0 R]/Type/Pages>>
endobj
7 0 obj<</Length 3339/Type/Metadata/Subtype/XML>>stream
<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
<?adobe-xap-filters esc="CRLF"?>
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9.1-13, framework 1.6'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:iX='http://ns.adobe.com/iX/1.0/'>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' pdf:Producer='Acrobat Distiller 6.0 (Windows)'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xap='http://ns.adobe.com/xap/1.0/' xap:CreatorTool='PScript5.dll Version 5.2' xap:ModifyDate='2005-06-10T14:07:36-04:00' xap:CreateDate='2005-06-10T14:07:36-04:00'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/' xapMM:DocumentID='uuid:2aee5402-c607-44cd-a815-8ad3a0bf0a56'/>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:dc='http://purl.org/dc/elements/1.1/' dc:format='application/pdf'><dc:title><rdf:Alt><rdf:li xml:lang='x-default'>Microsoft Word - 1 - DTOD Overview.doc</rdf:li></rdf:Alt></dc:title><dc:creator><rdf:Seq><rdf:li>AttardA</rdf:li></rdf:Seq></dc:creator></rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>
endstream
endobj
8 0 obj<</ModDate(D:20050610140736-04'00')/CreationDate(D:20050610140736-04'00')/Title(Microsoft Word - 1 - DTOD Overview.doc)/Creator(PScript5.dll Version 5.2)/Producer(Acrobat Distiller 6.0 \(Windows\))/Author(AttardA)>>
endobj
xref
0 9
0000000000 65535 f
0000005419 00000 n
0000005544 00000 n
0000005638 00000 n
0000010369 00000 n
0000010402 00000 n
0000010425 00000 n
0000010482 00000 n
0000013897 00000 n
trailer
<</Size 9>>
startxref
116
%%EOF
RESOLVED
The issue was the DataKeys[0] was always selecting the 0 item in the array of keys. I changed that to:
string AttachmentID = grdAttachments.DataKeys[selectedRow.RowIndex].Value.ToString();
And now all is working fine! Wish I would have debugged closer into the attachmentID!
It just so happened that there was 2 documents that were the same and I though I was downloading the correct document.
In your admin section:
string AttachmentID = selectedRow.Cells[1].Text;
In your user section:
string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();
I would have to assume that the user section assignment is not doing what you expect it to, since the logic isn't the same for both. Verify what AttachmentID is set to in each case, perhaps via logging.