jdk/test/java/rmi/server/RMISocketFactory/useSocketFactory/registry/Compress.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 309 bda219d843f6
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/* 
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-1999 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class Compress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    interface CompressConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
	// constants for 6-bit code values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
	static final int NOP  = 0;	// no operation: used to pad words on flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
	static final int RAW  = 1;	// introduces raw byte format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
	static final int BASE = 2;	// base for codes found in lookup table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
	static final String codeTable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
	    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ,.!?\"'()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static class CompressRMIClientSocketFactory 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
	implements java.rmi.server.RMIClientSocketFactory, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
	public Socket createSocket(String host, int port) 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
	    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
	    return ((Socket) new CompressSocket(host, port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static class CompressRMIServerSocketFactory 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
	implements RMIServerSocketFactory, 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
		   Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
	public ServerSocket createServerSocket(int port) 
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
	    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
	    return ((ServerSocket) new CompressServerSocket(port));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static class CompressSocket extends Socket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
	private InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
	private OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
	public CompressSocket() { super(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
	public CompressSocket(String host, int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
	    super(host, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
	public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
	    if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
		in = new CompressInputStream(super.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
	    return in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
	public OutputStream getOutputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
	    if (out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
		out = new CompressOutputStream(super.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
	    return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static class CompressServerSocket extends ServerSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
	public CompressServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
	    super(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
	public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
	    Socket s = new CompressSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
	    implAccept(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
	    return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static class CompressInputStream extends FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
	implements CompressConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
	public CompressInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
	    super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
	// buffer of unpacked 6-bit codes from last 32-word read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
	int buf[] = new int[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
	// position of next code to read in buffer (5 == end of buffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
	int bufPos = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
	public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
	    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
		int code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
		do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
		    code = readCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
		} while (code == NOP);	// ignore NOP codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
		
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
		if (code >= BASE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
		    return codeTable.charAt(code - BASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
		else if (code == RAW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
		    int high = readCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
		    int low = readCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
		    return (high << 4) | low;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
		} else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
		    throw new IOException("unknown compression code: " + code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
	    } catch (EOFException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
		return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
	public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
	    if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
		return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
	    int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
	    if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
		return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
	    b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
	    int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
	    /*****
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
		  try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
		  for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
		  c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
		  if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
		  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
		  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
		  if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
		  b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
		  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
		  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
		  } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
		  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
	    *****/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
	    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
	private int readCode() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
	    if (bufPos == 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
		int b1 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
		int b2 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
		int b3 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
		int b4 = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
		if ((b1 | b2 | b3 | b4) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
		    throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
		int pack = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
		buf[0] = (pack >>> 24) & 0x3F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
		buf[1] = (pack >>> 18) & 0x3F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
		buf[2] = (pack >>> 12) & 0x3F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
		buf[3] = (pack >>>  6) & 0x3F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
		buf[4] = (pack >>>  0) & 0x3F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
		bufPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
	    return buf[bufPos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public static class CompressOutputStream extends FilterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
	implements CompressConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
	public CompressOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
	    super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
	// buffer of 6-bit codes to pack into next 32-bit word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
	int buf[] = new int[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
	// number of valid codes pending in buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
	int bufPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
	public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
	    b &= 0xFF;			// force argument to a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
	    
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
	    int pos = codeTable.indexOf((char)b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
	    if (pos != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
		writeCode(BASE + pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
	    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
		writeCode(RAW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
		writeCode(b >> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
		writeCode(b & 0xF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
	public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
	    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
	     * This is quite an inefficient implementation, because it has to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
	     * call the other write method for every byte in the array.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
	     * could be optimized for performance by doing all the processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
	     * in this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
	     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
	    for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
		write(b[off + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
	public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
	    while (bufPos > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
		writeCode(NOP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
	
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
	private void writeCode(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
	    buf[bufPos++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
	    if (bufPos == 5) {	// write next word when we have 5 codes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
		int pack = (buf[0] << 24) | (buf[1] << 18) | (buf[2] << 12) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
		    (buf[3] << 6) | buf[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
		out.write((pack >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
		out.write((pack >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
		out.write((pack >>> 8)  & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
		out.write((pack >>> 0)  & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
		bufPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
	    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
	}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
}