1 jls.loader.provide('jls.net.FileSelectionHandler');
  2 
  3 jls.loader.require('jls.net.SelectionHandler');
  4 jls.loader.require('jls.io.File');
  5 jls.loader.require('jls.io.FileChannel');
  6 jls.loader.require('jls.lang.ByteBuffer');
  7 
  8 jls.net.FileSelectionHandler = jls.lang.Class.create(jls.net.SelectionHandler, /** @lends jls.net.FileSelectionHandler.prototype */
  9 {
 10     /**
 11      * Creates a file selection handler.
 12      *
 13      * @param {jls.io.File} file The file to use.
 14      * @constructs
 15 	 * @augments jls.net.SelectionHandler
 16 	 * @class A selection handler for reading and writing a file.
 17      */
 18     initialize : function(file, length) {
 19         if (! (file instanceof jls.io.File)) {
 20             throw new jls.lang.Exception('Invalid file argument type (' + (typeof file) + ')');
 21         }
 22         this._file = file;
 23         this._fc = null;
 24         this._buffer = jls.lang.ByteBuffer.allocate(1024);
 25         this._length = length || this._file.length();
 26     },
 27     length : function() {
 28         return this._length;
 29     },
 30     reset : function() {
 31         this._remaining = this._length;
 32         this._buffer.clear();
 33         this._buffer.setLimit(0);
 34         if (this._fc != null) {
 35             this._fc.close();
 36             this._fc = null;
 37         }
 38     },
 39     onRead : function(channel) {
 40         var noDataStatus = jls.net.SelectionHandler.STATUS_FAILURE;
 41         for (;;) {
 42             this._buffer.clear();
 43             var count = channel.read(this._buffer);
 44             if (count <= 0) {
 45                 return noDataStatus;
 46             }
 47             this._buffer.flip();
 48             this._remaining -= this._buffer.remaining();
 49             noDataStatus = jls.net.SelectionHandler.STATUS_IN_PROGRESS;
 50             if (this._fc == null) {
 51                 this._fc = new jls.io.FileChannel(this._file, jls.io.FileChannel.WRITE_ONLY | jls.io.FileChannel.CREATE_FILE);
 52             }
 53             this._fc.write(this._buffer);
 54             if (this._remaining == 0) {
 55                 this.reset();
 56                 return jls.net.SelectionHandler.STATUS_DONE;
 57             }
 58         }
 59     },
 60     onWrite : function(channel) {
 61         var noDataStatus = jls.net.SelectionHandler.STATUS_FAILURE;
 62         for (;;) {
 63             if (this._buffer.remaining() > 0) {
 64                 if (channel.write(this._buffer) <= 0) {
 65                     return noDataStatus;
 66                 }
 67                 if (this._buffer.remaining() > 0) {
 68                     return jls.net.SelectionHandler.STATUS_IN_PROGRESS;
 69                 }
 70                 noDataStatus = jls.net.SelectionHandler.STATUS_IN_PROGRESS;
 71             }
 72             if (this._remaining == 0) {
 73                 this.reset();
 74                 return jls.net.SelectionHandler.STATUS_DONE;
 75             }
 76             if (this._fc == null) {
 77                 this._fc = new jls.io.FileChannel(this._file, jls.io.FileChannel.READ_ONLY);
 78             }
 79             this._buffer.clear();
 80             var count = this._fc.read(this._buffer);
 81             if (count > 0) {
 82                 this._remaining -= count;
 83             }
 84             if (this._remaining == 0) {
 85                 this._fc.close();
 86                 this._fc = null;
 87             }
 88             this._buffer.flip();
 89         }
 90     }
 91 });
 92