read block from stream once into a buffer instead of getc from stream, there is still one little bug, see issue.