Would something like this help? (No of course I haven't compiled it.)
int len;
while ((len=file.Read(buf,0,buf.Length)) > 0) {
for (int i=0; i<len; i++_) {
if (buf[i] == '1') {
counter++;
}
}
}
(This bug won't affect the performance comparison, but I was just reminded of how error prone these kinds APIs can be vs the PHP/Python route of having the library function just allocate a new buffer each time.)