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 this._reader.readCharBuffer(this._buffer); 65 this._buffer.flip(); 66 this._mark = -1; 67 }, 68 /** 69 * Reads a byte. 70 * 71 * @returns {Number} The unsigned byte or -1. 72 */ 73 readChar : function() { 74 this._fill(); 75 return this._buffer.getChar(); 76 }, 77 /** 78 * Reads this file into a buffer. 79 * 80 * @param {jls.lang.Buffer} buffer The buffer to read. 81 * @returns {Number} the read byte count. 82 */ 83 readCharBuffer : function(buffer) { 84 var start = buffer.position(); 85 while (buffer.remaining() > 0) { 86 this._fill(); 87 if (this._buffer.remaining() == 0) { 88 break; 89 } 90 buffer.putBuffer(this._buffer); 91 } 92 return buffer.position() - start; 93 }, 94 /** 95 * Reads a line and returns the string. 96 * 97 * @returns {String} The line. 98 */ 99 readLine : function() { 100 var line = ''; 101 while (true) { 102 this._fill(); 103 if (this._buffer.remaining() == 0) { 104 break; 105 } 106 var p = this._buffer.position(); 107 var s = this._buffer.getString(); 108 var i = s.indexOf(jls.io.BufferedReader.separator); 109 //jls.lang.System.out.println('->' + s + '<- ' + s.length + ', ' + i); 110 if (i >= 0) { 111 this._buffer.setPosition(p + i + jls.io.BufferedReader.separator.length); 112 line += s.substr(0, i); 113 break; 114 } 115 line += s; 116 } 117 return line; 118 } 119 }); 120 121 Object.extend(jls.io.BufferedReader, 122 { 123 separator : _native.core.properties['line.separator'] 124 }); 125 126