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