source: OpenRLabs-Git/web2py/applications/rlabs/static/js/guacamole-common-js/modules/ArrayBufferWriter.js

main
Last change on this file was 42bd667, checked in by David Fuertes <dfuertes@…>, 4 years ago

Historial Limpio

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