filewriter.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. * @fileoverview A wrapper for the HTML5 FileWriter object.
  16. *
  17. * When adding or modifying functionality in this namespace, be sure to update
  18. * the mock counterparts in goog.testing.fs.
  19. *
  20. */
  21. goog.provide('goog.fs.FileWriter');
  22. goog.require('goog.fs.Error');
  23. goog.require('goog.fs.FileSaver');
  24. /**
  25. * An object for monitoring the saving of files, as well as other fine-grained
  26. * writing operations.
  27. *
  28. * This should not be instantiated directly. Instead, it should be accessed via
  29. * {@link goog.fs.FileEntry#createWriter}.
  30. *
  31. * @param {!FileWriter} writer The underlying FileWriter object.
  32. * @constructor
  33. * @extends {goog.fs.FileSaver}
  34. * @final
  35. */
  36. goog.fs.FileWriter = function(writer) {
  37. goog.fs.FileWriter.base(this, 'constructor', writer);
  38. /**
  39. * The underlying FileWriter object.
  40. *
  41. * @type {!FileWriter}
  42. * @private
  43. */
  44. this.writer_ = writer;
  45. };
  46. goog.inherits(goog.fs.FileWriter, goog.fs.FileSaver);
  47. /**
  48. * @return {number} The byte offset at which the next write will occur.
  49. */
  50. goog.fs.FileWriter.prototype.getPosition = function() {
  51. return this.writer_.position;
  52. };
  53. /**
  54. * @return {number} The length of the file.
  55. */
  56. goog.fs.FileWriter.prototype.getLength = function() {
  57. return this.writer_.length;
  58. };
  59. /**
  60. * Write data to the file.
  61. *
  62. * @param {!Blob} blob The data to write.
  63. */
  64. goog.fs.FileWriter.prototype.write = function(blob) {
  65. try {
  66. this.writer_.write(blob);
  67. } catch (e) {
  68. throw new goog.fs.Error(e, 'writing file');
  69. }
  70. };
  71. /**
  72. * Set the file position at which the next write will occur.
  73. *
  74. * @param {number} offset An absolute byte offset into the file.
  75. */
  76. goog.fs.FileWriter.prototype.seek = function(offset) {
  77. try {
  78. this.writer_.seek(offset);
  79. } catch (e) {
  80. throw new goog.fs.Error(e, 'seeking in file');
  81. }
  82. };
  83. /**
  84. * Changes the length of the file to that specified.
  85. *
  86. * @param {number} size The new size of the file, in bytes.
  87. */
  88. goog.fs.FileWriter.prototype.truncate = function(size) {
  89. try {
  90. this.writer_.truncate(size);
  91. } catch (e) {
  92. throw new goog.fs.Error(e, 'truncating file');
  93. }
  94. };