Source: main/webapp/modules/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
  28. * The stream that data will be read from.
  29. *
  30. * @param {!string} mimetype
  31. * The mimetype of the blob being built.
  32. */
  33. Guacamole.BlobReader = function(stream, mimetype) {
  34. /**
  35. * Reference to this Guacamole.InputStream.
  36. *
  37. * @private
  38. * @type {!Guacamole.BlobReader}
  39. */
  40. var guac_reader = this;
  41. /**
  42. * The length of this Guacamole.InputStream in bytes.
  43. *
  44. * @private
  45. * @type {!number}
  46. */
  47. var length = 0;
  48. // Get blob builder
  49. var blob_builder;
  50. if (window.BlobBuilder) blob_builder = new BlobBuilder();
  51. else if (window.WebKitBlobBuilder) blob_builder = new WebKitBlobBuilder();
  52. else if (window.MozBlobBuilder) blob_builder = new MozBlobBuilder();
  53. else
  54. blob_builder = new (function() {
  55. var blobs = [];
  56. /** @ignore */
  57. this.append = function(data) {
  58. blobs.push(new Blob([data], {"type": mimetype}));
  59. };
  60. /** @ignore */
  61. this.getBlob = function() {
  62. return new Blob(blobs, {"type": mimetype});
  63. };
  64. })();
  65. // Append received blobs
  66. stream.onblob = function(data) {
  67. // Convert to ArrayBuffer
  68. var binary = window.atob(data);
  69. var arrayBuffer = new ArrayBuffer(binary.length);
  70. var bufferView = new Uint8Array(arrayBuffer);
  71. for (var i=0; i<binary.length; i++)
  72. bufferView[i] = binary.charCodeAt(i);
  73. blob_builder.append(arrayBuffer);
  74. length += arrayBuffer.byteLength;
  75. // Call handler, if present
  76. if (guac_reader.onprogress)
  77. guac_reader.onprogress(arrayBuffer.byteLength);
  78. // Send success response
  79. stream.sendAck("OK", 0x0000);
  80. };
  81. // Simply call onend when end received
  82. stream.onend = function() {
  83. if (guac_reader.onend)
  84. guac_reader.onend();
  85. };
  86. /**
  87. * Returns the current length of this Guacamole.InputStream, in bytes.
  88. *
  89. * @return {!number}
  90. * The current length of this Guacamole.InputStream.
  91. */
  92. this.getLength = function() {
  93. return length;
  94. };
  95. /**
  96. * Returns the contents of this Guacamole.BlobReader as a Blob.
  97. *
  98. * @return {!Blob}
  99. * The contents of this Guacamole.BlobReader.
  100. */
  101. this.getBlob = function() {
  102. return blob_builder.getBlob();
  103. };
  104. /**
  105. * Fired once for every blob of data received.
  106. *
  107. * @event
  108. * @param {!number} length
  109. * The number of bytes received.
  110. */
  111. this.onprogress = null;
  112. /**
  113. * Fired once this stream is finished and no further data will be written.
  114. * @event
  115. */
  116. this.onend = null;
  117. };