Source: ArrayBufferWriter.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 writer which automatically writes to the given output stream with arbitrary
  22. * binary data, supplied as ArrayBuffers.
  23. *
  24. * @constructor
  25. * @param {Guacamole.OutputStream} stream The stream that data will be written
  26. * to.
  27. */
  28. Guacamole.ArrayBufferWriter = function(stream) {
  29. /**
  30. * Reference to this Guacamole.StringWriter.
  31. * @private
  32. */
  33. var guac_writer = this;
  34. // Simply call onack for acknowledgements
  35. stream.onack = function(status) {
  36. if (guac_writer.onack)
  37. guac_writer.onack(status);
  38. };
  39. /**
  40. * Encodes the given data as base64, sending it as a blob. The data must
  41. * be small enough to fit into a single blob instruction.
  42. *
  43. * @private
  44. * @param {Uint8Array} bytes The data to send.
  45. */
  46. function __send_blob(bytes) {
  47. var binary = "";
  48. // Produce binary string from bytes in buffer
  49. for (var i=0; i<bytes.byteLength; i++)
  50. binary += String.fromCharCode(bytes[i]);
  51. // Send as base64
  52. stream.sendBlob(window.btoa(binary));
  53. }
  54. /**
  55. * The maximum length of any blob sent by this Guacamole.ArrayBufferWriter,
  56. * in bytes. Data sent via
  57. * [sendData()]{@link Guacamole.ArrayBufferWriter#sendData} which exceeds
  58. * this length will be split into multiple blobs. As the Guacamole protocol
  59. * limits the maximum size of any instruction or instruction element to
  60. * 8192 bytes, and the contents of blobs will be base64-encoded, this value
  61. * should only be increased with extreme caution.
  62. *
  63. * @type {Number}
  64. * @default {@link Guacamole.ArrayBufferWriter.DEFAULT_BLOB_LENGTH}
  65. */
  66. this.blobLength = Guacamole.ArrayBufferWriter.DEFAULT_BLOB_LENGTH;
  67. /**
  68. * Sends the given data.
  69. *
  70. * @param {ArrayBuffer|TypedArray} data The data to send.
  71. */
  72. this.sendData = function(data) {
  73. var bytes = new Uint8Array(data);
  74. // If small enough to fit into single instruction, send as-is
  75. if (bytes.length <= guac_writer.blobLength)
  76. __send_blob(bytes);
  77. // Otherwise, send as multiple instructions
  78. else {
  79. for (var offset=0; offset<bytes.length; offset += guac_writer.blobLength)
  80. __send_blob(bytes.subarray(offset, offset + guac_writer.blobLength));
  81. }
  82. };
  83. /**
  84. * Signals that no further text will be sent, effectively closing the
  85. * stream.
  86. */
  87. this.sendEnd = function() {
  88. stream.sendEnd();
  89. };
  90. /**
  91. * Fired for received data, if acknowledged by the server.
  92. * @event
  93. * @param {Guacamole.Status} status The status of the operation.
  94. */
  95. this.onack = null;
  96. };
  97. /**
  98. * The default maximum blob length for new Guacamole.ArrayBufferWriter
  99. * instances.
  100. *
  101. * @constant
  102. * @type {Number}
  103. */
  104. Guacamole.ArrayBufferWriter.DEFAULT_BLOB_LENGTH = 6048;