summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-08-25 06:59:43 (EDT)
committerEike Stepper2007-08-25 06:59:43 (EDT)
commitdebd33662d508aa7a5136fc9e651c3ac8175a1e8 (patch)
treeb3ba7851af0bbbdf8a0720acde354de2a53dc2de
parentd2a5fd4bfbef792c4aba6c8d0311ebef2f8036d9 (diff)
downloadcdo-debd33662d508aa7a5136fc9e651c3ac8175a1e8.zip
cdo-debd33662d508aa7a5136fc9e651c3ac8175a1e8.tar.gz
cdo-debd33662d508aa7a5136fc9e651c3ac8175a1e8.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/SortedFileMapTest.java98
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java8
2 files changed, 105 insertions, 1 deletions
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/SortedFileMapTest.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/SortedFileMapTest.java
new file mode 100644
index 0000000..08b9151
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/SortedFileMapTest.java
@@ -0,0 +1,98 @@
+/***************************************************************************
+ * Copyright (c) 2004-2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.tests;
+
+import org.eclipse.net4j.util.io.ExtendedIOUtil;
+import org.eclipse.net4j.util.io.SortedFileMap;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class SortedFileMapTest extends AbstractOMTest
+{
+ public void testMap() throws Exception
+ {
+ File file = new File("SortedFileMapTest.dat");
+ SortedFileMap<Integer, String> map = new TestMap(file);
+
+ try
+ {
+ map.put(1, "Eike");
+ map.put(3, "René");
+ map.put(7, "Birte");
+ map.put(9, "Mama");
+ map.put(11, "Papa");
+ map.put(15, "Julchen");
+ }
+ finally
+ {
+ map.close();
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ public static final class TestMap extends SortedFileMap<Integer, String>
+ {
+ public TestMap(File file)
+ {
+ super(file, "rw");
+ }
+
+ @Override
+ public int getKeySize()
+ {
+ return 4;
+ }
+
+ @Override
+ protected Integer readKey(DataInput in) throws IOException
+ {
+ return in.readInt();
+ }
+
+ @Override
+ protected void writeKey(DataOutput out, Integer key) throws IOException
+ {
+ out.writeInt(key);
+ }
+
+ @Override
+ public int getValueSize()
+ {
+ return 20;
+ }
+
+ @Override
+ protected String readValue(DataInput in) throws IOException
+ {
+ return ExtendedIOUtil.readString(in);
+ }
+
+ @Override
+ protected void writeValue(DataOutput out, String value) throws IOException
+ {
+ byte[] bytes = value.getBytes();
+ if (bytes.length + 4 > getValueSize())
+ {
+ throw new IllegalArgumentException("Value size of " + getValueSize() + " exceeded: " + value);
+ }
+
+ ExtendedIOUtil.writeByteArray(out, bytes);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java
index 1e3bf03..13dc5eb 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java
@@ -10,6 +10,7 @@
**************************************************************************/
package org.eclipse.net4j.util.io;
+import java.io.Closeable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
@@ -19,7 +20,7 @@ import java.io.RandomAccessFile;
/**
* @author Eike Stepper
*/
-public abstract class SortedFileMap<K extends Comparable, V>
+public abstract class SortedFileMap<K extends Comparable, V> implements Closeable
{
private File file;
@@ -47,6 +48,11 @@ public abstract class SortedFileMap<K extends Comparable, V>
}
}
+ public void close() throws IOException
+ {
+ IOUtil.close(randomAccessFile);
+ }
+
public File getFile()
{
return file;