1 jls.loader.provide('jls.io.FileDescriptor'); 2 3 jls.loader.requireLibrary('jls_io'); 4 5 /** 6 * @class This class represents a file system descriptor. 7 * A file descriptor represents I/O objects, such as open files and sockets. 8 */ 9 jls.io.FileDescriptor = jls.lang.Class.create(/** @lends jls.io.FileDescriptor.prototype */ 10 { 11 initialize : function(nfd) { 12 if (! ((nfd instanceof _native.io.FileDesc) || (nfd instanceof _native.io.StaticFileDesc))) { 13 throw new jls.lang.Exception('Invalid file descriptor type'); 14 } 15 this._fd = nfd; 16 }, 17 setInheritable : function(inheritable) { 18 this._fd.setInheritable(inheritable); 19 return this; 20 }, 21 /** 22 * Closes this file. 23 * 24 */ 25 close : function() { 26 this._fd.close(); 27 return this; 28 }, 29 /** 30 * Flushs this file. 31 * 32 */ 33 flush : function() { 34 return this; 35 }, 36 /** 37 * Tells if this stream supports the mark and reset methods. 38 * 39 * @returns {Boolean} if this stream instance supports the mark and reset methods; false otherwise. 40 */ 41 markSupported : function() { 42 return ('mark' in this) && ('reset' in this); 43 }, 44 /* 45 * Seek. 46 * 47 * @param {Number} offset ?. 48 * @param {Number} [whence] ?. 49 * @returns {Number} The position. 50 */ 51 seek : function(offset, whence) { 52 if (typeof whence == 'undefined') { 53 whence = jls.io.FileDescriptor.SEEK_SET; 54 } 55 return this._fd.seek(offset, whence); 56 }, 57 skipBytes : function(n) { 58 return 0; // TODO ? 59 }, 60 /** 61 * Reads a byte. 62 * 63 * @returns {Number} The unsigned byte or -1. 64 */ 65 readByte : function() { 66 return this._fd.readByte(); 67 }, 68 /** 69 * Writes a byte. 70 * 71 * @param {Number} b The byte to write. 72 * @returns {Boolean} true if the byte has been write. 73 */ 74 writeByte : function(b) { 75 return this._fd.writeByte(b); 76 }, 77 /** 78 * Reads bytes into the specified byte array, starting at the given offset. 79 * 80 * @param {ByteArray} barray The destination byte array. 81 * @param {Number} offset The offset at which to start storing bytes. 82 * @param {Number} length The maximum number of bytes to read. 83 * @returns {Number} The total number of bytes read. 84 */ 85 readByteArray : function(barray, offset, length) { 86 offset = offset || 0; 87 length = length || barray.size() - offset; 88 return this._fd.read(barray, offset, length); 89 }, 90 /** 91 * Writes bytes from the specified byte array starting at the given offset. 92 * 93 * @param {ByteArray} barray The source byte array. 94 * @param {Number} offset The offset at which to start getting bytes. 95 * @param {Number} length The maximum number of bytes to write. 96 * @returns {Number} The number of bytes written. 97 */ 98 writeByteArray : function(barray, offset, length) { 99 offset = offset || 0; 100 length = length || barray.size() - offset; 101 return this._fd.write(barray, offset, length); 102 }, 103 /** 104 * Reads this file into a buffer. 105 * 106 * @param {jls.lang.Buffer} buffer The buffer to read. 107 * @returns {Number} the read byte count. 108 */ 109 read : function(buffer) { 110 var count = this._fd.read(buffer.byteArray(), buffer.offset(), buffer.remaining()); 111 if (count > 0) { 112 buffer.incrementPosition(count); 113 } 114 return count; 115 }, 116 /** 117 * Writes a buffer into this file. 118 * 119 * @param {jls.lang.Buffer} buffer The buffer to write. 120 * @returns {Number} the write byte count. 121 */ 122 write : function(buffer) { 123 var count = this._fd.write(buffer.byteArray(), buffer.offset(), buffer.remaining()); 124 if (count > 0) { 125 buffer.incrementPosition(count); 126 } 127 return count; 128 }, 129 // TODO Remove 130 writeString : function(s) { 131 return this._fd.writeString(s); 132 }, 133 getFD : function() { 134 return this._fd; 135 } 136 }); 137 138 Object.extend(jls.io.FileDescriptor, /** @lends jls.io.FileDescriptor */ 139 { 140 SEEK_SET : _native.io.FileDesc.SEEK_SET, 141 SEEK_CUR : _native.io.FileDesc.SEEK_CUR, 142 SEEK_END : _native.io.FileDesc.SEEK_END 143 }); 144