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