Skip to content
This repository was archived by the owner on Apr 8, 2019. It is now read-only.

Commit 9e32c6b

Browse files
author
aki
committed
add FileIOUtils
1 parent f880718 commit 9e32c6b

File tree

4 files changed

+304
-10
lines changed

4 files changed

+304
-10
lines changed
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
package com.akjava.gwt.html5.client.file;
2+
3+
import com.akjava.gwt.html5.client.file.RequestFileSystem.FileSystemCallback;
4+
import com.akjava.gwt.html5.client.file.callback.FileEntryCallback;
5+
import com.akjava.gwt.html5.client.file.callback.FileErrorCallback;
6+
import com.akjava.gwt.html5.client.file.callback.FileWriterCallback;
7+
import com.akjava.gwt.html5.client.file.callback.ProgressEventCallback;
8+
import com.akjava.gwt.html5.client.file.webkit.FileCallback;
9+
import com.akjava.gwt.html5.client.file.webkit.FileEntry;
10+
import com.akjava.gwt.html5.client.file.webkit.WebkitStorageInfo;
11+
import com.akjava.gwt.html5.client.file.webkit.WebkitStorageInfo.RequestQuotaCallback;
12+
import com.akjava.gwt.html5.client.file.webkit.WebkitStorageInfo.StorageInfoUsageCallback;
13+
import com.akjava.gwt.html5test.client.HTML5Test;
14+
15+
public class FileIOUtils {
16+
private FileIOUtils(){}
17+
18+
19+
public static void readFileAsString(final String path,boolean persitent,final ReadStringCallback callback){
20+
final String encoding="UTF8";
21+
int type=RequestFileSystem.TEMPORARY;
22+
if(persitent){
23+
type=RequestFileSystem.PERSISTENT;
24+
}
25+
RequestFileSystem.requestFileSystem(type,0,new FileSystemCallback() {
26+
27+
@Override
28+
public void fileSystemCallback(FileSystem fileSystem) {
29+
//if already exists call code: 9
30+
fileSystem.getRoot().getFile(path, false, false, new FileEntryCallback(){
31+
32+
@Override
33+
public void fileEntryCallback(final FileEntry fileEntry) {
34+
35+
fileEntry.file(new FileCallback(){
36+
@Override
37+
public void callback(File file) {
38+
final FileReader reader=FileReader.createFileReader();
39+
reader.setOnLoad(new FileHandler() {
40+
@Override
41+
public void onLoad() {
42+
callback.onReadString(reader.getResultAsString(),fileEntry);
43+
}
44+
});
45+
reader.readAsText(file, encoding);
46+
}});
47+
48+
49+
}}, new MessageErrorCallback("getFile",callback));
50+
}
51+
}
52+
,new MessageErrorCallback("requestFileSystem",callback)
53+
);
54+
}
55+
56+
public static void writeFile(final String path,boolean persitent,final String text,final WriteCallback callback,final boolean append){
57+
final String mimeType="text/plain;charset=UTF-8";
58+
int type=RequestFileSystem.TEMPORARY;
59+
if(persitent){
60+
type=RequestFileSystem.PERSISTENT;
61+
}
62+
RequestFileSystem.requestFileSystem(type,0,new FileSystemCallback() {
63+
@Override
64+
public void fileSystemCallback(FileSystem fileSystem) {
65+
66+
final Blob blob=Blob.createBlob(text, mimeType);
67+
68+
fileSystem.getRoot().getFile(path,true,false, new FileEntryCallback(){
69+
70+
@Override
71+
public void fileEntryCallback(final FileEntry file) {
72+
73+
file.createWriter(new FileWriterCallback() {
74+
75+
@Override
76+
public void createWriterCallback(final FileWriter fileWriter) {
77+
78+
79+
fileWriter.setOnError(new ProgressEventCallback() {
80+
@Override
81+
public void progressEventCallback(ProgressEvent progressEvent) {
82+
callback.onError("onWrite",file);
83+
}
84+
});//maybe should remove it
85+
86+
87+
if(fileWriter.length()>0 &&!append){
88+
fileWriter.setOnWriteEnd(new ProgressEventCallback() {
89+
@Override
90+
public void progressEventCallback(ProgressEvent progressEvent) {
91+
fileWriter.setOnWriteEnd(new ProgressEventCallback() {
92+
@Override
93+
public void progressEventCallback(ProgressEvent progressEvent) {
94+
callback.onWriteEnd(file);
95+
96+
}
97+
});
98+
fileWriter.write(blob);
99+
}
100+
});
101+
HTML5Test.log("truncated");
102+
103+
104+
105+
fileWriter.truncate(0);//TODO blob support length
106+
}else{
107+
HTML5Test.log("empty ust write");
108+
fileWriter.setOnWriteEnd(new ProgressEventCallback() {
109+
@Override
110+
public void progressEventCallback(ProgressEvent progressEvent) {
111+
112+
callback.onWriteEnd(file);
113+
}
114+
});
115+
if(fileWriter.length()>0){
116+
fileWriter.seek(fileWriter.length());
117+
}
118+
fileWriter.write(blob);
119+
}
120+
121+
}
122+
}, new MessageErrorCallback("getFileWriter",callback));
123+
124+
}}, new MessageErrorCallback("getFile",callback));
125+
}
126+
}
127+
,new MessageErrorCallback("requestFileSystem",callback)
128+
);
129+
}
130+
131+
private static class MessageErrorCallback implements FileErrorCallback{
132+
private String message;
133+
private ErrorCallback callback;
134+
135+
public MessageErrorCallback(String message,ErrorCallback callback){
136+
this.message=message;
137+
this.callback=callback;
138+
}
139+
140+
@Override
141+
public void fileErrorCallback(FileError fileError) {
142+
callback.onError(message, fileError);
143+
}
144+
}
145+
146+
147+
148+
public static void getFileQuataAndUsage(boolean persistent,FileQuataAndUsageListener listener){
149+
WebkitStorageInfo.queryUsageAndQuota(persistent?RequestFileSystem.PERSISTENT:RequestFileSystem.TEMPORARY, listener);
150+
}
151+
152+
153+
154+
public static void getFileSystem(boolean persitent,final GetFileSystemListener listener){
155+
int type=RequestFileSystem.TEMPORARY;
156+
if(persitent){
157+
type=RequestFileSystem.PERSISTENT;
158+
}
159+
WebkitStorageInfo.requestQuota(type, 0, new RequestQuotaCallback(){
160+
@Override
161+
public void requestQuotaCallback(final double grantedBytes) {
162+
163+
if(grantedBytes==0){
164+
listener.onError("zero quota", null);
165+
return;
166+
}
167+
168+
RequestFileSystem.requestFileSystem(RequestFileSystem.PERSISTENT,grantedBytes,new FileSystemCallback(){
169+
@Override
170+
public void fileSystemCallback(FileSystem fileSystem) {
171+
listener.onGetFileSystem(fileSystem);
172+
}}
173+
174+
,new FileErrorCallback() {
175+
@Override
176+
public void fileErrorCallback(FileError fileError) {
177+
listener.onError("onRequestFileSystem", fileError);
178+
}
179+
}
180+
);
181+
182+
183+
}
184+
});
185+
}
186+
187+
public static void requestPersitentFileQuota(double size,final RequestPersitentFileQuotaListener listener){
188+
189+
WebkitStorageInfo.requestQuota(RequestFileSystem.PERSISTENT, size, new RequestQuotaCallback(){
190+
@Override
191+
public void requestQuotaCallback(final double grantedBytes) {
192+
193+
if(grantedBytes==0){
194+
listener.onError("zero quota", null);
195+
return;
196+
}
197+
198+
RequestFileSystem.requestFileSystem(RequestFileSystem.PERSISTENT,grantedBytes,new FileSystemCallback(){
199+
@Override
200+
public void fileSystemCallback(FileSystem fileSystem) {
201+
listener.onAccepted(fileSystem, grantedBytes);
202+
}}
203+
204+
,new FileErrorCallback() {
205+
@Override
206+
public void fileErrorCallback(FileError fileError) {
207+
listener.onError("onRequestFileSystem", fileError);
208+
}
209+
}
210+
);
211+
212+
213+
}
214+
});
215+
}
216+
217+
public interface RequestPersitentFileQuotaListener extends ErrorCallback{
218+
public void onAccepted(FileSystem fileSystem,double acceptedSize);
219+
}
220+
221+
public interface FileQuataAndUsageListener extends StorageInfoUsageCallback{
222+
223+
}
224+
225+
226+
227+
public interface GetFileSystemListener extends ErrorCallback{
228+
public void onGetFileSystem(FileSystem fileSystem);
229+
}
230+
231+
public interface ErrorCallback{
232+
public void onError(String message,Object option);
233+
}
234+
235+
public interface ReadStringCallback extends ErrorCallback{
236+
public void onReadString(String text,FileEntry file);
237+
}
238+
239+
public interface WriteCallback extends ErrorCallback{
240+
public void onWriteEnd(FileEntry file);
241+
}
242+
}

src/com/akjava/gwt/html5/client/file/RequestFileSystem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class RequestFileSystem {
1212
public static final int TEMPORARY=0;
1313
public static final int PERSISTENT=1;
1414

15-
public static final native void requestFileSystem(int type,int size,FileSystemCallback successCallback,FileErrorCallback errorCallback)/*-{
15+
public static final native void requestFileSystem(int type,double size,FileSystemCallback successCallback,FileErrorCallback errorCallback)/*-{
1616
1717
$wnd.requestFileSystem = $wnd.requestFileSystem || $wnd.webkitRequestFileSystem;
1818
console.log($wnd.requestFileSystem);

src/com/akjava/gwt/html5/client/file/webkit/WebkitStorageInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ public static final native void test()/*-{
1717

1818

1919

20-
public static final native void requestQuota(int type,int size,RequestQuotaCallback requestQuotaCallback)/*-{
20+
public static final native void requestQuota(int type,double size,RequestQuotaCallback requestQuotaCallback)/*-{
2121
2222
2323
var quotaCallback=function(callback){
24-
requestQuotaCallback.@com.akjava.gwt.html5.client.file.webkit.WebkitStorageInfo$RequestQuotaCallback::requestQuotaCallback(I)(callback);
24+
requestQuotaCallback.@com.akjava.gwt.html5.client.file.webkit.WebkitStorageInfo$RequestQuotaCallback::requestQuotaCallback(D)(callback);
2525
};
2626
2727
@@ -43,7 +43,7 @@ public static final native void queryUsageAndQuota(int type,StorageInfoUsageCal
4343
}-*/;
4444

4545
public static interface RequestQuotaCallback{
46-
public void requestQuotaCallback(int grantedBytes);
46+
public void requestQuotaCallback(double grantedBytes);
4747
}
4848

4949
public static interface StorageInfoUsageCallback {

src/com/akjava/gwt/html5test/client/FileSystemTest.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
import com.akjava.gwt.html5.client.file.File;
1212
import com.akjava.gwt.html5.client.file.FileError;
1313
import com.akjava.gwt.html5.client.file.FileHandler;
14+
import com.akjava.gwt.html5.client.file.FileIOUtils;
15+
import com.akjava.gwt.html5.client.file.FileIOUtils.ReadStringCallback;
16+
import com.akjava.gwt.html5.client.file.FileIOUtils.WriteCallback;
1417
import com.akjava.gwt.html5.client.file.FileReader;
1518
import com.akjava.gwt.html5.client.file.FileSystem;
1619
import com.akjava.gwt.html5.client.file.FileUploadForm;
@@ -62,6 +65,56 @@ public FileSystemTest(){
6265

6366
createListButton(this);
6467

68+
69+
70+
71+
createFileIOButton(this);
72+
}
73+
74+
private void createFileIOButton(Panel panel){
75+
HorizontalPanel requestButtons=new HorizontalPanel();
76+
panel.add(requestButtons);
77+
78+
Button write=new Button("write",new ClickHandler() {
79+
@Override
80+
public void onClick(ClickEvent event) {
81+
FileIOUtils.writeFile("dir/a.txt", true, "fileio", new WriteCallback() {
82+
83+
@Override
84+
public void onError(String message, Object option) {
85+
HTML5Test.log("error:"+message);
86+
}
87+
88+
@Override
89+
public void onWriteEnd(FileEntry file) {
90+
HTML5Test.log("write done:"+file.getFullPath());
91+
}
92+
}, false);
93+
}
94+
});
95+
requestButtons.add(write);
96+
97+
Button read=new Button("read",new ClickHandler() {
98+
@Override
99+
public void onClick(ClickEvent event) {
100+
FileIOUtils.readFileAsString("dir/a.txt", true, new ReadStringCallback() {
101+
102+
@Override
103+
public void onError(String message, Object option) {
104+
HTML5Test.log("error:"+message);
105+
}
106+
107+
@Override
108+
public void onReadString(String text, FileEntry file) {
109+
HTML5Test.log("read done:"+file.getFullPath());
110+
previewTextArea.setText(text);
111+
}
112+
113+
114+
});
115+
}
116+
});
117+
requestButtons.add(read);
65118
}
66119

67120
private void createSimpleButton(Panel panel){
@@ -87,7 +140,6 @@ public void onClick(ClickEvent event) {
87140
@Override
88141
public void fileSystemCallback(FileSystem fileSystem) {
89142
label.setText("[file]"+fileSystem.getName());
90-
HTML5Test.log(fileSystem);
91143

92144
}
93145
}
@@ -162,15 +214,15 @@ public void onClick(ClickEvent event) {
162214

163215
WebkitStorageInfo.requestQuota(type, 10, new RequestQuotaCallback() {
164216
@Override
165-
public void requestQuotaCallback(int grantedBytes) {
166-
final int size=grantedBytes;
217+
public void requestQuotaCallback(double grantedBytes) {
218+
final long size=(long) grantedBytes;
167219

168220
RequestFileSystem.requestFileSystem(type,size,new FileSystemCallback() {
169221

170222
@Override
171223
public void fileSystemCallback(FileSystem fileSystem) {
172224
label.setText("[file]"+fileSystem.getName()+" size="+size);
173-
HTML5Test.log(fileSystem);
225+
174226
}
175227
}
176228
,
@@ -398,7 +450,7 @@ public void fileErrorCallback(FileError fileError) {
398450
public void onSelectionChange(SelectionChangeEvent event) {
399451

400452
FileEntry fileEntry=selectionModel.getSelectedObject();
401-
HTML5Test.log(fileEntry.getFullPath());
453+
HTML5Test.log("selection-changed:"+fileEntry.getFullPath());
402454

403455
currentSelectionFileEntry=fileEntry;
404456

@@ -449,7 +501,7 @@ public void callback(JsArray<FileEntry> entries) {
449501

450502
}
451503

452-
504+
updateCellList();
453505
}
454506
});
455507

0 commit comments

Comments
 (0)