1 jls.loader.provide('jls.io.OutputStreamWriter'); 2 3 jls.loader.require('jls.io.cs.Charset'); 4 jls.loader.require('jls.lang.CharBuffer'); 5 6 jls.io.OutputStreamWriter = jls.lang.Class.create( /** @lends jls.io.OutputStreamWriter.prototype */ 7 { 8 /** 9 * Creates a writer. 10 * 11 * @param {jls.io.OutputStream} out The underlying byte output stream. 12 * @param {String} csn The name of the character set to use. 13 * @constructs 14 * @class A character writer for byte output stream. 15 */ 16 initialize : function(out, csn) { 17 this._out = out; 18 var charset = csn ? jls.io.cs.Charset.forName(csn) : jls.io.cs.Charset.defaultCharset(); 19 this._encoder = charset.newEncoder(); 20 }, 21 close : function() { 22 return this._out.close(); 23 }, 24 flush : function() { 25 return this._out.flush(); 26 }, 27 writeChar : function(c) { 28 // TODO Change this 29 return this.write(String.fromCharCode(c)); 30 }, 31 writeCharBuffer : function(cb) { 32 var bb = this._encoder.encode(cb); 33 bb.flip(); 34 return this._out.write(bb); 35 }, 36 /** 37 * Writes a string. 38 * 39 * @param {String} s The string to write. 40 * @returns {Number} the write byte count. 41 */ 42 write : function(s) { 43 if (typeof s != 'string') { 44 throw new jls.lang.Exception('Invalid argument'); 45 } 46 var cb = jls.lang.CharBuffer.wrap(s); 47 return this.writeCharBuffer(cb); 48 }, 49 /** 50 * Writes a string. 51 * 52 * @param {String} s The string to write. 53 * @returns {Number} the write byte count. 54 */ 55 writeLine : function(s) { 56 return this.write(s + jls.io.OutputStreamWriter.separator); 57 } 58 }); 59 60 Object.extend(jls.io.OutputStreamWriter, 61 { 62 separator : _native.core.properties['line.separator'] 63 }); 64 65