Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORInputStream.java56
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XOROutputStream.java52
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORStreamWrapper.java61
3 files changed, 169 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORInputStream.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORInputStream.java
new file mode 100644
index 0000000000..b2033b30e5
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORInputStream.java
@@ -0,0 +1,56 @@
+/***************************************************************************
+ * 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.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * @author Eike Stepper
+ */
+public class XORInputStream extends FilterInputStream
+{
+ private byte[] key;
+
+ private int index;
+
+ public XORInputStream(InputStream in, byte... key)
+ {
+ super(in);
+ this.key = key;
+ }
+
+ public byte[] getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public int read() throws IOException
+ {
+ int b = super.read();
+ if (b != -1)
+ {
+ if (key != null && key.length != 0)
+ {
+ if (index == key.length)
+ {
+ index = 0;
+ }
+
+ b = b & 0x0f ^ key[index++];
+ }
+ }
+
+ return b;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XOROutputStream.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XOROutputStream.java
new file mode 100644
index 0000000000..291ba708b7
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XOROutputStream.java
@@ -0,0 +1,52 @@
+/***************************************************************************
+ * 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.FilterOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * @author Eike Stepper
+ */
+public class XOROutputStream extends FilterOutputStream
+{
+ private byte[] key;
+
+ private int index;
+
+ public XOROutputStream(OutputStream out, byte... key)
+ {
+ super(out);
+ this.key = key;
+ }
+
+ public byte[] getKey()
+ {
+ return key;
+ }
+
+ @Override
+ public void write(int b) throws IOException
+ {
+ if (key != null && key.length != 0)
+ {
+ if (index == key.length)
+ {
+ index = 0;
+ }
+
+ b = b & 0x0f ^ key[index++];
+ }
+
+ super.write(b);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORStreamWrapper.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORStreamWrapper.java
new file mode 100644
index 0000000000..65853cb077
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/XORStreamWrapper.java
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * 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.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author Eike Stepper
+ */
+public class XORStreamWrapper implements IStreamWrapper<XORInputStream, XOROutputStream>
+{
+ private byte[] key;
+
+ public XORStreamWrapper(byte[] key)
+ {
+ this.key = key;
+ }
+
+ public byte[] getKey()
+ {
+ return key;
+ }
+
+ public XORInputStream wrapInputStream(InputStream in) throws IOException
+ {
+ if (in instanceof XORInputStream)
+ {
+ return (XORInputStream)in;
+ }
+
+ return new XORInputStream(in, key);
+ }
+
+ public XOROutputStream wrapOutputStream(OutputStream out) throws IOException
+ {
+ if (out instanceof XOROutputStream)
+ {
+ return (XOROutputStream)out;
+ }
+
+ return new XOROutputStream(out, key);
+ }
+
+ public void finishInputStream(XORInputStream in) throws IOException
+ {
+ }
+
+ public void finishOutputStream(XOROutputStream out) throws IOException
+ {
+ }
+}

Back to the top