Path: blob/trunk/third_party/closure/goog/fs/filereader.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A wrapper for the HTML5 FileReader object.16*17*/1819goog.provide('goog.fs.FileReader');20goog.provide('goog.fs.FileReader.EventType');21goog.provide('goog.fs.FileReader.ReadyState');2223goog.require('goog.async.Deferred');24goog.require('goog.events.EventTarget');25goog.require('goog.fs.Error');26goog.require('goog.fs.ProgressEvent');27282930/**31* An object for monitoring the reading of files. This emits ProgressEvents of32* the types listed in {@link goog.fs.FileReader.EventType}.33*34* @constructor35* @extends {goog.events.EventTarget}36* @final37*/38goog.fs.FileReader = function() {39goog.fs.FileReader.base(this, 'constructor');4041/**42* The underlying FileReader object.43*44* @type {!FileReader}45* @private46*/47this.reader_ = new FileReader();4849this.reader_.onloadstart = goog.bind(this.dispatchProgressEvent_, this);50this.reader_.onprogress = goog.bind(this.dispatchProgressEvent_, this);51this.reader_.onload = goog.bind(this.dispatchProgressEvent_, this);52this.reader_.onabort = goog.bind(this.dispatchProgressEvent_, this);53this.reader_.onerror = goog.bind(this.dispatchProgressEvent_, this);54this.reader_.onloadend = goog.bind(this.dispatchProgressEvent_, this);55};56goog.inherits(goog.fs.FileReader, goog.events.EventTarget);575859/**60* Possible states for a FileReader.61*62* @enum {number}63*/64goog.fs.FileReader.ReadyState = {65/**66* The object has been constructed, but there is no pending read.67*/68INIT: 0,69/**70* Data is being read.71*/72LOADING: 1,73/**74* The data has been read from the file, the read was aborted, or an error75* occurred.76*/77DONE: 278};798081/**82* Events emitted by a FileReader.83*84* @enum {string}85*/86goog.fs.FileReader.EventType = {87/**88* Emitted when the reading begins. readyState will be LOADING.89*/90LOAD_START: 'loadstart',91/**92* Emitted when progress has been made in reading the file. readyState will be93* LOADING.94*/95PROGRESS: 'progress',96/**97* Emitted when the data has been successfully read. readyState will be98* LOADING.99*/100LOAD: 'load',101/**102* Emitted when the reading has been aborted. readyState will be LOADING.103*/104ABORT: 'abort',105/**106* Emitted when an error is encountered or the reading has been aborted.107* readyState will be LOADING.108*/109ERROR: 'error',110/**111* Emitted when the reading is finished, whether successfully or not.112* readyState will be DONE.113*/114LOAD_END: 'loadend'115};116117118/**119* Abort the reading of the file.120*/121goog.fs.FileReader.prototype.abort = function() {122try {123this.reader_.abort();124} catch (e) {125throw new goog.fs.Error(e, 'aborting read');126}127};128129130/**131* @return {goog.fs.FileReader.ReadyState} The current state of the FileReader.132*/133goog.fs.FileReader.prototype.getReadyState = function() {134return /** @type {goog.fs.FileReader.ReadyState} */ (this.reader_.readyState);135};136137138/**139* @return {*} The result of the file read.140*/141goog.fs.FileReader.prototype.getResult = function() {142return this.reader_.result;143};144145146/**147* @return {goog.fs.Error} The error encountered while reading, if any.148*/149goog.fs.FileReader.prototype.getError = function() {150return this.reader_.error &&151new goog.fs.Error(this.reader_.error, 'reading file');152};153154155/**156* Wrap a progress event emitted by the underlying file reader and re-emit it.157*158* @param {!ProgressEvent} event The underlying event.159* @private160*/161goog.fs.FileReader.prototype.dispatchProgressEvent_ = function(event) {162this.dispatchEvent(new goog.fs.ProgressEvent(event, this));163};164165166/** @override */167goog.fs.FileReader.prototype.disposeInternal = function() {168goog.fs.FileReader.base(this, 'disposeInternal');169delete this.reader_;170};171172173/**174* Starts reading a blob as a binary string.175* @param {!Blob} blob The blob to read.176*/177goog.fs.FileReader.prototype.readAsBinaryString = function(blob) {178this.reader_.readAsBinaryString(blob);179};180181182/**183* Reads a blob as a binary string.184* @param {!Blob} blob The blob to read.185* @return {!goog.async.Deferred} The deferred Blob contents as a binary string.186* If an error occurs, the errback is called with a {@link goog.fs.Error}.187*/188goog.fs.FileReader.readAsBinaryString = function(blob) {189var reader = new goog.fs.FileReader();190var d = goog.fs.FileReader.createDeferred_(reader);191reader.readAsBinaryString(blob);192return d;193};194195196/**197* Starts reading a blob as an array buffer.198* @param {!Blob} blob The blob to read.199*/200goog.fs.FileReader.prototype.readAsArrayBuffer = function(blob) {201this.reader_.readAsArrayBuffer(blob);202};203204205/**206* Reads a blob as an array buffer.207* @param {!Blob} blob The blob to read.208* @return {!goog.async.Deferred} The deferred Blob contents as an array buffer.209* If an error occurs, the errback is called with a {@link goog.fs.Error}.210*/211goog.fs.FileReader.readAsArrayBuffer = function(blob) {212var reader = new goog.fs.FileReader();213var d = goog.fs.FileReader.createDeferred_(reader);214reader.readAsArrayBuffer(blob);215return d;216};217218219/**220* Starts reading a blob as text.221* @param {!Blob} blob The blob to read.222* @param {string=} opt_encoding The name of the encoding to use.223*/224goog.fs.FileReader.prototype.readAsText = function(blob, opt_encoding) {225this.reader_.readAsText(blob, opt_encoding);226};227228229/**230* Reads a blob as text.231* @param {!Blob} blob The blob to read.232* @param {string=} opt_encoding The name of the encoding to use.233* @return {!goog.async.Deferred} The deferred Blob contents as text.234* If an error occurs, the errback is called with a {@link goog.fs.Error}.235*/236goog.fs.FileReader.readAsText = function(blob, opt_encoding) {237var reader = new goog.fs.FileReader();238var d = goog.fs.FileReader.createDeferred_(reader);239reader.readAsText(blob, opt_encoding);240return d;241};242243244/**245* Starts reading a blob as a data URL.246* @param {!Blob} blob The blob to read.247*/248goog.fs.FileReader.prototype.readAsDataUrl = function(blob) {249this.reader_.readAsDataURL(blob);250};251252253/**254* Reads a blob as a data URL.255* @param {!Blob} blob The blob to read.256* @return {!goog.async.Deferred} The deferred Blob contents as a data URL.257* If an error occurs, the errback is called with a {@link goog.fs.Error}.258*/259goog.fs.FileReader.readAsDataUrl = function(blob) {260var reader = new goog.fs.FileReader();261var d = goog.fs.FileReader.createDeferred_(reader);262reader.readAsDataUrl(blob);263return d;264};265266267/**268* Creates a new deferred object for the results of a read method.269* @param {goog.fs.FileReader} reader The reader to create a deferred for.270* @return {!goog.async.Deferred} The deferred results.271* @private272*/273goog.fs.FileReader.createDeferred_ = function(reader) {274var deferred = new goog.async.Deferred();275reader.listen(276goog.fs.FileReader.EventType.LOAD_END, goog.partial(function(d, r, e) {277var result = r.getResult();278var error = r.getError();279if (result != null && !error) {280d.callback(result);281} else {282d.errback(error);283}284r.dispose();285}, deferred, reader));286return deferred;287};288289290