2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 WebInspector.OutputStreamDelegate = function()
38 WebInspector.OutputStreamDelegate.prototype = {
39 onTransferStarted: function() { },
41 onTransferFinished: function() { },
44 * @param {!WebInspector.ChunkedReader} reader
46 onChunkTransferred: function(reader) { },
49 * @param {!WebInspector.ChunkedReader} reader
50 * @param {!Event} event
52 onError: function(reader, event) { },
58 WebInspector.ChunkedReader = function()
62 WebInspector.ChunkedReader.prototype = {
66 fileSize: function() { },
71 loadedSize: function() { },
76 fileName: function() { },
78 cancel: function() { }
83 * @implements {WebInspector.ChunkedReader}
85 * @param {number} chunkSize
86 * @param {!WebInspector.OutputStreamDelegate} delegate
88 WebInspector.ChunkedFileReader = function(file, chunkSize, delegate)
91 this._fileSize = file.size;
93 this._chunkSize = chunkSize;
94 this._delegate = delegate;
95 this._decoder = new TextDecoder();
96 this._isCanceled = false;
99 WebInspector.ChunkedFileReader.prototype = {
101 * @param {!WebInspector.OutputStream} output
103 start: function(output)
105 this._output = output;
107 this._reader = new FileReader();
108 this._reader.onload = this._onChunkLoaded.bind(this);
109 this._reader.onerror = this._delegate.onError.bind(this._delegate, this);
110 this._delegate.onTransferStarted();
119 this._isCanceled = true;
126 loadedSize: function()
128 return this._loadedSize;
137 return this._fileSize;
146 return this._file.name;
150 * @param {!Event} event
152 _onChunkLoaded: function(event)
154 if (this._isCanceled)
157 if (event.target.readyState !== FileReader.DONE)
160 var buffer = event.target.result;
161 this._loadedSize += buffer.byteLength;
162 var endOfFile = this._loadedSize === this._fileSize;
163 var decodedString = this._decoder.decode(buffer, {stream: !endOfFile});
164 this._output.write(decodedString);
165 if (this._isCanceled)
167 this._delegate.onChunkTransferred(this);
172 this._output.close();
173 this._delegate.onTransferFinished();
180 _loadChunk: function()
182 var chunkStart = this._loadedSize;
183 var chunkEnd = Math.min(this._fileSize, chunkStart + this._chunkSize);
184 var nextPart = this._file.slice(chunkStart, chunkEnd);
185 this._reader.readAsArrayBuffer(nextPart);
190 * @param {function(!File)} callback
193 WebInspector.createFileSelectorElement = function(callback)
195 var fileSelectorElement = createElement("input");
196 fileSelectorElement.type = "file";
197 fileSelectorElement.style.display = "none";
198 fileSelectorElement.setAttribute("tabindex", -1);
199 fileSelectorElement.onchange = onChange;
200 function onChange(event)
202 callback(fileSelectorElement.files[0]);
204 return fileSelectorElement;
209 * @implements {WebInspector.OutputStream}
211 WebInspector.FileOutputStream = function()
215 WebInspector.FileOutputStream.prototype = {
217 * @param {string} fileName
218 * @param {function(boolean)} callback
220 open: function(fileName, callback)
222 this._closed = false;
223 this._writeCallbacks = [];
224 this._fileName = fileName;
227 * @param {boolean} accepted
228 * @this {WebInspector.FileOutputStream}
230 function callbackWrapper(accepted)
233 WebInspector.fileManager.addEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
236 WebInspector.fileManager.save(this._fileName, "", true, callbackWrapper.bind(this));
241 * @param {string} data
242 * @param {function(!WebInspector.OutputStream)=} callback
244 write: function(data, callback)
246 this._writeCallbacks.push(callback);
247 WebInspector.fileManager.append(this._fileName, data);
256 if (this._writeCallbacks.length)
258 WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
259 WebInspector.fileManager.close(this._fileName);
263 * @param {!WebInspector.Event} event
265 _onAppendDone: function(event)
267 if (event.data !== this._fileName)
269 var callback = this._writeCallbacks.shift();
272 if (!this._writeCallbacks.length) {
274 WebInspector.fileManager.removeEventListener(WebInspector.FileManager.EventTypes.AppendedToURL, this._onAppendDone, this);
275 WebInspector.fileManager.close(this._fileName);