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