Source: BlobReader.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * A reader which automatically handles the given input stream, assembling all
  22. * received blobs into a single blob by appending them to each other in order.
  23. * Note that this object will overwrite any installed event handlers on the
  24. * given Guacamole.InputStream.
  25. *
  26. * @constructor
  27. * @param {Guacamole.InputStream} stream The stream that data will be read
  28. * from.
  29. * @param {String} mimetype The mimetype of the blob being built.
  30. */
  31. Guacamole.BlobReader = function(stream, mimetype) {
  32. /**
  33. * Reference to this Guacamole.InputStream.
  34. * @private
  35. */
  36. var guac_reader = this;
  37. /**
  38. * The length of this Guacamole.InputStream in bytes.
  39. * @private
  40. */
  41. var length = 0;
  42. // Get blob builder
  43. var blob_builder;
  44. if (window.BlobBuilder) blob_builder = new BlobBuilder();
  45. else if (window.WebKitBlobBuilder) blob_builder = new WebKitBlobBuilder();
  46. else if (window.MozBlobBuilder) blob_builder = new MozBlobBuilder();
  47. else
  48. blob_builder = new (function() {
  49. var blobs = [];
  50. /** @ignore */
  51. this.append = function(data) {
  52. blobs.push(new Blob([data], {"type": mimetype}));
  53. };
  54. /** @ignore */
  55. this.getBlob = function() {
  56. return new Blob(blobs, {"type": mimetype});
  57. };
  58. })();
  59. // Append received blobs
  60. stream.onblob = function(data) {
  61. // Convert to ArrayBuffer
  62. var binary = window.atob(data);
  63. var arrayBuffer = new ArrayBuffer(binary.length);
  64. var bufferView = new Uint8Array(arrayBuffer);
  65. for (var i=0; i<binary.length; i++)
  66. bufferView[i] = binary.charCodeAt(i);
  67. blob_builder.append(arrayBuffer);
  68. length += arrayBuffer.byteLength;
  69. // Call handler, if present
  70. if (guac_reader.onprogress)
  71. guac_reader.onprogress(arrayBuffer.byteLength);
  72. // Send success response
  73. stream.sendAck("OK", 0x0000);
  74. };
  75. // Simply call onend when end received
  76. stream.onend = function() {
  77. if (guac_reader.onend)
  78. guac_reader.onend();
  79. };
  80. /**
  81. * Returns the current length of this Guacamole.InputStream, in bytes.
  82. * @return {Number} The current length of this Guacamole.InputStream.
  83. */
  84. this.getLength = function() {
  85. return length;
  86. };
  87. /**
  88. * Returns the contents of this Guacamole.BlobReader as a Blob.
  89. * @return {Blob} The contents of this Guacamole.BlobReader.
  90. */
  91. this.getBlob = function() {
  92. return blob_builder.getBlob();
  93. };
  94. /**
  95. * Fired once for every blob of data received.
  96. *
  97. * @event
  98. * @param {Number} length The number of bytes received.
  99. */
  100. this.onprogress = null;
  101. /**
  102. * Fired once this stream is finished and no further data will be written.
  103. * @event
  104. */
  105. this.onend = null;
  106. };