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 // TODO Check output stream 18 this._out = out; 19 var charset = csn ? jls.io.cs.Charset.forName(csn) : jls.io.cs.Charset.defaultCharset(); 20 this._encoder = charset.newEncoder(); 21 }, 22 close : function() { 23 return this._out.close(); 24 }, 25 flush : function() { 26 return this._out.flush(); 27 }, 28 writeChar : function(c) { 29 // TODO Change this 30 return this.write(String.fromCharCode(c)); 31 }, 32 writeCharBuffer : function(cb) { 33 var bb = this._encoder.encode(cb); 34 bb.flip(); 35 return this._out.write(bb); 36 }, 37 /** 38 * Writes a string. 39 * 40 * @param {String} s The string to write. 41 * @returns {Number} the write byte count. 42 */ 43 write : function(s) { 44 if (typeof s != 'string') { 45 throw new jls.lang.Exception('Invalid argument'); 46 } 47 var cb = jls.lang.CharBuffer.wrap(s); 48 return this.writeCharBuffer(cb); 49 }, 50 /** 51 * Writes a string. 52 * 53 * @param {String} s The string to write. 54 * @returns {Number} the write byte count. 55 */ 56 writeLine : function(s) { 57 if (s) { 58 return this.write(s + jls.io.OutputStreamWriter.separator); 59 } else { 60 return this.write(jls.io.OutputStreamWriter.separator); 61 } 62 } 63 }); 64 65 Object.extend(jls.io.OutputStreamWriter, 66 { 67 separator : _native.core.properties['line.separator'] 68 }); 69 70