Source: main/webapp/modules/StringReader.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 text data. Note that this object will overwrite any installed event
  23. * 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.StringReader = function(stream) {
  30. /**
  31. * Reference to this Guacamole.InputStream.
  32. *
  33. * @private
  34. * @type {!Guacamole.StringReader}
  35. */
  36. var guac_reader = this;
  37. /**
  38. * Parser for received UTF-8 data.
  39. *
  40. * @type {!Guacamole.UTF8Parser}
  41. */
  42. var utf8Parser = new Guacamole.UTF8Parser();
  43. /**
  44. * Wrapped Guacamole.ArrayBufferReader.
  45. *
  46. * @private
  47. * @type {!Guacamole.ArrayBufferReader}
  48. */
  49. var array_reader = new Guacamole.ArrayBufferReader(stream);
  50. // Receive blobs as strings
  51. array_reader.ondata = function(buffer) {
  52. // Decode UTF-8
  53. var text = utf8Parser.decode(buffer);
  54. // Call handler, if present
  55. if (guac_reader.ontext)
  56. guac_reader.ontext(text);
  57. };
  58. // Simply call onend when end received
  59. array_reader.onend = function() {
  60. if (guac_reader.onend)
  61. guac_reader.onend();
  62. };
  63. /**
  64. * Fired once for every blob of text data received.
  65. *
  66. * @event
  67. * @param {!string} text
  68. * The data packet received.
  69. */
  70. this.ontext = null;
  71. /**
  72. * Fired once this stream is finished and no further data will be written.
  73. * @event
  74. */
  75. this.onend = null;
  76. };