Path: blob/trunk/third_party/closure/goog/fs/filewriter.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 FileWriter object.16*17* When adding or modifying functionality in this namespace, be sure to update18* the mock counterparts in goog.testing.fs.19*20*/2122goog.provide('goog.fs.FileWriter');2324goog.require('goog.fs.Error');25goog.require('goog.fs.FileSaver');26272829/**30* An object for monitoring the saving of files, as well as other fine-grained31* writing operations.32*33* This should not be instantiated directly. Instead, it should be accessed via34* {@link goog.fs.FileEntry#createWriter}.35*36* @param {!FileWriter} writer The underlying FileWriter object.37* @constructor38* @extends {goog.fs.FileSaver}39* @final40*/41goog.fs.FileWriter = function(writer) {42goog.fs.FileWriter.base(this, 'constructor', writer);4344/**45* The underlying FileWriter object.46*47* @type {!FileWriter}48* @private49*/50this.writer_ = writer;51};52goog.inherits(goog.fs.FileWriter, goog.fs.FileSaver);535455/**56* @return {number} The byte offset at which the next write will occur.57*/58goog.fs.FileWriter.prototype.getPosition = function() {59return this.writer_.position;60};616263/**64* @return {number} The length of the file.65*/66goog.fs.FileWriter.prototype.getLength = function() {67return this.writer_.length;68};697071/**72* Write data to the file.73*74* @param {!Blob} blob The data to write.75*/76goog.fs.FileWriter.prototype.write = function(blob) {77try {78this.writer_.write(blob);79} catch (e) {80throw new goog.fs.Error(e, 'writing file');81}82};838485/**86* Set the file position at which the next write will occur.87*88* @param {number} offset An absolute byte offset into the file.89*/90goog.fs.FileWriter.prototype.seek = function(offset) {91try {92this.writer_.seek(offset);93} catch (e) {94throw new goog.fs.Error(e, 'seeking in file');95}96};979899/**100* Changes the length of the file to that specified.101*102* @param {number} size The new size of the file, in bytes.103*/104goog.fs.FileWriter.prototype.truncate = function(size) {105try {106this.writer_.truncate(size);107} catch (e) {108throw new goog.fs.Error(e, 'truncating file');109}110};111112113