Source: main/webapp/modules/ArrayBufferReader.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. * strictly received packets as array buffers. Note that this object will
  23. * overwrite any installed event handlers on the given Guacamole.InputStream.
  24. *
  25. * @constructor
  26. * @param {!Guacamole.InputStream} stream
  27. * The stream that data will be read from.
  28. */
  29. Guacamole.ArrayBufferReader = function(stream) {
  30. /**
  31. * Reference to this Guacamole.InputStream.
  32. * @private
  33. */
  34. var guac_reader = this;
  35. // Receive blobs as array buffers
  36. stream.onblob = function(data) {
  37. // Convert to ArrayBuffer
  38. var binary = window.atob(data);
  39. var arrayBuffer = new ArrayBuffer(binary.length);
  40. var bufferView = new Uint8Array(arrayBuffer);
  41. for (var i=0; i<binary.length; i++)
  42. bufferView[i] = binary.charCodeAt(i);
  43. // Call handler, if present
  44. if (guac_reader.ondata)
  45. guac_reader.ondata(arrayBuffer);
  46. };
  47. // Simply call onend when end received
  48. stream.onend = function() {
  49. if (guac_reader.onend)
  50. guac_reader.onend();
  51. };
  52. /**
  53. * Fired once for every blob of data received.
  54. *
  55. * @event
  56. * @param {!ArrayBuffer} buffer
  57. * The data packet received.
  58. */
  59. this.ondata = null;
  60. /**
  61. * Fired once this stream is finished and no further data will be written.
  62. * @event
  63. */
  64. this.onend = null;
  65. };