Skip to content

Commit

Permalink
Merge pull request #260 from botic/textstream-writable
Browse files Browse the repository at this point in the history
Better error message if writing read-only streams, fixes #245
  • Loading branch information
botic committed Jun 24, 2014
2 parents a097d69 + 627ef01 commit f124f26
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ exports.TextStream = function TextStream(io, options, buflen) {
* Writes all arguments to the stream.
*/
this.write = function () {
if (!io.writable()) {
throw new Error("The TextStream is not writable!");
}

for (var i = 0; i < arguments.length; i++) {
encoder.encode(String(arguments[i]));
}
Expand Down
18 changes: 18 additions & 0 deletions test/io_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ exports.testTextStream = function() {

assert.strictEqual(lines[0], "Hello\n");
assert.strictEqual(lines[1], "World!");

// Try to write a read-only stream
try {
stream.writeLine("Hello World!");
assert.fail("writeLine() should throw an error!");
} catch (err) {
assert.strictEqual(err.name, "Error");
assert.strictEqual(err.message, "The TextStream is not writable!");
}
stream.close();

// Check writing
var output = new java.io.ByteArrayOutputStream();
stream = new TextStream(new Stream(output));
stream.writeLine("Hello");
stream.write("World!");
stream.close();
assert.strictEqual(output.toString(), "Hello\nWorld!");
};

if (module == require.main) {
Expand Down

0 comments on commit f124f26

Please sign in to comment.