home *** CD-ROM | disk | FTP | other *** search
- package netscape.util;
-
- import java.io.ByteArrayOutputStream;
- import java.io.FilterOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
-
- public class Serializer extends FilterOutputStream {
- private static boolean[] unsafeChars = new boolean[127];
- private static int SMALL_STRING_LIMIT = 40;
- private final int BUF_LEN = 128;
- private byte[] buf = new byte[128];
- private int bufIndex;
-
- public Serializer(OutputStream var1) {
- super(var1);
- }
-
- private void flushBuffer() throws IOException {
- if (this.bufIndex > 0) {
- ((FilterOutputStream)this).write(this.buf, 0, this.bufIndex);
- this.bufIndex = 0;
- }
-
- }
-
- final void writeOutput(int var1) throws IOException {
- if (this.bufIndex >= 128) {
- this.flushBuffer();
- }
-
- this.buf[this.bufIndex++] = (byte)var1;
- }
-
- private final void serializeHashtable(Hashtable var1) throws IOException {
- Enumeration var2 = var1.keys();
- this.writeOutput(123);
-
- for(; var2.hasMoreElements(); this.writeOutput(59)) {
- Object var3 = var2.nextElement();
- Object var4 = var1.get(var3);
- if (var3 instanceof String) {
- this.serializeString((String)var3);
- } else {
- this.serializeObjectInternal(var3);
- }
-
- this.writeOutput(61);
- if (var4 instanceof String) {
- this.serializeString((String)var4);
- } else {
- this.serializeObjectInternal(var4);
- }
- }
-
- this.writeOutput(125);
- }
-
- private final void serializeArray(Object[] var1) throws IOException {
- this.writeOutput(91);
- int var3 = 0;
-
- for(int var4 = var1.length; var3 < var4; ++var3) {
- Object var2 = var1[var3];
- if (var2 instanceof String) {
- this.serializeString((String)var2);
- } else {
- this.serializeObjectInternal(var2);
- }
-
- if (var3 < var4 - 1) {
- this.writeOutput(44);
- }
- }
-
- this.writeOutput(93);
- }
-
- private final void serializeVector(Vector var1) throws IOException {
- this.writeOutput(40);
- int var3 = 0;
-
- for(int var4 = var1.count(); var3 < var4; ++var3) {
- Object var2 = var1.elementAt(var3);
- if (var2 instanceof String) {
- this.serializeString((String)var2);
- } else {
- this.serializeObjectInternal(var2);
- }
-
- if (var3 < var4 - 1) {
- this.writeOutput(44);
- }
- }
-
- this.writeOutput(41);
- }
-
- final boolean stringRequiresQuotes(String var1) {
- int var3 = 0;
-
- for(int var4 = var1.length(); var3 < var4; ++var3) {
- char var2 = var1.charAt(var3);
- if (var2 >= 127) {
- return true;
- }
-
- if (unsafeChars[var2]) {
- return true;
- }
- }
-
- return false;
- }
-
- private final boolean stringRequiresQuotes(char[] var1) {
- int var3 = 0;
-
- for(int var4 = var1.length; var3 < var4; ++var3) {
- char var2 = var1[var3];
- if (var2 >= 127) {
- return true;
- }
-
- if (unsafeChars[var2]) {
- return true;
- }
- }
-
- return false;
- }
-
- private final int fourBitToAscii(int var1) {
- return var1 < 10 ? 48 + var1 : 65 + (var1 - 10);
- }
-
- void serializeString(String var1) throws IOException {
- boolean var2 = false;
- boolean var3 = true;
- char[] var7 = null;
- int var5;
- if (var1 != null && (var5 = var1.length()) != 0) {
- if (var5 <= 8) {
- var3 = false;
- } else {
- var3 = true;
- var7 = var1.toCharArray();
- }
-
- if (var5 > SMALL_STRING_LIMIT) {
- var2 = true;
- } else if (var3) {
- var2 = this.stringRequiresQuotes(var7);
- } else {
- var2 = this.stringRequiresQuotes(var1);
- }
-
- if (var2) {
- this.writeOutput(34);
- }
-
- for(int var4 = 0; var4 < var5; ++var4) {
- char var6;
- if (var3) {
- var6 = var7[var4];
- } else {
- var6 = var1.charAt(var4);
- }
-
- if (var6 < 255) {
- if (var6 >= '#' && var6 <= '~' && var6 != '\\') {
- this.writeOutput(var6);
- } else {
- switch (var6) {
- case '\t':
- this.writeOutput(92);
- this.writeOutput(116);
- break;
- case '\n':
- this.writeOutput(92);
- this.writeOutput(110);
- break;
- case '\r':
- this.writeOutput(92);
- this.writeOutput(114);
- break;
- case ' ':
- case '!':
- this.writeOutput(var6);
- break;
- case '"':
- this.writeOutput(92);
- this.writeOutput(34);
- break;
- case '\\':
- this.writeOutput(92);
- this.writeOutput(92);
- break;
- default:
- this.writeOutput(92);
- this.writeOutput(48 + (var6 >> 6));
- this.writeOutput(48 + (var6 >> 3 & 7));
- this.writeOutput(48 + (var6 & 7));
- }
- }
- } else {
- this.writeOutput(92);
- this.writeOutput(117);
- this.writeOutput(this.fourBitToAscii(var6 >> 12));
- this.writeOutput(this.fourBitToAscii(var6 >> 8 & 15));
- this.writeOutput(this.fourBitToAscii(var6 >> 4 & 15));
- this.writeOutput(this.fourBitToAscii(var6 & 15));
- }
- }
-
- if (var3) {
- Object var10 = null;
- }
-
- if (var2) {
- this.writeOutput(34);
- }
-
- } else {
- this.writeOutput(34);
- this.writeOutput(34);
- }
- }
-
- final void serializeNull() throws IOException {
- this.writeOutput(64);
- }
-
- private final void serializeObjectInternal(Object var1) throws IOException {
- if (var1 instanceof String) {
- this.serializeString((String)var1);
- } else if (var1 instanceof Hashtable) {
- this.serializeHashtable((Hashtable)var1);
- } else if (var1 instanceof Object[]) {
- this.serializeArray(var1);
- } else if (var1 instanceof Vector) {
- this.serializeVector((Vector)var1);
- } else if (var1 == null) {
- this.serializeNull();
- } else {
- this.serializeString(var1.toString());
- }
- }
-
- public void flush() throws IOException {
- this.flushBuffer();
- super.flush();
- }
-
- public void writeObject(Object var1) throws IOException {
- this.serializeObjectInternal(var1);
- }
-
- public static String serializeObject(Object var0) {
- Object var1 = null;
- String var6;
- if (var0 == null) {
- var6 = null;
- } else {
- ByteArrayOutputStream var2 = new ByteArrayOutputStream(256);
- Serializer var3 = new Serializer(var2);
-
- try {
- var3.writeObject(var0);
- var3.flush();
- } catch (IOException var5) {
- }
-
- var6 = var2.toString();
-
- try {
- ((FilterOutputStream)var3).close();
- ((OutputStream)var2).close();
- } catch (IOException var4) {
- }
-
- Object var7 = null;
- Object var8 = null;
- }
-
- return var6;
- }
-
- public static boolean writeObject(OutputStream var0, Object var1) {
- try {
- Serializer var2 = new Serializer(var0);
- var2.writeObject(var1);
- var2.flush();
- return true;
- } catch (IOException var3) {
- return false;
- }
- }
-
- static {
- for(int var0 = 0; var0 < 32; ++var0) {
- unsafeChars[var0] = true;
- }
-
- unsafeChars[32] = true;
- unsafeChars[34] = true;
- unsafeChars[91] = true;
- unsafeChars[93] = true;
- unsafeChars[44] = true;
- unsafeChars[40] = true;
- unsafeChars[41] = true;
- unsafeChars[123] = true;
- unsafeChars[125] = true;
- unsafeChars[61] = true;
- unsafeChars[59] = true;
- unsafeChars[47] = true;
- unsafeChars[64] = true;
- unsafeChars[33] = true;
- unsafeChars[35] = true;
- unsafeChars[36] = true;
- unsafeChars[37] = true;
- unsafeChars[38] = true;
- unsafeChars[39] = true;
- unsafeChars[58] = true;
- unsafeChars[60] = true;
- unsafeChars[62] = true;
- unsafeChars[63] = true;
- unsafeChars[92] = true;
- unsafeChars[94] = true;
- unsafeChars[96] = true;
- unsafeChars[124] = true;
- unsafeChars[126] = true;
- }
- }
-