Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/SortedFileMapTest.java17
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataInputExtender.java112
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataOutputExtender.java107
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/SortedFileMap.java36
4 files changed, 247 insertions, 25 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
index 65fbd54fec..0364697478 100644
--- 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
@@ -10,12 +10,11 @@
**************************************************************************/
package org.eclipse.net4j.util.tests;
-import org.eclipse.net4j.util.io.ExtendedIOUtil;
+import org.eclipse.net4j.util.io.ExtendedDataInput;
+import org.eclipse.net4j.util.io.ExtendedDataOutput;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.io.SortedFileMap;
-import java.io.DataInput;
-import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
@@ -71,13 +70,13 @@ public class SortedFileMapTest extends AbstractOMTest
}
@Override
- protected Integer readKey(DataInput in) throws IOException
+ protected Integer readKey(ExtendedDataInput in) throws IOException
{
return in.readInt();
}
@Override
- protected void writeKey(DataOutput out, Integer key) throws IOException
+ protected void writeKey(ExtendedDataOutput out, Integer key) throws IOException
{
out.writeInt(key);
}
@@ -89,13 +88,13 @@ public class SortedFileMapTest extends AbstractOMTest
}
@Override
- protected String readValue(DataInput in) throws IOException
+ protected String readValue(ExtendedDataInput in) throws IOException
{
- return ExtendedIOUtil.readString(in);
+ return in.readString();
}
@Override
- protected void writeValue(DataOutput out, String value) throws IOException
+ protected void writeValue(ExtendedDataOutput out, String value) throws IOException
{
byte[] bytes = value.getBytes();
if (bytes.length + 4 > getValueSize())
@@ -103,7 +102,7 @@ public class SortedFileMapTest extends AbstractOMTest
throw new IllegalArgumentException("Value size of " + getValueSize() + " exceeded: " + value);
}
- ExtendedIOUtil.writeByteArray(out, bytes);
+ out.writeByteArray(bytes);
}
}
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataInputExtender.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataInputExtender.java
new file mode 100644
index 0000000000..cc752b14bf
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataInputExtender.java
@@ -0,0 +1,112 @@
+/***************************************************************************
+ * 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.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class DataInputExtender implements ExtendedDataInput
+{
+ private DataInput input;
+
+ public DataInputExtender(DataInput input)
+ {
+ this.input = input;
+ }
+
+ public boolean readBoolean() throws IOException
+ {
+ return input.readBoolean();
+ }
+
+ public byte readByte() throws IOException
+ {
+ return input.readByte();
+ }
+
+ public char readChar() throws IOException
+ {
+ return input.readChar();
+ }
+
+ public double readDouble() throws IOException
+ {
+ return input.readDouble();
+ }
+
+ public float readFloat() throws IOException
+ {
+ return input.readFloat();
+ }
+
+ public void readFully(byte[] b, int off, int len) throws IOException
+ {
+ input.readFully(b, off, len);
+ }
+
+ public void readFully(byte[] b) throws IOException
+ {
+ input.readFully(b);
+ }
+
+ public int readInt() throws IOException
+ {
+ return input.readInt();
+ }
+
+ public String readLine() throws IOException
+ {
+ return input.readLine();
+ }
+
+ public long readLong() throws IOException
+ {
+ return input.readLong();
+ }
+
+ public short readShort() throws IOException
+ {
+ return input.readShort();
+ }
+
+ public int readUnsignedByte() throws IOException
+ {
+ return input.readUnsignedByte();
+ }
+
+ public int readUnsignedShort() throws IOException
+ {
+ return input.readUnsignedShort();
+ }
+
+ public String readUTF() throws IOException
+ {
+ return input.readUTF();
+ }
+
+ public byte[] readByteArray() throws IOException
+ {
+ return ExtendedIOUtil.readByteArray(input);
+ }
+
+ public String readString() throws IOException
+ {
+ return ExtendedIOUtil.readString(input);
+ }
+
+ public int skipBytes(int n) throws IOException
+ {
+ return input.skipBytes(n);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataOutputExtender.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataOutputExtender.java
new file mode 100644
index 0000000000..482d27f418
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/DataOutputExtender.java
@@ -0,0 +1,107 @@
+/***************************************************************************
+ * 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.DataOutput;
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class DataOutputExtender implements ExtendedDataOutput
+{
+ private DataOutput output;
+
+ public DataOutputExtender(DataOutput output)
+ {
+ this.output = output;
+ }
+
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ output.write(b, off, len);
+ }
+
+ public void write(byte[] b) throws IOException
+ {
+ output.write(b);
+ }
+
+ public void write(int b) throws IOException
+ {
+ output.write(b);
+ }
+
+ public void writeBoolean(boolean v) throws IOException
+ {
+ output.writeBoolean(v);
+ }
+
+ public void writeByte(int v) throws IOException
+ {
+ output.writeByte(v);
+ }
+
+ public void writeBytes(String s) throws IOException
+ {
+ output.writeBytes(s);
+ }
+
+ public void writeChar(int v) throws IOException
+ {
+ output.writeChar(v);
+ }
+
+ public void writeChars(String s) throws IOException
+ {
+ output.writeChars(s);
+ }
+
+ public void writeDouble(double v) throws IOException
+ {
+ output.writeDouble(v);
+ }
+
+ public void writeFloat(float v) throws IOException
+ {
+ output.writeFloat(v);
+ }
+
+ public void writeInt(int v) throws IOException
+ {
+ output.writeInt(v);
+ }
+
+ public void writeLong(long v) throws IOException
+ {
+ output.writeLong(v);
+ }
+
+ public void writeShort(int v) throws IOException
+ {
+ output.writeShort(v);
+ }
+
+ public void writeUTF(String str) throws IOException
+ {
+ output.writeUTF(str);
+ }
+
+ public void writeByteArray(byte[] b) throws IOException
+ {
+ ExtendedIOUtil.writeByteArray(output, b);
+ }
+
+ public void writeString(String str) throws IOException
+ {
+ ExtendedIOUtil.writeString(output, str);
+ }
+}
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 244ec56751..5aca41f41f 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
@@ -11,8 +11,6 @@
package org.eclipse.net4j.util.io;
import java.io.Closeable;
-import java.io.DataInput;
-import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
@@ -26,6 +24,10 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
private RandomAccessFile randomAccessFile;
+ private ExtendedDataInput input;
+
+ private ExtendedDataOutput output;
+
private long entrySize;
private long entryCount;
@@ -39,6 +41,8 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
{
this.file = file;
randomAccessFile = new RandomAccessFile(file, mode);
+ input = new DataInputExtender(randomAccessFile);
+ output = new DataOutputExtender(randomAccessFile);
entrySize = getKeySize() + getValueSize();
entryCount = randomAccessFile.length() / entrySize;
}
@@ -88,7 +92,7 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
return null;
}
- return readValue(randomAccessFile);
+ return readValue(input);
}
catch (IOException ex)
{
@@ -104,9 +108,9 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
if (index >= 0)
{
long pos = randomAccessFile.getFilePointer();
- V oldValue = readValue(randomAccessFile);
+ V oldValue = readValue(input);
randomAccessFile.seek(pos);
- writeValue(randomAccessFile, value);
+ writeValue(output, value);
return oldValue;
}
@@ -114,18 +118,18 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
for (long i = entryCount; i > index; --i)
{
randomAccessFile.seek((i - 1) * entrySize);
- K k = readKey(randomAccessFile);
- V v = readValue(randomAccessFile);
+ K k = readKey(input);
+ V v = readValue(input);
randomAccessFile.seek(i * entrySize);
- writeKey(randomAccessFile, k);
- writeValue(randomAccessFile, v);
+ writeKey(output, k);
+ writeValue(output, v);
}
++entryCount;
randomAccessFile.seek(getPosition(index));
- writeKey(randomAccessFile, key);
- writeValue(randomAccessFile, value);
+ writeKey(output, key);
+ writeValue(output, value);
return null;
}
catch (IOException ex)
@@ -147,7 +151,7 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
{
long mid = low + high >> 1;
randomAccessFile.seek(mid * entrySize);
- Comparable midVal = readKey(randomAccessFile);
+ Comparable midVal = readKey(input);
int cmp = midVal.compareTo(key);
if (cmp < 0)
@@ -162,13 +166,13 @@ public abstract class SortedFileMap<K extends Comparable, V> implements Closeabl
public abstract int getKeySize();
- protected abstract K readKey(DataInput in) throws IOException;
+ protected abstract K readKey(ExtendedDataInput in) throws IOException;
- protected abstract void writeKey(DataOutput out, K key) throws IOException;
+ protected abstract void writeKey(ExtendedDataOutput out, K key) throws IOException;
public abstract int getValueSize();
- protected abstract V readValue(DataInput in) throws IOException;
+ protected abstract V readValue(ExtendedDataInput in) throws IOException;
- protected abstract void writeValue(DataOutput out, V value) throws IOException;
+ protected abstract void writeValue(ExtendedDataOutput out, V value) throws IOException;
}

Back to the top