I have an io.Reader
in Golang and I want to double-check that the size of its data is below a predetermined maximum before or while running io.Copy()
to save it to disk using io.Writer
.
Since the file data in io.Reader
could theoretically be quite large, I want to minimize memory usage and processing here if avoidable.
I don't think there's a function that's like io.CopyLessThanOrEqualToThisManyBytesOrReturnError()
, but I did notice that io.ReadFull()
can do the opposite to return an error if not enough bytes are there to fill the provided buffer.
Does anyone have a solution to this?
EDIT:
To clarify, copying a fraction of the data is not OK. It either needs to fail if it's over the threshold, or work if it's under.