Source: main/webapp/modules/DataURIReader.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, returning
  22. * received blobs as a single data URI built over the course of the stream.
  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 data being received.
  32. */
  33. Guacamole.DataURIReader = function(stream, mimetype) {
  34. /**
  35. * Reference to this Guacamole.DataURIReader.
  36. *
  37. * @private
  38. * @type {!Guacamole.DataURIReader}
  39. */
  40. var guac_reader = this;
  41. /**
  42. * Current data URI.
  43. *
  44. * @private
  45. * @type {!string}
  46. */
  47. var uri = 'data:' + mimetype + ';base64,';
  48. // Receive blobs as array buffers
  49. stream.onblob = function dataURIReaderBlob(data) {
  50. // Currently assuming data will ALWAYS be safe to simply append. This
  51. // will not be true if the received base64 data encodes a number of
  52. // bytes that isn't a multiple of three (as base64 expands in a ratio
  53. // of exactly 3:4).
  54. uri += data;
  55. };
  56. // Simply call onend when end received
  57. stream.onend = function dataURIReaderEnd() {
  58. if (guac_reader.onend)
  59. guac_reader.onend();
  60. };
  61. /**
  62. * Returns the data URI of all data received through the underlying stream
  63. * thus far.
  64. *
  65. * @returns {!string}
  66. * The data URI of all data received through the underlying stream thus
  67. * far.
  68. */
  69. this.getURI = function getURI() {
  70. return uri;
  71. };
  72. /**
  73. * Fired once this stream is finished and no further data will be written.
  74. *
  75. * @event
  76. */
  77. this.onend = null;
  78. };