Revision: 9900
Author: sco...@google.com
Date: Fri Mar 25 16:15:23 2011
Log: DiskCacheToken wrapper type for easier serialization.
http://gwt-code-reviews.appspot.com/1391802/
Review by: zun...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=9900
Added:
/trunk/dev/core/src/com/google/gwt/dev/util/DiskCacheToken.java
/trunk/dev/core/test/com/google/gwt/dev/util/DiskCacheTokenTest.java
Modified:
/trunk/dev/core/src/com/google/gwt/dev/util/DiskCache.java
=======================================
--- /dev/null
+++ /trunk/dev/core/src/com/google/gwt/dev/util/DiskCacheToken.java Fri Mar
25 16:15:23 2011
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2011 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.dev.util;
+
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+
+/**
+ * Wrapper around a {@link DiskCache} token that allows easy serialization.
+ */
+public class DiskCacheToken implements Serializable {
+
+ private transient DiskCache diskCache;
+ private transient long token;
+
+ /**
+ * Create a wrapper for a token associated with {@link
DiskCache#INSTANCE}.
+ */
+ public DiskCacheToken(long token) {
+ this(DiskCache.INSTANCE, token);
+ }
+
+ /**
+ * Create a wrapper for a token associated with the given diskCache.
+ */
+ DiskCacheToken(DiskCache diskCache, long token) {
+ this.diskCache = diskCache;
+ this.token = token;
+ }
+
+ /**
+ * Retrieve the underlying bytes.
+ *
+ * @return the bytes that were written
+ */
+ public synchronized byte[] readByteArray() {
+ return diskCache.readByteArray(token);
+ }
+
+ /**
+ * Deserialize the underlying bytes as an object.
+ *
+ * @param <T> the type of the object to deserialize
+ * @param type the type of the object to deserialize
+ * @return the deserialized object
+ */
+ public <T> T readObject(Class<T> type) {
+ return diskCache.readObject(token, type);
+ }
+
+ /**
+ * Read the underlying bytes as a String.
+ *
+ * @return the String that was written
+ */
+ public String readString() {
+ return diskCache.readString(token);
+ }
+
+ /**
+ * Writes the underlying bytes into the specified output stream.
+ *
+ * @param out the stream to write into
+ */
+ public synchronized void transferToStream(OutputStream out) {
+ diskCache.transferToStream(token, out);
+ }
+
+ private void readObject(ObjectInputStream inputStream) {
+ diskCache = DiskCache.INSTANCE;
+ token = diskCache.transferFromStream(inputStream);
+ }
+
+ private void writeObject(ObjectOutputStream outputStream) {
+ diskCache.transferToStream(token, outputStream);
+ }
+}
=======================================
--- /dev/null
+++ /trunk/dev/core/test/com/google/gwt/dev/util/DiskCacheTokenTest.java
Fri Mar 25 16:15:23 2011
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 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.dev.util;
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+
+/**
+ * Tests {@link DiskCacheToken}.
+ */
+public class DiskCacheTokenTest extends TestCase {
+ private final DiskCache diskCache = new DiskCache();
+
+ public void testSerialization() {
+ byte[] buf = new byte[]{1, 5, 9, 7, 3, 4, 2};
+ long t = diskCache.writeByteArray(buf);
+ DiskCacheToken token1 = new DiskCacheToken(diskCache, t);
+
+ long t2 = diskCache.writeObject(token1);
+ DiskCacheToken token2 = new DiskCacheToken(diskCache, t2);
+
+ DiskCacheToken token1again = token2.readObject(DiskCacheToken.class);
+ byte[] buf2 = token1again.readByteArray();
+ assertTrue("Values were not equals", Arrays.equals(buf, buf2));
+ }
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/util/DiskCache.java Wed Feb 16
11:06:56 2011
+++ /trunk/dev/core/src/com/google/gwt/dev/util/DiskCache.java Fri Mar 25
16:15:23 2011
@@ -70,7 +70,7 @@
private boolean atEnd = true;
private RandomAccessFile file;
- protected DiskCache() {
+ DiskCache() {
try {
File temp = File.createTempFile("gwt", "byte-cache");
temp.deleteOnExit();
@@ -87,10 +87,9 @@
}
/**
- * Read some bytes off disk.
+ * Retrieve the underlying bytes.
*
- * @param token a handle previously returned from
- * {@link #writeByteArray(byte[])}
+ * @param token a previously returned token
* @return the bytes that were written
*/
public synchronized byte[] readByteArray(long token) {
@@ -106,24 +105,30 @@
}
}
+ /**
+ * Deserialize the underlying bytes as an object.
+ *
+ * @param <T> the type of the object to deserialize
+ * @param token a previously returned token
+ * @param type the type of the object to deserialize
+ * @return the deserialized object
+ */
public <T> T readObject(long token, Class<T> type) {
try {
byte[] bytes = readByteArray(token);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
return Util.readStreamAsObject(in, type);
} catch (ClassNotFoundException e) {
- throw new RuntimeException(
- "Unexpected exception deserializing from disk cache", e);
+ throw new RuntimeException("Unexpected exception deserializing from
disk cache", e);
} catch (IOException e) {
- throw new RuntimeException(
- "Unexpected exception deserializing from disk cache", e);
+ throw new RuntimeException("Unexpected exception deserializing from
disk cache", e);
}
}
/**
- * Read a String from disk.
+ * Read the underlying bytes as a String.
*
- * @param token a handle previously returned from {@link
#writeString(String)}
+ * @param token a previously returned token
* @return the String that was written
*/
public String readString(long token) {
@@ -133,7 +138,7 @@
/**
* Write the rest of the data in an input stream to disk.
*
- * @return a handle to retrieve it later
+ * @return a token to retrieve the data later
*/
public synchronized long transferFromStream(InputStream in) {
byte[] buf = Util.takeThreadLocalBuf();
@@ -165,8 +170,10 @@
}
/**
- * Reads bytes of data back from disk and writes them into the specified
- * output stream.
+ * Writes the underlying bytes into the specified output stream.
+ *
+ * @param token a previously returned token
+ * @param out the stream to write into
*/
public synchronized void transferToStream(long token, OutputStream out) {
byte[] buf = Util.takeThreadLocalBuf();
@@ -195,7 +202,7 @@
/**
* Write a byte array to disk.
*
- * @return a handle to retrieve it later
+ * @return a token to retrieve the data later
*/
public synchronized long writeByteArray(byte[] bytes) {
try {
@@ -208,21 +215,25 @@
}
}
+ /**
+ * Serialize an Object to disk.
+ *
+ * @return a token to retrieve the data later
+ */
public long writeObject(Object object) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Util.writeObjectToStream(out, object);
return writeByteArray(out.toByteArray());
} catch (IOException e) {
- throw new RuntimeException("Unexpected IOException on in-memory
stream",
- e);
+ throw new RuntimeException("Unexpected IOException on in-memory
stream", e);
}
}
/**
- * Write a String to disk.
+ * Write a String to disk as bytes.
*
- * @return a handle to retrieve it later
+ * @return a token to retrieve the data later
*/
public long writeString(String str) {
return writeByteArray(Util.getBytes(str));
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors