⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testserialfields.java

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			throws IOException, ClassNotFoundException		{			//Object ignored = stream.putFields();			stream.writeFields();		}		private void readObject(ObjectInputStream stream) 			throws IOException, ClassNotFoundException		{			stream.readFields();		}	}	private static class Test0012 // check defaultReadObject	implements Serializable	{		private static final long serialVersionUID = constantUID;		private int x = 11;		private int y = 12;		public String toString()		{			return (this.getClass().getName() + ": " +x+ "," +y);		}		private void writeObject(ObjectOutputStream stream) 			throws IOException, ClassNotFoundException		{			Object x = stream.putFields();			stream.writeFields();		}		private void readObject(ObjectInputStream stream) 			throws IOException, ClassNotFoundException		{			stream.defaultReadObject();		}	}	private static class Test0014 // bogus reads/puts	implements Serializable	{		private static final long serialVersionUID = constantUID;		private int x = 11;		private int y = 12;		public String toString()		{			return (this.getClass().getName() + ": " +x+ "," +y);		}		private void writeObject(ObjectOutputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectOutputStream.PutField pf = stream.putFields();			pf.put("totallyBogus", true);			stream.writeFields();		}		private void readObject(ObjectInputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectInputStream.GetField gf = stream.readFields();			gf.get("totallyBogus", true);		}	}	private static class Test0015 // impossible field names	implements Serializable	{		private static final long serialVersionUID = constantUID;		private int x = 11;		private int y = 12;		private static final ObjectStreamField[] serialPersistentFields =		{			new ObjectStreamField("x", int.class),			new ObjectStreamField("No $!#%ing way!", String.class),			new ObjectStreamField("y", int.class),		};		public String toString()		{			return (this.getClass().getName() + ": " +x+ "," +y);		}		private void writeObject(ObjectOutputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectOutputStream.PutField pf = stream.putFields();			pf.put("No $!#%ing way!", "way");			stream.writeFields();		}		private void readObject(ObjectInputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectInputStream.GetField gf = stream.readFields();			gf.get("No $!#%ing way!", "way");		}	}	// XXX JDK1.4 and Kaffe differ on this one (JDK blows up with multiple 	// readFields()) both Kaffe and JDK1.4 are fine with multiple putFields.	private static class Test0016 // multiple gets/sets	implements Serializable	{		private static final long serialVersionUID = constantUID;		private int x = 11;		private int y = 12;		public String toString()		{			return (this.getClass().getName() + ": " +x+ "," +y);		}		private void writeObject(ObjectOutputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectOutputStream.PutField pf1 = stream.putFields();			ObjectOutputStream.PutField pf2 = stream.putFields();			if (pf1 != pf2)				throw new Error("Two different PutFields?!");			stream.writeFields();		}		private void readObject(ObjectInputStream stream) 			throws IOException, ClassNotFoundException		{			ObjectInputStream.GetField gf1 = stream.readFields();			ObjectInputStream.GetField gf2 = stream.readFields();			if (gf1 != gf2)				throw new Error("Two different GetFields?!");			this.x = gf1.get("x", 0);			this.y = gf1.get("y", 0);		}	}	private static void tryRead(Class c, byte[] bytes)	{		System.out.println(c.getName()+ ":");		try		{			System.out.println("  Deserializing from Test0001 stream...");			//System.out.println("Expecting a " +c.getName()+ " in stream...");			ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));			Object o = ois.readObject();			if (!c.isInstance(o))				throw new Exception("Object '" +o+ "' is not an instance of " +c);			System.out.println("    OK. Got: " +o);		}		catch (Exception e)		{			System.out.println("    Exception: " +e.getClass().getName());			if (DEBUG) e.printStackTrace(System.out);		}	}	private static ByteArrayOutputStream generateBytes(Class c)	{		try		{			System.out.println("  Serializing " +c.getName());			ByteArrayOutputStream bo = new ByteArrayOutputStream(256);			ObjectOutputStream oos = new ObjectOutputStream(bo);			oos.writeObject(c.newInstance());			oos.close();			System.out.println("    OK: " +bo.size()+ " bytes");			return bo;		}		catch (Exception e)		{			System.out.println("    Exception: " +e.getClass().getName());			if (DEBUG) e.printStackTrace(System.out);			return null;		}	}		private static void dumpBytes(byte[] b)	{		for (int i = 0; i < b.length; i++)		{			if ((0x00FF&b[i]) < 16)				System.out.print("0");			System.out.print(Integer.toHexString(b[i] & 0x00FF));			System.out.print(" ");			if (((i + 1) % 15) == 0)				System.out.println();		}		System.out.println();	}	private static byte[] mungeBytes(byte[] base, String srcStr, String replStr)	{		byte[] src = srcStr.getBytes();		byte[] repl = replStr.getBytes();				if (src.length != repl.length)			throw new Error("Can't handle different length src/repl ("+src+ ", "+repl+")");		for (int i = 0; i < base.length; i++)		{			if (i > base.length - src.length)				throw new Error("Didn't find it... ran out of room.");			// found start			if (base[i] == src[0])			{				//System.out.println("Checking " +i+ "...");				int s = 0;				while (s < src.length)				{					if (base[i+s] != src[s])						break;					s++;				}								// Found full match				if (s == src.length)				{					//System.out.println("Found src at " +i+ "...");					for (int r = 0; r < repl.length; r++)					{						base[i+r] = repl[r];					}					return base;				}			}		}		return null; // Never Reached	}	private static ByteArrayOutputStream canonicalOutput;	private static Class canonicalClass;	private static void checkClass(Class c)	{		// "fix" the canonical output so it looks like it was		// a serialization of the given class c.  Does the		// munge on a *copy* of the canonical bytes.		byte[] mungedOutput = mungeBytes(canonicalOutput.toByteArray(), 						 canonicalClass.getName(), 						 c.getName());				if (DEBUG) dumpBytes(mungedOutput);				// Expect an instance of c from mungedOutput		tryRead(c, mungedOutput);		// Hmm.. just for kicks		generateBytes(c);		System.out.println();	}	public static void main(String[] args)	{		// Canonical, correct format:		System.out.println("Generating canonical stream");		canonicalClass = Test0001.class;		canonicalOutput = generateBytes(Test0001.class);			String canonName = canonicalClass.getName();		String checkName = canonName.substring(0, canonName.length() - 4); // strip version no.			Class[] tests = TestSerialFields.class.getDeclaredClasses();		// XXX output assumes returned test array is consistently sorted...		for (int i = 0; i < tests.length; i++)		{			if (tests[i].getName().startsWith(checkName))				checkClass(tests[i]);		}	}}/* Expected Output:Generating canonical stream  Serializing TestSerialFields$Test0001    put x: int=42     put y: int=42     OK: 132 bytesTestSerialFields$Test0001:  Deserializing from Test0001 stream...    x: int=42    y: int=42    OK. Got: TestSerialFields$Test0001 x=0; y=0; foo=0  Serializing TestSerialFields$Test0001    put x: int=42     put y: int=42     OK: 132 bytesTestSerialFields$Test0002:  Deserializing from Test0001 stream...    x: int=42    y: int=42    xd: (defaulted) double=0.0     yd: (defaulted) double=0.0     OK. Got: TestSerialFields$Test0002 instance  Serializing TestSerialFields$Test0002    put xd: double=6.9     put yd: double=6.9     OK: 73 bytesTestSerialFields$Test0005:  Deserializing from Test0001 stream...    x: int=42    y: int=42    OK. Got: TestSerialFields$Test0005 instance  Serializing TestSerialFields$Test0005    OK: 47 bytesTestSerialFields$Test0008:  Deserializing from Test0001 stream...    x: int=42    y: int=42    OK. Got: TestSerialFields$Test0008 instance  Serializing TestSerialFields$Test0008    put x: int=42     put y: int=42     OK: 63 bytesTestSerialFields$Test0010:  Deserializing from Test0001 stream...    x: int=42    y: int=42    xodd: (defaulted) Object=null    yodd: (defaulted) Object=null    OK. Got: TestSerialFields$Test0010 instance  Serializing TestSerialFields$Test0010    put xodd: string='putfield test string'     put yodd: string='putfield test string'     OK: 133 bytesTestSerialFields$Test0011:  Deserializing from Test0001 stream...    OK. Got: TestSerialFields$Test0011 instance  Serializing TestSerialFields$Test0011    Exception: java.io.NotActiveExceptionTestSerialFields$Test0012:  Deserializing from Test0001 stream...    OK. Got: TestSerialFields$Test0012: 42,42  Serializing TestSerialFields$Test0012    OK: 63 bytesTestSerialFields$Test0014:  Deserializing from Test0001 stream...    Exception: java.lang.IllegalArgumentException  Serializing TestSerialFields$Test0014    Exception: java.lang.IllegalArgumentExceptionTestSerialFields$Test0015:  Deserializing from Test0001 stream...    OK. Got: TestSerialFields$Test0015: 0,0  Serializing TestSerialFields$Test0015    OK: 108 bytesTestSerialFields$Test0016:  Deserializing from Test0001 stream...    OK. Got: TestSerialFields$Test0016: 42,42  Serializing TestSerialFields$Test0016    OK: 63 bytes*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -