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 | * received blobs as a single data URI built over the course of the stream. |
---|
25 | * Note that this object will overwrite any installed event handlers on the |
---|
26 | * given Guacamole.InputStream. |
---|
27 | * |
---|
28 | * @constructor |
---|
29 | * @param {Guacamole.InputStream} stream |
---|
30 | * The stream that data will be read from. |
---|
31 | */ |
---|
32 | Guacamole.DataURIReader = function(stream, mimetype) { |
---|
33 | |
---|
34 | /** |
---|
35 | * Reference to this Guacamole.DataURIReader. |
---|
36 | * @private |
---|
37 | */ |
---|
38 | var guac_reader = this; |
---|
39 | |
---|
40 | /** |
---|
41 | * Current data URI. |
---|
42 | * |
---|
43 | * @private |
---|
44 | * @type {String} |
---|
45 | */ |
---|
46 | var uri = 'data:' + mimetype + ';base64,'; |
---|
47 | |
---|
48 | // Receive blobs as array buffers |
---|
49 | stream.onblob = function dataURIReaderBlob(data) { |
---|
50 | |
---|
51 | // Currently assuming data will ALWAYS be safe to simply append. This |
---|
52 | // will not be true if the received base64 data encodes a number of |
---|
53 | // bytes that isn't a multiple of three (as base64 expands in a ratio |
---|
54 | // of exactly 3:4). |
---|
55 | uri += data; |
---|
56 | |
---|
57 | }; |
---|
58 | |
---|
59 | // Simply call onend when end received |
---|
60 | stream.onend = function dataURIReaderEnd() { |
---|
61 | if (guac_reader.onend) |
---|
62 | guac_reader.onend(); |
---|
63 | }; |
---|
64 | |
---|
65 | /** |
---|
66 | * Returns the data URI of all data received through the underlying stream |
---|
67 | * thus far. |
---|
68 | * |
---|
69 | * @returns {String} |
---|
70 | * The data URI of all data received through the underlying stream thus |
---|
71 | * far. |
---|
72 | */ |
---|
73 | this.getURI = function getURI() { |
---|
74 | return uri; |
---|
75 | }; |
---|
76 | |
---|
77 | /** |
---|
78 | * Fired once this stream is finished and no further data will be written. |
---|
79 | * |
---|
80 | * @event |
---|
81 | */ |
---|
82 | this.onend = null; |
---|
83 | |
---|
84 | }; |
---|