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      * Reads a byte.
 46      * 
 47      * @returns {Number} The unsigned byte or -1.
 48      */
 49     readByte : function() {
 50         return this._fd.readByte();
 51     },
 52     /**
 53      * Writes a byte.
 54      * 
 55      * @param {Number} b The byte to write.
 56      * @returns {Boolean} true if the byte has been write.
 57      */
 58     writeByte : function(b) {
 59         return this._fd.writeByte(b);
 60     },
 61     /**
 62      * Reads bytes into the specified byte array, starting at the given offset.
 63      * 
 64      * @param {ByteArray} barray The destination byte array.
 65      * @param {Number} offset The offset at which to start storing bytes.
 66      * @param {Number} length The maximum number of bytes to read.
 67      * @returns {Number} The total number of bytes read.
 68      */
 69     readByteArray : function(barray, offset, length) {
 70     	offset = offset || 0;
 71     	length = length || barray.size() - offset;
 72         return this._fd.read(barray, offset, length);
 73     },
 74     /**
 75      * Writes bytes from the specified byte array starting at the given offset.
 76      * 
 77      * @param {ByteArray} barray The source byte array.
 78      * @param {Number} offset The offset at which to start getting bytes.
 79      * @param {Number} length The maximum number of bytes to write.
 80      * @returns {Number} The number of bytes written.
 81      */
 82     writeByteArray : function(barray, offset, length) {
 83     	offset = offset || 0;
 84     	length = length || barray.size() - offset;
 85         return this._fd.write(barray, offset, length);
 86     },
 87     /**
 88      * Reads this file into a buffer.
 89      *
 90      * @param {jls.lang.Buffer} buffer The buffer to read.
 91      * @returns {Number} the read byte count.
 92      */
 93     read : function(buffer) {
 94         var count = this._fd.read(buffer.byteArray(), buffer.offset(), buffer.remaining());
 95         if (count > 0) {
 96             buffer.incrementPosition(count);
 97         }
 98         return count;
 99     },
100     /**
101      * Writes a buffer into this file.
102      *
103      * @param {jls.lang.Buffer} buffer The buffer to write.
104      * @returns {Number} the write byte count.
105      */
106     write : function(buffer) {
107         var count = this._fd.write(buffer.byteArray(), buffer.offset(), buffer.remaining());
108         if (count > 0) {
109             buffer.incrementPosition(count);
110         }
111         return count;
112     },
113     // TODO Remove
114     writeString : function(s) {
115         return this._fd.writeString(s);
116     },
117     getFD : function() {
118         return this._fd;
119     }
120 });
121