1 jls.loader.provide('jls.io.BufferedReader'); 2 3 jls.loader.require('jls.lang.CharBuffer'); 4 5 jls.io.BufferedReader = jls.lang.Class.create( /** @lends jls.io.BufferedReader.prototype */ 6 { 7 /** 8 * Creates a BufferedReader. 9 * 10 * @param {jls.io.Reader} reader The underlying character reader. 11 * @param {Number} size The buffer size. 12 * @constructs 13 * @class Provides a buffered reader for character reader. 14 */ 15 initialize : function(reader, size) { 16 this._reader = reader; 17 this._buffer = jls.lang.CharBuffer.allocate(size || 1024); 18 this._buffer.setLimit(0); 19 this._mark = -1; 20 }, 21 /** 22 * Closes this stream. 23 * 24 */ 25 close : function() { 26 return this._reader.close(); 27 }, 28 /** 29 * Flushs this stream. 30 * 31 */ 32 flush : function() { 33 return this._reader.flush(); 34 }, 35 mark : function(limit) { 36 if (this._buffer.remaining() == 0) { 37 this._buffer.clear(); 38 this._buffer.setLimit(0); 39 } 40 if (limit > this._buffer.capacity() - this._buffer.position()) { 41 throw new jls.lang.BufferOverflowException(); 42 } 43 this._mark = this._buffer.position(); 44 }, 45 /** 46 * Tells if this stream supports the mark and reset methods. 47 * 48 * @returns {Boolean} if this stream instance supports the mark and reset methods; false otherwise. 49 */ 50 markSupported : function() { 51 return true; 52 }, 53 reset : function() { 54 if (this._mark < 0) { 55 throw new jls.lang.Exception('Invalid mark'); 56 } 57 this._buffer.setPosition(this._mark); 58 }, 59 _fill : function() { 60 if (this._buffer.remaining() > 0) { 61 return; 62 } 63 this._buffer.clear(); 64 var count = this._reader.readCharBuffer(this._buffer); 65 this._buffer.flip(); 66 this._mark = -1; 67 return count; 68 }, 69 /** 70 * Reads a byte. 71 * 72 * @returns {Number} The unsigned byte or -1. 73 */ 74 readChar : function() { 75 this._fill(); 76 return this._buffer.getChar(); 77 }, 78 /** 79 * Reads this file into a buffer. 80 * 81 * @param {jls.lang.Buffer} buffer The buffer to read. 82 * @returns {Number} the read byte count. 83 */ 84 readCharBuffer : function(buffer) { 85 var start = buffer.position(); 86 while (buffer.remaining() > 0) { 87 this._fill(); 88 if (this._buffer.remaining() == 0) { 89 break; 90 } 91 buffer.putBuffer(this._buffer); 92 } 93 return buffer.position() - start; 94 }, 95 /** 96 * Reads a line and returns the string. 97 * 98 * @returns {String} The line. 99 */ 100 readLine : function() { 101 var line = ''; 102 while (true) { 103 if (this._fill() == 0) { 104 return null; 105 } 106 if (this._buffer.remaining() == 0) { 107 break; 108 } 109 var p = this._buffer.position(); 110 var s = this._buffer.getString(); 111 var i = s.indexOf(jls.io.BufferedReader.separator); 112 //jls.lang.System.out.println('->' + s + '<- ' + s.length + ', ' + i); 113 if (i >= 0) { 114 this._buffer.setPosition(p + i + jls.io.BufferedReader.separator.length); 115 line += s.substr(0, i); 116 break; 117 } 118 line += s; 119 } 120 return line; 121 } 122 }); 123 124 Object.extend(jls.io.BufferedReader, 125 { 126 separator : _native.core.properties['line.separator'] 127 }); 128 129