Bug 1106 – Using Stream.readLine() and Stream.read(ubyte[]) requires understanding implementation details
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
All
OS
All
Creation time
2007-04-06T12:56:00Z
Last change time
2015-11-03T16:07:13Z
Assigned to
nobody
Creator
digitalmars-com
Comments
Comment #0 by digitalmars-com — 2007-04-06T12:56:34Z
There is a bug in the Stream class. If you mix, readLine() and read(ubyte[]) calls to a stream, readLine() may not appear to perform as documented. As documented, readLine() stop at "some combination of carriage return [CR] and line feed [LF]". However, this is not necessarily true. If the stream is non-seekable, readLine() will stop at CR and set a prevCr flag indicating that a LF should be consumed on the next read. However, this only takes place in the getc() method and methods using getc(). Some methods, like read(ubyte[]), read the stream directly and the prevCr flag is not processed.
At first, I thought this might just be odd behavior rather than a bug, but I decided that it is a bug. In order to understand how to use the stream effectively, one must understand the implementation details. One must know that the behavior for seekable streams is different that non-seekable streams, and adjust the client code accordingly. For this reason, it should be considered a bug.
Code example:
import std.stream;
void main() {
char[] testString = "line one\r\nline two\r\n";
File testFile = new File("testFile", FileMode.OutNew);
testFile.writeString(testString);
testFile.close();
File input = new File("testFile", FileMode.In);
input.seekable = false;
char[] line1 = input.readLine();
assert(line1 == "line one"); // correct
ubyte[1] b;
input.read(b);
assert(b[0] == '\n'); // incorrect
}
Comment #1 by rburners — 2015-11-03T16:07:13Z
std.stream is deprecated and will be removed in 2016