169N/A/*
2362N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.rmi.*;
0N/Aimport java.rmi.server.*;
0N/Aimport java.util.zip.*;
0N/A
0N/A
0N/Apublic class MultiSocketFactory {
0N/A
0N/A private static RMISocketFactory def =
169N/A RMISocketFactory.getDefaultSocketFactory();
169N/A
0N/A
0N/A public static class ServerFactory
169N/A implements RMIServerSocketFactory, Serializable
0N/A {
0N/A
169N/A private String protocol;
169N/A private byte[] data;
0N/A
169N/A public ServerFactory(String protocol, byte[] data) {
169N/A this.protocol = protocol;
169N/A this.data = data;
169N/A }
0N/A
169N/A public ServerSocket createServerSocket(int port) throws IOException
169N/A {
169N/A if (protocol.equals("compress")) {
169N/A return new CompressServerSocket(port);
0N/A
169N/A } else if (protocol.equals("xor")) {
169N/A if (data == null || data.length != 1)
169N/A throw new IOException("invalid argument for XOR protocol");
169N/A return new XorServerSocket(port, data[0]);
169N/A
169N/A }
169N/A
169N/A return def.createServerSocket(port);
169N/A }
0N/A }
0N/A
0N/A public static class ClientFactory
169N/A implements RMIClientSocketFactory, Serializable
0N/A {
0N/A
169N/A private String protocol;
169N/A private byte[] data;
169N/A
169N/A public ClientFactory(String protocol, byte[] data) {
169N/A this.protocol = protocol;
169N/A this.data = data;
169N/A }
0N/A
169N/A public Socket createSocket(String host, int port)
169N/A throws IOException
169N/A {
169N/A if (protocol.equals("compress")) {
169N/A return new CompressSocket(host, port);
0N/A
169N/A } else if (protocol.equals("xor")) {
169N/A if (data == null || data.length != 1)
169N/A throw new IOException("invalid argument for XOR protocol");
169N/A return new XorSocket(host, port, data[0]);
0N/A
169N/A }
169N/A
169N/A return def.createSocket(host, port);
169N/A }
0N/A }
0N/A
0N/A static class CompressSocket extends Socket {
169N/A private InputStream in;
169N/A private OutputStream out;
169N/A public CompressSocket() { super(); }
169N/A public CompressSocket(String host, int port) throws IOException {
169N/A super(host, port);
0N/A }
169N/A public InputStream getInputStream() throws IOException {
169N/A if (in == null) {
169N/A in = new CompressInputStream(super.getInputStream());
169N/A }
169N/A return in;
169N/A }
169N/A public OutputStream getOutputStream() throws IOException {
169N/A if (out == null) {
169N/A out = new CompressOutputStream(super.getOutputStream());
169N/A }
169N/A return out;
169N/A }
0N/A }
0N/A
0N/A static class CompressServerSocket extends ServerSocket {
169N/A public CompressServerSocket(int port) throws IOException {
169N/A super(port);
169N/A }
169N/A public Socket accept() throws IOException {
169N/A Socket s = new CompressSocket();
169N/A implAccept(s);
169N/A return s;
169N/A }
0N/A }
0N/A
0N/A static class XorSocket extends Socket {
169N/A private byte pattern;
169N/A private InputStream in;
169N/A private OutputStream out;
169N/A public XorSocket(byte pattern) { super(); this.pattern = pattern; }
169N/A public XorSocket(String host, int port, byte pattern)
169N/A throws IOException
169N/A {
169N/A super(host, port);
169N/A this.pattern = pattern;
0N/A }
169N/A public InputStream getInputStream() throws IOException {
169N/A if (in == null) {
169N/A in = new XorInputStream(super.getInputStream(), pattern);
169N/A }
169N/A return in;
169N/A }
169N/A public OutputStream getOutputStream() throws IOException {
169N/A if (out == null) {
169N/A out = new XorOutputStream(super.getOutputStream(), pattern);
169N/A }
169N/A return out;
169N/A }
0N/A }
0N/A
0N/A static class XorServerSocket extends ServerSocket {
169N/A private byte pattern;
169N/A public XorServerSocket(int port, byte pattern) throws IOException {
169N/A super(port);
169N/A this.pattern = pattern;
169N/A }
169N/A public Socket accept() throws IOException {
169N/A Socket s = new XorSocket(pattern);
169N/A implAccept(s);
169N/A return s;
169N/A }
0N/A }
0N/A
0N/A static class XorOutputStream extends FilterOutputStream {
169N/A private byte pattern;
169N/A public XorOutputStream(OutputStream out, byte pattern) {
169N/A super(out);
169N/A this.pattern = pattern;
169N/A }
169N/A public void write(int b) throws IOException {
169N/A out.write(b ^ pattern);
169N/A out.flush();
169N/A }
169N/A public void write(byte b[], int off, int len) throws IOException {
169N/A for (int i = 0; i < len; i++)
169N/A write(b[off + i]);
169N/A }
0N/A }
0N/A
0N/A static class XorInputStream extends FilterInputStream {
169N/A private byte pattern;
169N/A public XorInputStream(InputStream in, byte pattern) {
169N/A super(in);
169N/A this.pattern = pattern;
169N/A }
169N/A public int read() throws IOException {
169N/A int b = in.read();
169N/A// System.out.print("BEFORE: " + Integer.toHexString(b));
169N/A if (b != -1)
169N/A b = (b ^ pattern) & 0xFF;
169N/A// System.out.println("\tAFTER: " + Integer.toHexString(b));
169N/A return b;
169N/A }
169N/A public int read(byte b[], int off, int len) throws IOException {
169N/A if (len <= 0) {
169N/A return 0;
169N/A }
0N/A
169N/A int c = read();
169N/A if (c == -1) {
169N/A return -1;
169N/A }
169N/A b[off] = (byte)c;
0N/A
169N/A int i = 1;
0N/A/*****
169N/A try {
169N/A for (; i < len ; i++) {
169N/A c = read();
169N/A if (c == -1) {
169N/A break;
169N/A }
169N/A if (b != null) {
169N/A b[off + i] = (byte)c;
169N/A }
169N/A }
169N/A } catch (IOException ee) {
169N/A }
0N/A*****/
169N/A return i;
169N/A }
0N/A }
0N/A}