You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2016/04/06 17:06:31 UTC
[33/50] [abbrv] git commit: [flex-asjs] [refs/heads/master] - Added
file streaming input and output to FlexJS Storage project.
Added file streaming input and output to FlexJS Storage project.
Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/dcf3b3b9
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/dcf3b3b9
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/dcf3b3b9
Branch: refs/heads/master
Commit: dcf3b3b95388b86060c85cd01398eb98e1a17db6
Parents: 3cab6cf
Author: Peter Ent <pe...@apache.org>
Authored: Mon Mar 14 17:15:08 2016 -0400
Committer: Peter Ent <pe...@apache.org>
Committed: Mon Mar 14 17:15:08 2016 -0400
----------------------------------------------------------------------
.../StorageExample/src/MyInitialView.mxml | 158 +++++++++++++++---
.../Storage/src/main/flex/StorageClasses.as | 8 +-
.../apache/flex/storage/IPermanentStorage.as | 28 ++++
.../org/apache/flex/storage/PermanentStorage.as | 34 ++++
.../flex/storage/events/FileErrorEvent.as | 80 +++++++++
.../org/apache/flex/storage/events/FileEvent.as | 84 ++++++++++
.../apache/flex/storage/events/FileReadEvent.as | 70 --------
.../flex/storage/events/FileWriteEvent.as | 60 -------
.../apache/flex/storage/file/DataInputStream.as | 146 ++++++++++++++++
.../flex/storage/file/DataOutputStream.as | 165 +++++++++++++++++++
.../org/apache/flex/storage/file/IDataInput.as | 32 ++++
.../org/apache/flex/storage/file/IDataOutput.as | 32 ++++
.../org/apache/flex/storage/file/IDataStream.as | 32 ++++
.../storage/providers/AirStorageProvider.as | 106 +++++++++++-
.../providers/IPermanentStorageProvider.as | 28 ++++
.../storage/providers/WebStorageProvider.as | 116 +++++++++++--
16 files changed, 1001 insertions(+), 178 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/examples/flexjs/StorageExample/src/MyInitialView.mxml
----------------------------------------------------------------------
diff --git a/examples/flexjs/StorageExample/src/MyInitialView.mxml b/examples/flexjs/StorageExample/src/MyInitialView.mxml
index 2bb6b7a..0717fcd 100644
--- a/examples/flexjs/StorageExample/src/MyInitialView.mxml
+++ b/examples/flexjs/StorageExample/src/MyInitialView.mxml
@@ -24,39 +24,88 @@ limitations under the License.
<fx:Script>
<![CDATA[
import org.apache.flex.storage.PermanentStorage;
- import org.apache.flex.storage.events.FileReadEvent;
- import org.apache.flex.storage.events.FileWriteEvent;
+ import org.apache.flex.storage.events.FileEvent;
+ import org.apache.flex.storage.events.FileErrorEvent;
+ import org.apache.flex.storage.file.IDataInput;
+ import org.apache.flex.storage.file.IDataOutput;
private function startup():void
{
trace("Starting up");
}
+ // READING - PLAIN
+
private function onRead():void
{
var storage:PermanentStorage = new PermanentStorage();
var useFile:String = readFileNameField.text;
- storage.addEventListener("COMPLETE", handleRead);
+ storage.addEventListener("READ", handleRead);
storage.addEventListener("ERROR", handleRead);
storage.readTextFromDataFile( useFile );
}
- private function handleRead(event:FileReadEvent):void
+ private function handleRead(event:FileEvent):void
{
- trace("Read event type: "+event.type);
- if (event.type == "ERROR") {
- status.text = "Error: "+event.errorMessage;
- readContent.text = "";
- }
- else {
- trace(event.data);
- status.text = "Read Success!";
- readContent.text = String(event.data);
- }
+ trace(event.data);
+ status.text = "Read Success!";
+ readContent.text = String(event.data);
+ }
+
+ private function handleReadError(event:FileErrorEvent):void
+ {
+ status.text = "Error: "+event.errorMessage;
+ readContent.text = "";
+ }
+
+ // READING - STREAM
+
+ private function onReadStream():void
+ {
+ var storage:PermanentStorage = new PermanentStorage();
+ var useFile:String = readFileNameField.text;
+
+ storage.addEventListener("READY", handleReadStreamReady);
+ storage.addEventListener("READ", handleReadStreamData);
+ storage.addEventListener("COMPLETE", handleReadStreamComplete);
+ storage.addEventListener("ERROR", handleReadStreamError);
+ storage.openInputDataStream(useFile);
}
+ private function handleReadStreamReady(event:FileEvent):void
+ {
+ trace("INPUT STREAM READY");
+
+ status.text = "Input stream ready";
+
+ var stream:IDataInput = event.stream as IDataInput;
+ stream.readText();
+ }
+
+ private function handleReadStreamData(event:FileEvent):void
+ {
+ trace("INPUT STREAM DATA.");
+ var stream:IDataInput = event.stream as IDataInput;
+
+ status.text = "Input stream reading";
+ readContent.text = String(event.data);
+ }
+
+ private function handleReadStreamComplete(event:FileEvent):void
+ {
+ trace("INPUT STREAM CLOSED");
+ status.text = "Input stream closed";
+ }
+
+ private function handleReadStreamError(event:FileErrorEvent):void
+ {
+ status.text = event.errorMessage;
+ }
+
+ // SAVING - PLAIN
+
private function onSave():void
{
var storage:PermanentStorage = new PermanentStorage();
@@ -65,18 +114,75 @@ limitations under the License.
var useFile:String = writeFileNameField.text;
var useContent:String = today.toString() + " " + contentField.text;
- storage.addEventListener("COMPLETE", handleSave);
+ storage.addEventListener("WRITE", handleSave);
storage.writeTextToDataFile(useFile, useContent);
}
- private function handleSave(event:FileWriteEvent):void
+ private function handleSave(event:FileEvent):void
+ {
+ status.text = "Write Success!";
+ }
+
+ private function handleSaveError(event:FileErrorEvent):void
+ {
+ status.text = "Error: "+event.errorMessage;
+ }
+
+ // SAVING - STREAM
+
+ private var writeCount:Number = 0;
+
+ private function onSaveStream():void
+ {
+ var storage:PermanentStorage = new PermanentStorage();
+ var useFile:String = writeFileNameField.text;
+
+ storage.addEventListener("READY", handleSaveStreamReady);
+ storage.addEventListener("WRITE", handleSaveStreamData);
+ storage.addEventListener("COMPLETE", handleSaveStreamComplete);
+ storage.addEventListener("ERROR", handleSaveStreamError);
+ storage.openOutputDataStream(useFile);
+ }
+
+ private function handleSaveStreamReady(event:FileEvent):void
{
- if (event.type == "ERROR") {
- status.text = "Error: "+event.errorMessage;
- } else {
- status.text = "Write Success!";
+ writeCount = 3;
+ trace("OUTPUT STREAM READY");
+
+ status.text = "Output stream ready";
+ var today:Date = new Date();
+ var useContent:String = "("+String(writeCount)+") "+today.toString() + " " + contentField.text + "\n";
+
+ var stream:IDataOutput = event.stream as IDataOutput;
+ stream.writeText(useContent);
+ }
+
+ private function handleSaveStreamData(event:FileEvent):void
+ {
+ trace("OUTPUT STREAM DATA. Count = "+writeCount);
+ var stream:IDataOutput = event.stream as IDataOutput;
+
+ if (--writeCount == 0) {
+ stream.close();
+ }
+ else {
+ status.text = "Output stream writing";
+ var today:Date = new Date();
+ var useContent:String = "("+String(writeCount)+") "+today.toString() + " " + contentField.text + "\n";
+ stream.writeText(useContent);
}
}
+
+ private function handleSaveStreamComplete(event:FileEvent):void
+ {
+ trace("OUTPUT STREAM CLOSED");
+ status.text = "Output stream closed";
+ }
+
+ private function handleSaveStreamError(event:FileErrorEvent):void
+ {
+ status.text = event.errorMessage;
+ }
]]>
</fx:Script>
@@ -108,7 +214,11 @@ limitations under the License.
<js:Label text="Content:" className="labelStyle" />
<js:TextInput id="contentField" />
</js:HContainer>
- <js:TextButton text="SAVE" click="onSave()" />
+ <js:HContainer>
+ <js:TextButton text="SAVE" click="onSave()" />
+ <js:Spacer width="10" />
+ <js:TextButton text="SAVE STREAM" click="onSaveStream()" />
+ </js:HContainer>
<js:Spacer height="20" />
<js:Label id="status" />
@@ -118,7 +228,11 @@ limitations under the License.
<js:Label text="Read File Name:" className="labelStyle" />
<js:TextInput id="readFileNameField" text="testfile.txt" />
</js:HContainer>
- <js:TextButton text="READ" click="onRead()" />
+ <js:HContainer>
+ <js:TextButton text="READ" click="onRead()" />
+ <js:Spacer width="10" />
+ <js:TextButton text="READ STREAM" click="onReadStream()" />
+ </js:HContainer>
<js:HContainer>
<js:Label text="Content:" className="labelStyle" />
</js:HContainer>
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/StorageClasses.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/StorageClasses.as b/frameworks/projects/Storage/src/main/flex/StorageClasses.as
index 92d9bd4..9fb9ea3 100644
--- a/frameworks/projects/Storage/src/main/flex/StorageClasses.as
+++ b/frameworks/projects/Storage/src/main/flex/StorageClasses.as
@@ -31,11 +31,13 @@ internal class StorageClasses
import org.apache.flex.storage.providers.LocalStorageProvider; LocalStorageProvider;
import org.apache.flex.storage.PermanentStorage; PermanentStorage;
- import org.apache.flex.storage.events.FileReadEvent; FileReadEvent;
- import org.apache.flex.storage.events.FileWriteEvent; FileWriteEvent;
+ import org.apache.flex.storage.events.FileEvent; FileEvent;
+ import org.apache.flex.storage.events.FileErrorEvent; FileErrorEvent;
+
+ import org.apache.flex.storage.file.DataInputStream; DataInputStream;
+ import org.apache.flex.storage.file.DataOutputStream; DataOutputStream;
import org.apache.flex.storage.providers.AirStorageProvider; AirStorageProvider;
-
import org.apache.flex.storage.providers.WebStorageProvider; WebStorageProvider;
}
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IPermanentStorage.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IPermanentStorage.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IPermanentStorage.as
index d47866b..d461420 100644
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IPermanentStorage.as
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/IPermanentStorage.as
@@ -59,5 +59,33 @@ package org.apache.flex.storage
* @productversion FlexJS 0.0
*/
function writeTextToDataFile( fileName:String, text:String ) : void;
+
+ /**
+ * Opens an output stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * write data to the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ function openOutputDataStream( fileName:String ) : void;
+
+ /**
+ * Opens an input stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * read data from the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ function openInputDataStream( fileName:String ) : void;
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/PermanentStorage.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/PermanentStorage.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/PermanentStorage.as
index d1e8887..178dee8 100644
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/PermanentStorage.as
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/PermanentStorage.as
@@ -93,5 +93,39 @@ package org.apache.flex.storage
{
_storageProvider.writeTextToDataFile( fileName, text );
}
+
+ /**
+ * Opens an output stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * write data to the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openOutputDataStream( fileName:String ) : void
+ {
+ _storageProvider.openOutputDataStream(fileName);
+ }
+
+ /**
+ * Opens an input stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * read data from the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openInputDataStream( fileName:String ) : void
+ {
+ _storageProvider.openInputDataStream(fileName);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileErrorEvent.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileErrorEvent.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileErrorEvent.as
new file mode 100644
index 0000000..9491ad2
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileErrorEvent.as
@@ -0,0 +1,80 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.events
+{
+ import org.apache.flex.events.Event;
+ import org.apache.flex.storage.file.IDataStream;
+
+ /**
+ * The FileErrorEvent class is used to signal varies errors that may occur
+ * while handling a permanent file.
+ *
+ * ERROR - A genric error occurred reading or opening the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public class FileErrorEvent extends Event
+ {
+ /**
+ * Constructor.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function FileErrorEvent(type:String)
+ {
+ super(type);
+ }
+
+ /**
+ * Contains the IDataOutput or IDataInput stream on which the error occurred.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var stream:IDataStream;
+
+ /**
+ * If not null, the error that occurred opening or reading the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var errorMessage:String;
+
+ /**
+ * An error code, if any, associated with the error that triggered this event.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var errorCode:Number;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileEvent.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileEvent.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileEvent.as
new file mode 100644
index 0000000..423db20
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileEvent.as
@@ -0,0 +1,84 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.events
+{
+ import org.apache.flex.events.Event;
+ import org.apache.flex.storage.file.IDataStream;
+
+ /**
+ * The FileEvent class is used to signal varies events in the life and
+ * use of permanent files.
+ *
+ * READY - The file has been created or opened successfully.
+ * READ - Some (or all) of the data has been read.
+ * WRITE - Some (or all) of the data has been written.
+ * COMPLETE - The read or write process has finished and the file is closed.
+ * ERROR - An error occurred reading or opening the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public class FileEvent extends Event
+ {
+ /**
+ * Constructor.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function FileEvent(type:String)
+ {
+ super(type);
+ }
+
+ /**
+ * The data read from the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var data:String;
+
+ /**
+ * Contains the IDataOutput or IDataInput stream to use for streaming.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var stream:IDataStream;
+
+ /**
+ * If not null, the error that occurred opening or reading the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public var errorMessage:String;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileReadEvent.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileReadEvent.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileReadEvent.as
deleted file mode 100644
index 528c36b..0000000
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileReadEvent.as
+++ /dev/null
@@ -1,70 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-//
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You under the Apache License, Version 2.0
-// (the "License"); you may not use this file except in compliance with
-// the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-////////////////////////////////////////////////////////////////////////////////
-package org.apache.flex.storage.events
-{
- import org.apache.flex.events.Event;
-
- /**
- * The FileReadEvent class is used to signal varies events in the life and
- * use of permanent files.
- *
- * COMPLETE - The file has been completely read.
- * ERROR - An error occurred reading or opening the file.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public class FileReadEvent extends Event
- {
- /**
- * Constructor.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public function FileReadEvent(type:String)
- {
- super(type);
- }
-
- /**
- * The data read from the file.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public var data:String;
-
- /**
- * If not null, the error that occurred opening or reading the file.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public var errorMessage:String;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileWriteEvent.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileWriteEvent.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileWriteEvent.as
deleted file mode 100644
index fea4542..0000000
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/events/FileWriteEvent.as
+++ /dev/null
@@ -1,60 +0,0 @@
-////////////////////////////////////////////////////////////////////////////////
-//
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You under the Apache License, Version 2.0
-// (the "License"); you may not use this file except in compliance with
-// the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-////////////////////////////////////////////////////////////////////////////////
-package org.apache.flex.storage.events
-{
- import org.apache.flex.events.Event;
-
- /**
- * The FileWriteEvent class is used to signal varies events in the life and
- * use of permanent files.
- *
- * COMPLETE - The file has been successfully written.
- * ERROR - An error occurred writing or opening the file.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public class FileWriteEvent extends Event
- {
- /**
- * Constructor.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public function FileWriteEvent(type:String)
- {
- super(type);
- }
-
- /**
- * If not null, the error that occurred opening or writing the file.
- *
- * @langversion 3.0
- * @playerversion Flash 10.2
- * @playerversion AIR 2.6
- * @productversion FlexJS 0.0
- */
- public var errorMessage:String;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataInputStream.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataInputStream.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataInputStream.as
new file mode 100644
index 0000000..369eeb7
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataInputStream.as
@@ -0,0 +1,146 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.file
+{
+import org.apache.flex.events.EventDispatcher;
+import org.apache.flex.events.IEventDispatcher;
+import org.apache.flex.storage.events.FileEvent;
+
+COMPILE::AS3 {
+ import flash.filesystem.File;
+ import flash.filesystem.FileMode;
+ import flash.filesystem.FileStream;
+ import flash.utils.ByteArray;
+ import flash.errors.IOError;
+}
+
+/**
+ * Instances of the DataInputStream by the storage provider are created when a
+ * file is opened for input streaming. That is, its contents are read in chunks.
+ * Events are dispatched when a chunk has been read, if there is an error, and
+ * when the file has been read completely.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+public class DataInputStream extends EventDispatcher implements IDataInput
+{
+ /**
+ * Constructor.
+ *
+ * @param target The object to use as the event dispatch target.
+ * @param fileHandle A platform-specific handle to the file system
+ * @param fileReader A platform-specific handle to the file system
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function DataInputStream(target:IEventDispatcher, fileHandle:Object, fileReader:Object)
+ {
+ super();
+
+ _target = target;
+
+ COMPILE::JS {
+ _fileHandle = fileHandle as File;
+ _fileReader = fileReader as FileReader;
+
+ var self:DataInputStream = this;
+
+ _fileReader.onloadend = function (e):void {
+ var streamEvent:FileEvent = new FileEvent("READ");
+ streamEvent.stream = self;
+ streamEvent.data = this.result;
+ _target.dispatchEvent(streamEvent);
+
+ self.close();
+ };
+ }
+
+ COMPILE::AS3 {
+ _fileHandle = fileHandle as File;
+ _fileReader = fileReader as FileStream;
+ }
+ }
+
+ /**
+ * @target
+ */
+ private var _target:IEventDispatcher;
+
+ COMPILE::JS {
+ private var _fileHandle:File;
+ private var _fileReader:FileReader;
+ }
+ COMPILE::AS3 {
+ private var _fileHandle:File;
+ private var _fileReader:FileStream;
+ }
+
+ /**
+ * Reads a chunk of text. A FileEvent "READ" event is dispatched when
+ * the chunk is read.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function readText():void
+ {
+ COMPILE::JS {
+ _fileReader.readAsText(_fileHandle);
+ }
+ COMPILE::AS3 {
+ var bytes:ByteArray = new ByteArray();
+ _fileReader.readBytes(bytes);
+
+ var text:String = new String(bytes);
+
+ var newEvent:FileEvent = new FileEvent("READ");
+ newEvent.data = text;
+ _target.dispatchEvent(newEvent);
+ }
+ }
+
+ /**
+ * Closes the file (and dispatches a FileEvent "COMPLETE" event).
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function close():void
+ {
+ COMPILE::AS3 {
+ _fileReader.close();
+ }
+
+ var endEvent:FileEvent = new FileEvent("COMPLETE");
+ endEvent.stream = this;
+ _target.dispatchEvent(endEvent);
+ }
+}
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataOutputStream.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataOutputStream.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataOutputStream.as
new file mode 100644
index 0000000..e19f4ca
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/DataOutputStream.as
@@ -0,0 +1,165 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.file
+{
+import org.apache.flex.events.EventDispatcher;
+import org.apache.flex.events.IEventDispatcher;
+import org.apache.flex.storage.events.FileEvent;
+import org.apache.flex.storage.events.FileErrorEvent;
+
+COMPILE::AS3 {
+ import flash.filesystem.File;
+ import flash.filesystem.FileMode;
+ import flash.filesystem.FileStream;
+ import flash.utils.ByteArray;
+ import flash.errors.IOError;
+}
+
+/**
+ * Instances of the DataOutput stream are created by the storage provider when a file
+ * is opened for output streaming. Events are dispatches as the file is opened, written to,
+ * and finally closed.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+public class DataOutputStream extends EventDispatcher implements IDataOutput
+{
+ /**
+ * Constructor.
+ *
+ * @param target The object to as the event dispatch target.
+ * @param fileHandle A reference to a platform-specific file object.
+ * @param fileWriter A reference to a platform-specific file object.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function DataOutputStream(target:IEventDispatcher, fileHandle:Object, fileWriter:Object)
+ {
+ super();
+
+ _target = target;
+
+ COMPILE::JS {
+ _fileHandle = fileHandle as FileEntry;
+ _fileWriter = fileWriter as FileWriter;
+
+ var self:DataOutputStream = this;
+
+ _fileWriter.onwriteend = function (e):void {
+ var newEvent:FileEvent = new FileEvent("WRITE");
+ newEvent.stream = self;
+ _target.dispatchEvent(newEvent);
+ };
+
+ _fileWriter.onerror = function (e):void {
+ var newEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ newEvent.stream = self;
+ newEvent.errorMessage = "Failed to write the file.";
+ _target.dispatchEvent(newEvent);
+ };
+
+ _fileWriter.truncate(0);
+ }
+
+ COMPILE::AS3 {
+ _fileHandle = fileHandle as File;
+ _fileWriter = fileWriter as FileStream;
+ }
+ }
+
+ /**
+ * @private
+ */
+ private var _target:IEventDispatcher;
+
+ COMPILE::JS {
+ private var _fileHandle:FileEntry;
+ private var _fileWriter:FileWriter;
+ }
+ COMPILE::AS3 {
+ private var _fileHandle:File;
+ private var _fileWriter:FileStream;
+ }
+
+ /**
+ * Writes a chunk of text into the file. When the file is ready to accept the nex
+ * chunk, a FileEvent "WRITE" event is dispatched.
+ *
+ * @param text The string to write into the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function writeText(text:String):void
+ {
+ COMPILE::JS {
+ var blob:Blob = new Blob([text], { type: 'text/plain' });
+ _fileWriter.write(blob);
+ }
+ COMPILE::AS3 {
+ try {
+ _fileWriter.writeUTFBytes(text);
+
+ var newEvent:FileEvent = new FileEvent("WRITE");
+ newEvent.stream = this;
+ _target.dispatchEvent(newEvent);
+
+ } catch(ioerror:IOError) {
+ var ioEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ ioEvent.errorMessage = "I/O Error";
+ ioEvent.errorCode = 2;
+ _target.dispatchEvent(ioEvent);
+ } catch(secerror:SecurityError) {
+ var secEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ secEvent.errorMessage = "Security Error";
+ secEvent.errorCode = 3;
+ _target.dispatchEvent(secEvent);
+ }
+ }
+ }
+
+ /**
+ * Closes the file and dispatches a FileEvent "COMPLETE" event.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function close():void
+ {
+ COMPILE::AS3 {
+ _fileWriter.close();
+ }
+
+ var newEvent:FileEvent = new FileEvent("COMPLETE");
+ newEvent.stream = this;
+ _target.dispatchEvent(newEvent);
+ }
+}
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataInput.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataInput.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataInput.as
new file mode 100644
index 0000000..0e3b41e
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataInput.as
@@ -0,0 +1,32 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.file
+{
+
+/**
+ * The File class provides access to a specific file on the device.
+ */
+public interface IDataInput extends IDataStream
+{
+ function readText():void;
+
+ function close():void;
+}
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataOutput.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataOutput.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataOutput.as
new file mode 100644
index 0000000..0105c04
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataOutput.as
@@ -0,0 +1,32 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.file
+{
+
+/**
+ * The File class provides access to a specific file on the device.
+ */
+public interface IDataOutput extends IDataStream
+{
+ function writeText(text:String):void;
+
+ function close():void;
+}
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataStream.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataStream.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataStream.as
new file mode 100644
index 0000000..448aef4
--- /dev/null
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/file/IDataStream.as
@@ -0,0 +1,32 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.storage.file
+{
+
+import org.apache.flex.events.IEventDispatcher;
+
+/**
+ * The File class provides access to a specific file on the device.
+ */
+public interface IDataStream
+{
+
+}
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AirStorageProvider.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AirStorageProvider.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AirStorageProvider.as
index 185b6a3..7657532 100644
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AirStorageProvider.as
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/AirStorageProvider.as
@@ -28,14 +28,20 @@ package org.apache.flex.storage.providers
import flash.utils.ByteArray;
import flash.errors.IOError;
}
-
- import org.apache.flex.storage.events.FileReadEvent;
- import org.apache.flex.storage.events.FileWriteEvent;
+
+ import org.apache.flex.storage.events.FileEvent;
+ import org.apache.flex.storage.events.FileErrorEvent;
+ import org.apache.flex.storage.file.DataOutputStream;
+ import org.apache.flex.storage.file.DataInputStream;
/**
* The AirStorageProvider class implements the IPermanentStorageProvider
* interface for saving files to a mobile device using the Adobe(tm) AIR platform.
*
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
* @flexjsignorecoercion FileEntry
* @flexjsignorecoercion FileWriter
* @flexjsignorecoercion window
@@ -100,8 +106,9 @@ package org.apache.flex.storage.providers
var file:File = File.applicationStorageDirectory.resolvePath(fileName);
if (!file.exists) {
- var errEvent:FileReadEvent = new FileReadEvent("ERROR");
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
errEvent.errorMessage = "File does not exist.";
+ errEvent.errorCode = 1;
_target.dispatchEvent(errEvent);
return;
}
@@ -115,9 +122,46 @@ package org.apache.flex.storage.providers
var text:String = new String(bytes);
- var newEvent:FileReadEvent = new FileReadEvent("COMPLETE");
+ var newEvent:FileEvent = new FileEvent("READ");
newEvent.data = text;
_target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
+ }
+ }
+
+ /**
+ * Opens a file for input streaming. Events are dispatched when the file is ready
+ * for reading, after each read, and when the file is closed.
+ *
+ * @param fileName The name of the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openInputDataStream( fileName:String ) : void
+ {
+ COMPILE::AS3 {
+ var file:File = File.applicationStorageDirectory.resolvePath(fileName);
+ var stream:FileStream = new FileStream();
+
+ if (!file.exists) {
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ errEvent.errorMessage = "File does not exist.";
+ errEvent.errorCode = 1;
+ _target.dispatchEvent(errEvent);
+ return;
+ }
+
+ var instream:DataInputStream = new DataInputStream(_target, file, stream);
+ stream.open(file, FileMode.READ);
+
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = instream;
+ _target.dispatchEvent(newEvent);
}
}
@@ -146,19 +190,65 @@ package org.apache.flex.storage.providers
stream.writeUTFBytes(text);
stream.close();
} catch(ioerror:IOError) {
- var ioEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var ioEvent:FileErrorEvent = new FileErrorEvent("ERROR");
ioEvent.errorMessage = "I/O Error";
+ ioEvent.errorCode = 2;
_target.dispatchEvent(ioEvent);
return;
} catch(secerror:SecurityError) {
- var secEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var secEvent:FileErrorEvent = new FileErrorEvent("ERROR");
secEvent.errorMessage = "Security Error";
+ secEvent.errorCode = 3;
_target.dispatchEvent(secEvent);
return;
}
- var newEvent:FileWriteEvent = new FileWriteEvent("COMPLETE");
+ var newEvent:FileEvent = new FileEvent("WRITE");
_target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
+ }
+ }
+
+ /**
+ * Opens a file for output streaming. Events are dispatched when the file is ready
+ * to receive output, after each write to the file, and when the file is closed.
+ *
+ * @param fileName The name of the file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openOutputDataStream( fileName:String ) : void
+ {
+ COMPILE::AS3 {
+ var file:File = File.applicationStorageDirectory.resolvePath(fileName);
+ var stream:FileStream = new FileStream();
+
+ var outstream:DataOutputStream = new DataOutputStream(_target, file, stream);
+ try {
+ stream.open(file, FileMode.WRITE);
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = outstream;
+ _target.dispatchEvent(newEvent);
+
+ } catch(ioerror:IOError) {
+ var ioEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ ioEvent.errorMessage = "I/O Error";
+ ioEvent.errorCode = 2;
+ _target.dispatchEvent(ioEvent);
+ return;
+
+ } catch(secerror:SecurityError) {
+ var secEvent:FileErrorEvent = new FileErrorEvent("ERROR");
+ secEvent.errorMessage = "Security Error";
+ secEvent.errorCode = 3;
+ _target.dispatchEvent(secEvent);
+ return;
+ }
}
}
}
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/IPermanentStorageProvider.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/IPermanentStorageProvider.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/IPermanentStorageProvider.as
index 023b1a7..f5a200d 100644
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/IPermanentStorageProvider.as
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/IPermanentStorageProvider.as
@@ -70,5 +70,33 @@ package org.apache.flex.storage.providers
* @productversion FlexJS 0.0
*/
function writeTextToDataFile( fileName:String, text:String ) : void;
+
+ /**
+ * Opens an output stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * write data to the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ function openOutputDataStream( fileName:String ) : void;
+
+ /**
+ * Opens an input stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * read data from the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ function openInputDataStream( fileName:String ) : void;
}
}
http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/dcf3b3b9/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/WebStorageProvider.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/WebStorageProvider.as b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/WebStorageProvider.as
index 557b591..df33c16 100644
--- a/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/WebStorageProvider.as
+++ b/frameworks/projects/Storage/src/main/flex/org/apache/flex/storage/providers/WebStorageProvider.as
@@ -20,14 +20,20 @@ package org.apache.flex.storage.providers
{
import org.apache.flex.events.EventDispatcher;
import org.apache.flex.events.IEventDispatcher;
-
- import org.apache.flex.storage.events.FileReadEvent;
- import org.apache.flex.storage.events.FileWriteEvent;
+
+ import org.apache.flex.storage.events.FileEvent;
+ import org.apache.flex.storage.events.FileErrorEvent;
+ import org.apache.flex.storage.file.DataInputStream;
+ import org.apache.flex.storage.file.DataOutputStream;
/**
* The WebStorageProvider class implements the IPermanentStorageProvider
* interface for saving files to a mobile device.
*
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
* @flexjsignorecoercion FileEntry
* @flexjsignorecoercion FileWriter
* @flexjsignorecoercion window
@@ -95,20 +101,63 @@ package org.apache.flex.storage.providers
fileEntry.file(function (file):void {
var reader:FileReader = new FileReader();
reader.onloadend = function (e):void {
- var newEvent:FileReadEvent = new FileReadEvent("COMPLETE");
+ var newEvent:FileEvent = new FileEvent("READ");
newEvent.data = this.result;
_target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
};
reader.readAsText(file);
}, function (e):void {
- var errEvent:FileReadEvent = new FileReadEvent("ERROR");
- errEvent.errorMessage = "Cannot open file for reading";
- _target.dispatchEvent(errEvent);
+ var err1Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err1Event.errorMessage = "Cannot open file for reading";
+ err1Event.errorCode = 2;
+ _target.dispatchEvent(err1Event);
});
}, function (e):void {
- var errEvent:FileReadEvent = new FileReadEvent("ERROR");
- errEvent.errorMessage = "File does not exist";
- _target.dispatchEvent(errEvent);
+ var err2Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err2Event.errorMessage = "File does not exist";
+ err2Event.errorCode = 1;
+ _target.dispatchEvent(err2Event);
+ });
+ }
+ }
+
+ /**
+ * Opens an input stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * read data from the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openInputDataStream( fileName:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["dataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(fullPath, function (fileEntry):void {
+ fileEntry.file(function (file):void {
+ var inputStream:DataInputStream = new DataInputStream(_target, file, new FileReader());
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = inputStream;
+ _target.dispatchEvent(newEvent);
+ }, function (e):void {
+ var err1Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err1Event.errorMessage = "Cannot open file for reading";
+ err1Event.errorCode = 2;
+ _target.dispatchEvent(err1Event);
+ });
+ }, function (e):void {
+ var err2Event:FileErrorEvent = new FileErrorEvent("ERROR");
+ err2Event.errorMessage = "File does not exist";
+ err2Event.errorCode = 1;
+ _target.dispatchEvent(err2Event);
});
}
}
@@ -136,34 +185,71 @@ package org.apache.flex.storage.providers
directoryEntry.getFile(fileName, { 'create': true }, function (fileEntry):void {
fileEntry.createWriter(function (fileWriter):void {
fileWriter.onwriteend = function (e):void {
- var newEvent:FileWriteEvent = new FileWriteEvent("COMPLETE");
+ var newEvent:FileEvent = new FileEvent("WRITE");
_target.dispatchEvent(newEvent);
+
+ var finEvent:FileEvent = new FileEvent("COMPLETE");
+ _target.dispatchEvent(finEvent);
};
fileWriter.onerror = function (e):void {
- var newEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var newEvent:FileErrorEvent = new FileErrorEvent("ERROR");
newEvent.errorMessage = "Failed to write the file.";
+ newEvent.errorCode = 3;
_target.dispatchEvent(newEvent);
};
var blob:Blob = new Blob([text], { type: 'text/plain' });
fileWriter.write(blob);
}, function(e):void {
- var errEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
errEvent.errorMessage = "Cannot open file for writing.";
+ errEvent.errorCode = 1;
_target.dispatchEvent(errEvent);
});
}, function(e):void {
- var errEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
errEvent.errorMessage = "Cannot create file.";
+ errEvent.errorCode = 4;
_target.dispatchEvent(errEvent);
});
}, function(e):void {
- var errEvent:FileWriteEvent = new FileWriteEvent("ERROR");
+ var errEvent:FileErrorEvent = new FileErrorEvent("ERROR");
errEvent.errorMessage = "Cannot create file.";
+ errEvent.errorCode = 4;
_target.dispatchEvent(errEvent);
});
}
}
+
+ /**
+ * Opens an output stream into a file in the data storage directory. A Ready
+ * event is dispatched when the stream has been opened. Use the stream to
+ * write data to the file.
+ *
+ * @param fileName The name of file.
+ *
+ * @langversion 3.0
+ * @playerversion Flash 10.2
+ * @playerversion AIR 2.6
+ * @productversion FlexJS 0.0
+ */
+ public function openOutputDataStream( fileName:String ) : void
+ {
+ COMPILE::JS {
+ var fullPath:String = String(cordova["file"]["dataDirectory"]) + fileName;
+
+ window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry):void {
+ directoryEntry.getFile(fileName, { 'create': true }, function (fileEntry):void {
+ fileEntry.createWriter(function (fileWriter):void {
+ var outputStream:DataOutputStream = new DataOutputStream(_target, fileEntry, fileWriter);
+ var newEvent:FileEvent = new FileEvent("READY");
+ newEvent.stream = outputStream;
+ _target.dispatchEvent(newEvent);
+ });
+ });
+ });
+ }
+ }
}
}