summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-08-25 06:50:37 (EDT)
committerEike Stepper2007-08-25 06:50:37 (EDT)
commitd2a5fd4bfbef792c4aba6c8d0311ebef2f8036d9 (patch)
treea6dfa78fc3f456731680a18c55403ba12271b598
parent364c16ae50b7c41c02d2b69814d63cc05fc23aea (diff)
downloadcdo-d2a5fd4bfbef792c4aba6c8d0311ebef2f8036d9.zip
cdo-d2a5fd4bfbef792c4aba6c8d0311ebef2f8036d9.tar.gz
cdo-d2a5fd4bfbef792c4aba6c8d0311ebef2f8036d9.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInput.java2
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInputStream.java32
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutput.java2
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutputStream.java19
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java83
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java167
6 files changed, 254 insertions, 51 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInput.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInput.java
index 9761694..75b5dcc 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInput.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInput.java
@@ -21,6 +21,4 @@ public interface ExtendedDataInput extends DataInput
public byte[] readByteArray() throws IOException;
public String readString() throws IOException;
-
- public Object readObject() throws IOException, ClassNotFoundException;
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInputStream.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInputStream.java
index e8d7df8..7e79f64 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInputStream.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataInputStream.java
@@ -27,40 +27,12 @@ public class ExtendedDataInputStream extends DataInputStream implements Extended
public byte[] readByteArray() throws IOException
{
- int length = readInt();
- if (length < 0)
- {
- return null;
- }
-
- byte[] b;
- try
- {
- b = new byte[length];
- }
- catch (Throwable t)
- {
- throw new IOException("Unable to allocate " + length + " bytes");
- }
-
- int bytes = read(b);
- if (bytes != length)
- {
- throw new IOException("Unable to read " + length + " bytes (after " + bytes + " bytes)");
- }
-
- return b;
+ return ExtendedIOUtil.readByteArray(this);
}
public String readString() throws IOException
{
- byte[] bytes = readByteArray();
- if (bytes == null)
- {
- return null;
- }
-
- return new String(bytes);
+ return ExtendedIOUtil.readString(this);
}
public Object readObject() throws IOException, ClassNotFoundException
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutput.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutput.java
index 5adac4e..402a718 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutput.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutput.java
@@ -21,6 +21,4 @@ public interface ExtendedDataOutput extends DataOutput
public void writeByteArray(byte[] b) throws IOException;
public void writeString(String str) throws IOException;
-
- public void writeObject(Object object) throws IOException;
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutputStream.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutputStream.java
index 633c60c..f790577 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutputStream.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedDataOutputStream.java
@@ -27,27 +27,12 @@ public class ExtendedDataOutputStream extends DataOutputStream implements Extend
public void writeByteArray(byte[] b) throws IOException
{
- if (b != null)
- {
- writeInt(b.length);
- write(b);
- }
- else
- {
- writeInt(-1);
- }
+ ExtendedIOUtil.writeByteArray(this, b);
}
public void writeString(String str) throws IOException
{
- if (str != null)
- {
- writeByteArray(str.getBytes());
- }
- else
- {
- writeInt(-1);
- }
+ ExtendedIOUtil.writeString(this, str);
}
public void writeObject(Object object) throws IOException
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java
new file mode 100644
index 0000000..8c2077f
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java
@@ -0,0 +1,83 @@
+/***************************************************************************
+ * 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.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public final class ExtendedIOUtil
+{
+ private ExtendedIOUtil()
+ {
+ }
+
+ public static void writeByteArray(DataOutput out, byte[] b) throws IOException
+ {
+ if (b != null)
+ {
+ out.writeInt(b.length);
+ out.write(b);
+ }
+ else
+ {
+ out.writeInt(-1);
+ }
+ }
+
+ public static void writeString(DataOutput out, String str) throws IOException
+ {
+ if (str != null)
+ {
+ writeByteArray(out, str.getBytes());
+ }
+ else
+ {
+ out.writeInt(-1);
+ }
+ }
+
+ public static byte[] readByteArray(DataInput in) throws IOException
+ {
+ int length = in.readInt();
+ if (length < 0)
+ {
+ return null;
+ }
+
+ byte[] b;
+ try
+ {
+ b = new byte[length];
+ }
+ catch (Throwable t)
+ {
+ throw new IOException("Unable to allocate " + length + " bytes");
+ }
+
+ in.readFully(b);
+ return b;
+ }
+
+ public static String readString(DataInput in) throws IOException
+ {
+ byte[] bytes = readByteArray(in);
+ if (bytes == null)
+ {
+ return null;
+ }
+
+ return new String(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
new file mode 100644
index 0000000..1e3bf03
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java
@@ -0,0 +1,167 @@
+/***************************************************************************
+ * 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.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class SortedFileMap<K extends Comparable, V>
+{
+ private File file;
+
+ private RandomAccessFile randomAccessFile;
+
+ private long entrySize;
+
+ private long entryCount;
+
+ /**
+ * @see RandomAccessFile#RandomAccessFile(File, String)
+ */
+ public SortedFileMap(File file, String mode)
+ {
+ try
+ {
+ this.file = file;
+ randomAccessFile = new RandomAccessFile(file, mode);
+ entrySize = getKeySize() + getValueSize();
+ entryCount = randomAccessFile.length() / entrySize;
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public File getFile()
+ {
+ return file;
+ }
+
+ public RandomAccessFile getRandomAccessFile()
+ {
+ return randomAccessFile;
+ }
+
+ public long getEntryCount()
+ {
+ return entryCount;
+ }
+
+ public int getEntrySize()
+ {
+ return (int)entrySize;
+ }
+
+ public long getPosition(long index)
+ {
+ return index * entrySize;
+ }
+
+ public V get(K key)
+ {
+ try
+ {
+ long index = search(key);
+ if (index < 0)
+ {
+ return null;
+ }
+
+ return readValue(randomAccessFile);
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public V put(K key, V value)
+ {
+ try
+ {
+ long index = search(key);
+ long pos = randomAccessFile.getFilePointer();
+ if (index >= 0)
+ {
+ V oldValue = readValue(randomAccessFile);
+ randomAccessFile.seek(pos);
+ writeValue(randomAccessFile, value);
+ return oldValue;
+ }
+
+ index = -index - 1;
+ for (long i = entryCount; i >= index; --i)
+ {
+ randomAccessFile.seek((i - 1) * entrySize);
+ K k = readKey(randomAccessFile);
+ V v = readValue(randomAccessFile);
+
+ randomAccessFile.seek(i * entrySize);
+ writeKey(randomAccessFile, k);
+ writeValue(randomAccessFile, v);
+ }
+
+ randomAccessFile.seek(pos);
+ writeKey(randomAccessFile, key);
+ writeValue(randomAccessFile, value);
+ return null;
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ /**
+ * @return The index of the entry with the given key if the key exists,
+ * <code>-(insertionIndex + 1)</code> otherwise.
+ */
+ protected long search(K key) throws IOException
+ {
+ long low = 0;
+ long high = entryCount - 1;
+
+ while (low <= high)
+ {
+ long mid = low + high >> 1;
+ randomAccessFile.seek(mid * entrySize);
+ Comparable midVal = readKey(randomAccessFile);
+ int cmp = midVal.compareTo(key);
+
+ if (cmp < 0)
+ low = mid + 1;
+ else if (cmp > 0)
+ high = mid - 1;
+ else
+ return mid; // key found
+ }
+ return -(low + 1); // key not found.
+ }
+
+ public abstract int getKeySize();
+
+ protected abstract K readKey(DataInput in) throws IOException;
+
+ protected abstract void writeKey(DataOutput out, K key) throws IOException;
+
+ public abstract int getValueSize();
+
+ protected abstract V readValue(DataInput in) throws IOException;
+
+ protected abstract void writeValue(DataOutput out, V value) throws IOException;
+}