summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-07-15 06:10:07 (EDT)
committerEike Stepper2007-07-15 06:10:07 (EDT)
commit247f5b67a0c116d769bee74ba0d6e75461372530 (patch)
treebae204e124dd8e6ad4f58f5364d477ac02f0b712
parentddf6f5e5f3e230d853deaf39086858286a0094e5 (diff)
downloadcdo-247f5b67a0c116d769bee74ba0d6e75461372530.zip
cdo-247f5b67a0c116d769bee74ba0d6e75461372530.tar.gz
cdo-247f5b67a0c116d769bee74ba0d6e75461372530.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java24
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/HexUtil.java130
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreference.java2
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreferences.java24
4 files changed, 163 insertions, 17 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
index c5391f2..5cbf89d 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/pref/Preferences.java
@@ -31,20 +31,6 @@ import java.util.Properties;
*/
public class Preferences extends Notifier implements OMPreferences
{
- public static final boolean DEFAULT_BOOLEAN = false;
-
- public static final int DEFAULT_INTEGER = 0;
-
- public static final long DEFAULT_LONG = 0L;
-
- public static final float DEFAULT_FLOAT = 0.0f;
-
- public static final double DEFAULT_DOUBLE = 0.0d;
-
- public static final String DEFAULT_STRING = "";
-
- public static final String[] DEFAULT_ARRAY = {};
-
private AbstractBundle bundle;
private Map<String, Preference> prefs = new HashMap();
@@ -174,6 +160,11 @@ public class Preferences extends Notifier implements OMPreferences
return init(new ArrayPreference(this, name, defaultValue));
}
+ public OMPreference<byte[]> init(String name, byte[] defaultValue)
+ {
+ return init(new BytesPreference(this, name, defaultValue));
+ }
+
public OMPreference<Boolean> initBoolean(String name)
{
return init(name, DEFAULT_BOOLEAN);
@@ -209,6 +200,11 @@ public class Preferences extends Notifier implements OMPreferences
return init(name, DEFAULT_ARRAY);
}
+ public OMPreference<byte[]> initBytes(String name)
+ {
+ return init(name, DEFAULT_BYTES);
+ }
+
public OMPreference<Boolean> getBoolean(String name)
{
return null;
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/HexUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/HexUtil.java
index cac31b3..91a78e4 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/HexUtil.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/HexUtil.java
@@ -10,6 +10,10 @@
**************************************************************************/
package org.eclipse.net4j.util;
+import org.eclipse.net4j.util.io.IORuntimeException;
+
+import java.io.IOException;
+
/**
* @author Eike Stepper
*/
@@ -21,7 +25,114 @@ public final class HexUtil
{
}
- public static String formatLong(long v)
+ /**
+ * Converts a byte array into a string of lower case hex chars.
+ *
+ * @param bs
+ * A byte array
+ * @param off
+ * The index of the first byte to read
+ * @param length
+ * The number of bytes to read.
+ * @return the string of hex chars.
+ */
+ public static final String bytesToHex(byte[] bs, int off, int length)
+ {
+ if (bs.length <= off || bs.length < off + length)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ StringBuilder sb = new StringBuilder(length * 2);
+ bytesToHexAppend(bs, off, length, sb);
+ return sb.toString();
+ }
+
+ public static final void bytesToHexAppend(byte[] bs, int off, int length, Appendable appendable)
+ {
+ if (bs.length <= off || bs.length < off + length)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ if (appendable instanceof StringBuffer)
+ {
+ StringBuffer buffer = (StringBuffer)appendable;
+ buffer.ensureCapacity(buffer.length() + length * 2);
+ }
+
+ try
+ {
+ for (int i = off; i < off + length; i++)
+ {
+ appendable.append(Character.forDigit(bs[i] >>> 4 & 0xf, 16));
+ appendable.append(Character.forDigit(bs[i] & 0xf, 16));
+ }
+ }
+ catch (IOException ex)
+ {
+ throw new IORuntimeException(ex);
+ }
+ }
+
+ public static final String bytesToHex(byte[] bs)
+ {
+ return bytesToHex(bs, 0, bs.length);
+ }
+
+ public static final byte[] hexToBytes(String s)
+ {
+ return hexToBytes(s, 0);
+ }
+
+ public static final byte[] hexToBytes(String s, int off)
+ {
+ byte[] bs = new byte[off + (1 + s.length()) / 2];
+ hexToBytes(s, bs, off);
+ return bs;
+ }
+
+ /**
+ * Converts a String of hex characters into an array of bytes.
+ *
+ * @param s
+ * A string of hex characters (upper case or lower) of even length.
+ * @param out
+ * A byte array of length at least s.length()/2 + off
+ * @param off
+ * The first byte to write of the array
+ */
+ public static final void hexToBytes(String s, byte[] out, int off) throws NumberFormatException,
+ IndexOutOfBoundsException
+ {
+ int slen = s.length();
+ if (slen % 2 != 0)
+ {
+ s = '0' + s;
+ }
+
+ if (out.length < off + slen / 2)
+ {
+ throw new IndexOutOfBoundsException("Output buffer too small for input (" + out.length + '<' + off + slen / 2
+ + ')');
+ }
+
+ // Safe to assume the string is even length
+ byte b1, b2;
+ for (int i = 0; i < slen; i += 2)
+ {
+ b1 = (byte)Character.digit(s.charAt(i), 16);
+ b2 = (byte)Character.digit(s.charAt(i + 1), 16);
+ if (b1 < 0 || b2 < 0)
+ {
+ throw new NumberFormatException();
+ }
+
+ out[off + i / 2] = (byte)(b1 << 4 | b2);
+ }
+ }
+
+ public static String longToHex(long v)
{
final String hex = Long.toHexString(v);
if (hex.length() < 8)
@@ -32,12 +143,26 @@ public final class HexUtil
return hex;
}
+ @Deprecated
public static String formatByte(int b)
{
assertByte(b);
return "" + DIGITS[b >> 4] + DIGITS[b & 0xf]; //$NON-NLS-1$
}
+ @Deprecated
+ public static String formatBytes(byte[] bytes)
+ {
+ StringBuilder builder = new StringBuilder();
+ for (byte b : bytes)
+ {
+ appendHex(builder, b - Byte.MIN_VALUE);
+ }
+
+ return builder.toString();
+ }
+
+ @Deprecated
public static void appendHex(StringBuilder builder, int b)
{
assertByte(b);
@@ -45,11 +170,12 @@ public final class HexUtil
builder.append(DIGITS[b & 0xf]);
}
+ @Deprecated
private static void assertByte(int b)
{
if (b < 0 || b > 255)
{
- throw new IllegalArgumentException("b < 0 || b > 255"); //$NON-NLS-1$
+ throw new IllegalArgumentException("b=" + b); //$NON-NLS-1$
}
}
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreference.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreference.java
index 8ed80a2..ee62faf 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreference.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreference.java
@@ -36,6 +36,6 @@ public interface OMPreference<T>
*/
public enum Type
{
- BOOLEAN, INTEGER, LONG, FLOAT, DOUBLE, STRING
+ BOOLEAN, INTEGER, LONG, FLOAT, DOUBLE, STRING, ARRAY, BYTES
}
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreferences.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreferences.java
index 33a7267..80ada6a 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreferences.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/pref/OMPreferences.java
@@ -18,6 +18,22 @@ import org.eclipse.net4j.util.om.OMBundle;
*/
public interface OMPreferences extends INotifier
{
+ public static final boolean DEFAULT_BOOLEAN = false;
+
+ public static final int DEFAULT_INTEGER = 0;
+
+ public static final long DEFAULT_LONG = 0L;
+
+ public static final float DEFAULT_FLOAT = 0.0f;
+
+ public static final double DEFAULT_DOUBLE = 0.0d;
+
+ public static final String DEFAULT_STRING = "";
+
+ public static final String[] DEFAULT_ARRAY = {};
+
+ public static final byte[] DEFAULT_BYTES = {};
+
public OMBundle getBundle();
public boolean isDirty();
@@ -36,6 +52,10 @@ public interface OMPreferences extends INotifier
public OMPreference<String> init(String name, String defaultValue);
+ public OMPreference<String[]> init(String name, String[] defaultValue);
+
+ public OMPreference<byte[]> init(String name, byte[] defaultValue);
+
public OMPreference<Boolean> initBoolean(String name);
public OMPreference<Integer> initInteger(String name);
@@ -48,6 +68,10 @@ public interface OMPreferences extends INotifier
public OMPreference<String> initString(String name);
+ public OMPreference<String[]> initArray(String name);
+
+ public OMPreference<byte[]> initBytes(String name);
+
public OMPreference<Boolean> getBoolean(String name);
public OMPreference<Integer> getInteger(String name);