1 jls.loader.provide('jls.io.BufferChannel'); 2 3 jls.loader.require('jls.lang.ByteBuffer'); 4 5 jls.io.BufferChannel = jls.lang.Class.create({ 6 initialize : function(buffer) { 7 if (buffer) { 8 if (! (buffer instanceof jls.lang.ByteBuffer)) { 9 throw new jls.lang.Exception('Invalid Buffer argument type (' + (typeof buffer) + ')'); 10 } 11 this._buffer = buffer.slice(); 12 this._append = false; 13 } else { 14 this._buffer = jls.lang.ByteBuffer.allocate(1024); 15 this._append = true; 16 } 17 }, 18 buffer : function() { 19 return this._buffer; 20 }, 21 close : function() { 22 this._buffer.free(); 23 return this; 24 }, 25 flush : function() { 26 return this; 27 }, 28 readByte : function() { 29 if (this._buffer.remaining() < 1) { 30 return -1; 31 } 32 return this._buffer.getByte(); 33 }, 34 writeByte : function(b) { 35 if (this._buffer.remaining() < 1) { 36 return false; 37 } 38 this._buffer.putByte(b); 39 return true; 40 }, 41 read : function(buffer) { 42 var count = buffer.remaining(); 43 if (count > this._buffer.remaining()) { 44 count = this._buffer.remaining(); 45 } 46 if (count > 0) { 47 buffer.putBuffer(this._buffer, count); 48 } 49 return count; 50 }, 51 write : function(buffer) { 52 var count = this._buffer.remaining(); 53 if (count > buffer.remaining()) { 54 count = buffer.remaining(); 55 } 56 //jls.logger.info('count: ' + count); 57 if (count > 0) { 58 this._buffer.putBuffer(buffer, count); 59 } 60 return count; 61 }, 62 toString : function() { 63 var l = this._buffer.limit(); 64 var p = this._buffer.position(); 65 this._buffer.flip(); 66 var s = this._buffer.getString(); 67 this._buffer.setLimit(l); 68 this._buffer.setPosition(p); 69 return s; 70 } 71 }); 72