1 jls.loader.provide('jls.io.InputStreamReader'); 2 3 //jls.loader.require('jls.io.BufferedInputStream'); 4 jls.loader.require('jls.io.cs.Charset'); 5 jls.loader.require('jls.lang.ByteBuffer'); 6 jls.loader.require('jls.lang.CharBuffer'); 7 8 jls.io.InputStreamReader = jls.lang.Class.create( /** @lends jls.io.InputStreamReader.prototype */ 9 { 10 /** 11 * Creates a writer. 12 * 13 * @param {jls.io.InputStream} input The underlying byte input stream. 14 * @param {String} csn The name of the character set to use. 15 * @constructs 16 * @class A character reader for byte input stream. 17 */ 18 initialize : function(input, csn) { 19 /*if (! input.markSupported()) { 20 input = new jls.io.BufferedInputStream(input); 21 }*/ 22 this._in = input; 23 var charset = csn ? jls.io.cs.Charset.forName(csn) : jls.io.cs.Charset.defaultCharset(); 24 this._decoder = charset.newDecoder(); 25 this._bbuffer = jls.lang.ByteBuffer.allocate(1024); 26 this._bbuffer.setLimit(0); 27 this._cbuffer = jls.lang.CharBuffer.allocate(1); // used for the readChar method 28 }, 29 /** 30 * Closes this stream. 31 * 32 */ 33 close : function() { 34 return this._in.close(); 35 }, 36 /** 37 * Flushs this stream. 38 * 39 */ 40 flush : function() { 41 return this._in.flush(); 42 }, 43 /** 44 * Tells if this stream supports the mark and reset methods. 45 * 46 * @returns {Boolean} if this stream instance supports the mark and reset methods; false otherwise. 47 */ 48 markSupported : function() { 49 return ('mark' in this) && ('reset' in this); 50 }, 51 readChar : function() { 52 var cb = this._cbuffer; 53 cb.clear(); 54 cb.setLimit(1); 55 this.readCharBuffer(cb); 56 if (cb.remaining() != 0) { 57 return -1; 58 } 59 cb.flip(); 60 return cb.getChar(); 61 }, 62 /** 63 * Reads the underlying stream into a character buffer. 64 * 65 * @param {jls.lang.CharBuffer} cb The character buffer to read into. 66 * @returns {Number} the read byte count. 67 */ 68 readCharBuffer : function(cb) { 69 var start = cb.position(); 70 // TODO adjust buffer 71 var bb = this._bbuffer; 72 while (cb.remaining() > 0) { 73 if (bb.remaining() == 0) { 74 bb.clear(); 75 this._in.read(bb); 76 bb.flip(); 77 //jls.logger.warn(bb.remaining() + ' byte(s) read'); 78 } 79 //jls.logger.warn('bb.remaining(): ' + bb.remaining()); 80 //jls.logger.warn('cb.remaining(): ' + cb.remaining()); 81 if (bb.remaining() > 0) { 82 this._decoder.decode(bb, cb); 83 } 84 if (bb.limit() != bb.capacity()) { 85 break; 86 } 87 } 88 return cb.position() - start; 89 } 90 }); 91