From ec849385918f630aa41f4a0d891c951c29a8de8e Mon Sep 17 00:00:00 2001 From: Justin Shapcott Date: Sat, 25 May 2013 09:36:24 -0700 Subject: [PATCH] Use standard GWT Storage API --- .../com/badlogic/gdx/backends/gwt/GwtFiles.java | 6 +- .../badlogic/gdx/backends/gwt/GwtPreferences.java | 15 ++-- .../google/gwt/corp/localstorage/LocalStorage.java | 84 ---------------------- .../gdx/backends/gwt/emu/java/io/File.java | 83 ++++++++------------- .../backends/gwt/emu/java/io/RandomAccessFile.java | 15 ++-- 5 files changed, 48 insertions(+), 155 deletions(-) delete mode 100644 backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/com/google/gwt/corp/localstorage/LocalStorage.java diff --git a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtFiles.java b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtFiles.java index 24981dc98..355624e19 100644 --- a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtFiles.java +++ b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtFiles.java @@ -20,10 +20,14 @@ import com.badlogic.gdx.Files; import com.badlogic.gdx.backends.gwt.preloader.Preloader; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.GdxRuntimeException; +import com.google.gwt.storage.client.Storage; public class GwtFiles implements Files { + + public static final Storage LocalStorage = Storage.getLocalStorageIfSupported(); + final Preloader preloader; - + public GwtFiles (Preloader preloader) { this.preloader = preloader; } diff --git a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtPreferences.java b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtPreferences.java index 4345b2112..d1146d278 100644 --- a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtPreferences.java +++ b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/GwtPreferences.java @@ -22,7 +22,6 @@ import java.util.Map; import com.badlogic.gdx.Preferences; import com.badlogic.gdx.utils.GdxRuntimeException; import com.badlogic.gdx.utils.ObjectMap; -import com.google.gwt.corp.localstorage.LocalStorage; public class GwtPreferences implements Preferences { final String prefix; @@ -32,10 +31,10 @@ public class GwtPreferences implements Preferences { this.prefix = prefix + ":"; int prefixLength = this.prefix.length(); try { - for (int i = 0; i < LocalStorage.length(); i++) { - String key = LocalStorage.key(i); + for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) { + String key = GwtFiles.LocalStorage.key(i); if (key.startsWith(prefix)) { - String value = LocalStorage.getItem(key); + String value = GwtFiles.LocalStorage.getItem(key); values.put(key.substring(prefixLength, key.length() - 1), toObject(key, value)); } } @@ -64,16 +63,16 @@ public class GwtPreferences implements Preferences { public void flush () { try { // remove all old values - for (int i = 0; i < LocalStorage.length(); i++) { - String key = LocalStorage.key(i); - if (key.startsWith(prefix)) LocalStorage.removeItem(key); + for (int i = 0; i < GwtFiles.LocalStorage.getLength(); i++) { + String key = GwtFiles.LocalStorage.key(i); + if (key.startsWith(prefix)) GwtFiles.LocalStorage.removeItem(key); } // push new values to LocalStorage for (String key : values.keys()) { String storageKey = toStorageKey(key, values.get(key)); String storageValue = "" + values.get(key).toString(); - LocalStorage.setItem(storageKey, storageValue); + GwtFiles.LocalStorage.setItem(storageKey, storageValue); } } catch (Exception e) { diff --git a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/com/google/gwt/corp/localstorage/LocalStorage.java b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/com/google/gwt/corp/localstorage/LocalStorage.java deleted file mode 100644 index 39e145cb7..000000000 --- a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/com/google/gwt/corp/localstorage/LocalStorage.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2010 Google Inc. - * - * Licensed 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 com.google.gwt.corp.localstorage; - -import java.io.IOException; - -import com.google.gwt.core.client.JavaScriptException; - -public class LocalStorage { - - public static String getItem (String key) throws IOException { - try { - return getItemImpl(key); - } catch (JavaScriptException e) { - throw new IOException("" + e); - } - } - - public static String key (int index) throws IOException { - try { - return keyImpl(index); - } catch (JavaScriptException e) { - throw new IOException("" + e); - } - } - - public static int length () throws IOException { - try { - return lengthImpl(); - } catch (JavaScriptException e) { - throw new IOException("" + e); - } - } - - public static void removeItem (String key) throws IOException { - try { - removeItemImpl(key); - } catch (JavaScriptException e) { - throw new IOException("" + e); - } - } - - public static void setItem (String key, String value) throws IOException { - try { - setItemImpl(key, value); - } catch (JavaScriptException e) { - throw new IOException("" + e); - } - } - - private native static String getItemImpl (String key) /*-{ - return $wnd.localStorage.getItem(key); - }-*/; - - private native static String keyImpl (int index) /*-{ - return $wnd.localStorage.key(index); - }-*/; - - public native static int lengthImpl () /*-{ - return $wnd.localStorage.length; - }-*/; - - public native static void setItemImpl (String key, String value) /*-{ - $wnd.localStorage.setItem(key, value); - }-*/; - - public native static void removeItemImpl (String key) /*-{ - $wnd.localStorage.removeItem(key); - }-*/; -} diff --git a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/File.java b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/File.java index 1f119094d..5da8dd50a 100644 --- a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/File.java +++ b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/File.java @@ -18,7 +18,7 @@ package java.io; import java.util.ArrayList; -import com.google.gwt.corp.localstorage.LocalStorage; +import com.google.gwt.storage.client.Storage; /** LocalStorage based File implementation for GWT. Should probably have used Harmony as a starting point instead of writing this * from scratch. @@ -36,6 +36,8 @@ public class File { public static final String pathSeparator = "" + pathSeparatorChar; + public static final Storage LocalStorage = Storage.getLocalStorageIfSupported(); + File parent; String name; boolean absolute; @@ -159,29 +161,17 @@ public class File { } public boolean exists () { - try { - return LocalStorage.getItem(getCanonicalPath()) != null; - } catch (IOException e) { - return false; - } + return LocalStorage.getItem(getCanonicalPath()) != null; } public boolean isDirectory () { - try { - String s = LocalStorage.getItem(getCanonicalPath()); - return s != null && s.startsWith("{"); - } catch (IOException e) { - return false; - } + String s = LocalStorage.getItem(getCanonicalPath()); + return s != null && s.startsWith("{"); } public boolean isFile () { - try { - String s = LocalStorage.getItem(getCanonicalPath()); - return s != null && !s.startsWith("{"); - } catch (IOException e) { - return false; - } + String s = LocalStorage.getItem(getCanonicalPath()); + return s != null && !s.startsWith("{"); } public boolean isHidden () { @@ -215,15 +205,11 @@ public class File { } public boolean delete () { - try { - if (!exists()) { - return false; - } - LocalStorage.removeItem(getCanonicalPath()); - return true; - } catch (IOException e) { + if (!exists()) { return false; } + LocalStorage.removeItem(getCanonicalPath()); + return true; } public void deleteOnExit () { @@ -244,25 +230,20 @@ public class File { public File[] listFiles (FilenameFilter filter) { ArrayList files = new ArrayList(); - try { - String prefix = getCanonicalPath(); - if (!prefix.endsWith(separator)) { - prefix += separatorChar; - } - - int cut = prefix.length(); - int cnt = LocalStorage.length(); - for (int i = 0; i < cnt; i++) { - String key = LocalStorage.key(i); - if (key.startsWith(prefix) && key.indexOf(separatorChar, cut) == -1) { - String name = key.substring(cut); - if (filter == null || filter.accept(this, name)) { - files.add(new File(this, name)); - } + String prefix = getCanonicalPath(); + if (!prefix.endsWith(separator)) { + prefix += separatorChar; + } + int cut = prefix.length(); + int cnt = LocalStorage.getLength(); + for (int i = 0; i < cnt; i++) { + String key = LocalStorage.key(i); + if (key.startsWith(prefix) && key.indexOf(separatorChar, cut) == -1) { + String name = key.substring(cut); + if (filter == null || filter.accept(this, name)) { + files.add(new File(this, name)); } } - } catch (IOException e) { - System.err.println("lisFiles() exception: " + e); } return files.toArray(new File[files.size()]); } @@ -272,19 +253,15 @@ public class File { */ public boolean mkdir () { - try { - if (parent != null && !parent.exists()) { - return false; - } - if (exists()) { - return false; - } - // We may want to make this a JS map - LocalStorage.setItem(getCanonicalPath(), "{}"); - return true; - } catch (IOException e) { + if (parent != null && !parent.exists()) { return false; } + if (exists()) { + return false; + } + // We may want to make this a JS map + LocalStorage.setItem(getCanonicalPath(), "{}"); + return true; } public boolean mkdirs () { diff --git a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/RandomAccessFile.java b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/RandomAccessFile.java index 9e1239aa8..24dcad113 100644 --- a/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/RandomAccessFile.java +++ b/backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/io/RandomAccessFile.java @@ -16,12 +16,12 @@ package java.io; -import com.google.gwt.corp.localstorage.LocalStorage; +import com.google.gwt.storage.client.Storage; /** Saves binary data to the local storage; currently using hex encoding. The string is prefixed with "hex:" * @author haustein */ public class RandomAccessFile /* implements DataOutput, DataInput, Closeable */{ - + /* * public final FileDescriptor getFD() throws IOException { } public final FileChannel getChannel() { } */ @@ -33,6 +33,7 @@ public class RandomAccessFile /* implements DataOutput, DataInput, Closeable */{ StringBuilder newData; int pos; int len; + DataInputStream dis = new DataInputStream(new RafInputStream()); DataOutputStream dos = new DataOutputStream(new RafOutputStream()); @@ -49,12 +50,8 @@ public class RandomAccessFile /* implements DataOutput, DataInput, Closeable */{ } writeable = mode.equals("rw"); if (file.exists()) { - try { - data = atob(LocalStorage.getItem(name)); - len = data.length(); - } catch (IOException e) { - throw new FileNotFoundException("" + e); - } + data = atob(File.LocalStorage.getItem(name)); + len = data.length(); } else if (writeable) { data = ""; dirty = true; @@ -176,7 +173,7 @@ public class RandomAccessFile /* implements DataOutput, DataInput, Closeable */{ return; } consolidate(); - LocalStorage.setItem(name, btoa(data)); + File.LocalStorage.setItem(name, btoa(data)); dirty = false; } -- 2.11.0