From: Peter J. <pe...@jo...> - 2004-04-09 15:16:17
|
> Actually IO does not work maybe well with buffered streams : the > specification is that when you're reading n "elements" (characters or any) > from an input, the result will be exactly the n elements you needed. Not > more, and not less. This might actually be questionable : if you have spare > time, could you suggest what parts of IO module should be modified in order > to support buffered streams? I attach a patch that (IMO) improves things, but it does involve changing the interface somewhat - maybe that's still okay at this stage? It changes the semantics of nread to return anything up to the requested number items. Unlike Pervasives.input, the only situation in which fewer are returned is if the end of the stream is reached first. The return value is an (int, 'b) pair where the int is the number actually returned. A new read_exactly function is added that behaves like nread did before, i.e. it calls nread and raises No_more_input if the returned item count does not match that requested. A side-effect of the implementation is that a failed call to read_exactly is guaranteed to consume the remainder of the stream. This may or may not be desirable, but it was already the case for streams wrapping enums, so this merely defines a previously undefined behaviour. There's one problem, which is input_bits/output_bits, where the old behaviour makes more sense. For now all I've done is changed the return value of nread for input_bits so it compiles. Of course, I won't be offended if you don't think it's worth making these changes. |