| 1 |
// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview A wrapper for the HTML5 FileWriter object. |
| 17 |
* |
| 18 |
* When adding or modifying functionality in this namespace, be sure to update |
| 19 |
* the mock counterparts in goog.testing.fs. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.fs.FileWriter'); |
| 24 |
|
| 25 |
goog.require('goog.fs.Error'); |
| 26 |
goog.require('goog.fs.FileSaver'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* An object for monitoring the saving of files, as well as other fine-grained |
| 32 |
* writing operations. |
| 33 |
* |
| 34 |
* This should not be instantiated directly. Instead, it should be accessed via |
| 35 |
* {@link goog.fs.FileEntry#createWriter}. |
| 36 |
* |
| 37 |
* @param {!FileWriter} writer The underlying FileWriter object. |
| 38 |
* @constructor |
| 39 |
* @extends {goog.fs.FileSaver} |
| 40 |
* @final |
| 41 |
*/ |
| 42 |
goog.fs.FileWriter = function(writer) { |
| 43 |
goog.fs.FileWriter.base(this, 'constructor', writer); |
| 44 |
|
| 45 |
/** |
| 46 |
* The underlying FileWriter object. |
| 47 |
* |
| 48 |
* @type {!FileWriter} |
| 49 |
* @private |
| 50 |
*/ |
| 51 |
this.writer_ = writer; |
| 52 |
}; |
| 53 |
goog.inherits(goog.fs.FileWriter, goog.fs.FileSaver); |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* @return {number} The byte offset at which the next write will occur. |
| 58 |
*/ |
| 59 |
goog.fs.FileWriter.prototype.getPosition = function() { |
| 60 |
return this.writer_.position; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/** |
| 65 |
* @return {number} The length of the file. |
| 66 |
*/ |
| 67 |
goog.fs.FileWriter.prototype.getLength = function() { |
| 68 |
return this.writer_.length; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Write data to the file. |
| 74 |
* |
| 75 |
* @param {!Blob} blob The data to write. |
| 76 |
*/ |
| 77 |
goog.fs.FileWriter.prototype.write = function(blob) { |
| 78 |
try { |
| 79 |
this.writer_.write(blob); |
| 80 |
} catch (e) { |
| 81 |
throw new goog.fs.Error(e, 'writing file'); |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Set the file position at which the next write will occur. |
| 88 |
* |
| 89 |
* @param {number} offset An absolute byte offset into the file. |
| 90 |
*/ |
| 91 |
goog.fs.FileWriter.prototype.seek = function(offset) { |
| 92 |
try { |
| 93 |
this.writer_.seek(offset); |
| 94 |
} catch (e) { |
| 95 |
throw new goog.fs.Error(e, 'seeking in file'); |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Changes the length of the file to that specified. |
| 102 |
* |
| 103 |
* @param {number} size The new size of the file, in bytes. |
| 104 |
*/ |
| 105 |
goog.fs.FileWriter.prototype.truncate = function(size) { |
| 106 |
try { |
| 107 |
this.writer_.truncate(size); |
| 108 |
} catch (e) { |
| 109 |
throw new goog.fs.Error(e, 'truncating file'); |
| 110 |
} |
| 111 |
}; |