objectinputstream.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,655 行 · 第 1/4 页

JAVA
1,655
字号
		if (switchmode)
			oldmode = setBlockDataMode(true);
		float value = this.dataInputStream.readFloat();
		if (switchmode)
			setBlockDataMode(oldmode);
		return value;
	}

	public double readDouble() throws IOException {
		boolean switchmode = true;
		boolean oldmode = this.readDataFromBlock;
		if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8)
			switchmode = false;
		if (switchmode)
			oldmode = setBlockDataMode(true);
		double value = this.dataInputStream.readDouble();
		if (switchmode)
			setBlockDataMode(oldmode);
		return value;
	}

	public void readFully(byte data[]) throws IOException {
		this.dataInputStream.readFully(data);
	}

	public void readFully(byte data[], int offset, int size)
		throws IOException {
		this.dataInputStream.readFully(data, offset, size);
	}

	public int skipBytes(int len) throws IOException {
		return this.dataInputStream.skipBytes(len);
	}

	/**
	   @deprecated
	   @see java.io.DataInputStream#readLine ()
	*/
	public String readLine() throws IOException {
		return this.dataInputStream.readLine();
	}

	public String readUTF() throws IOException {
		return this.dataInputStream.readUTF();
	}

	/**
	   This class allows a class to specify exactly which fields should
	   be read, and what values should be read for these fields.
	
	   XXX: finish up comments
	*/
	public static abstract class GetField {
		public abstract ObjectStreamClass getObjectStreamClass();

		public abstract boolean defaulted(String name)
			throws IOException, IllegalArgumentException;

		public abstract boolean get(String name, boolean defvalue)
			throws IOException, IllegalArgumentException;

		public abstract char get(String name, char defvalue)
			throws IOException, IllegalArgumentException;

		public abstract byte get(String name, byte defvalue)
			throws IOException, IllegalArgumentException;

		public abstract short get(String name, short defvalue)
			throws IOException, IllegalArgumentException;

		public abstract int get(String name, int defvalue)
			throws IOException, IllegalArgumentException;

		public abstract long get(String name, long defvalue)
			throws IOException, IllegalArgumentException;

		public abstract float get(String name, float defvalue)
			throws IOException, IllegalArgumentException;

		public abstract double get(String name, double defvalue)
			throws IOException, IllegalArgumentException;

		public abstract Object get(String name, Object defvalue)
			throws IOException, IllegalArgumentException;
	}

	public GetField readFields()
		throws IOException, ClassNotFoundException, NotActiveException {
		if (this.currentObject == null
			|| this.currentObjectStreamClass == null)
			throw new NotActiveException("readFields called by non-active class and/or object");

		if (fieldsAlreadyRead)
			throw new NotActiveException("readFields called but fields already read from stream (by defaultReadObject or readFields)");

		final ObjectStreamClass clazz = this.currentObjectStreamClass;
		final byte[] prim_field_data = new byte[clazz.primFieldSize];
		final Object[] objs = new Object[clazz.objectFieldCount];

		// Apparently Block data is not used with GetField as per
		// empirical evidence against JDK 1.2.  Also see Mauve test
		// java.io.ObjectInputOutput.Test.GetPutField.
		boolean oldmode = setBlockDataMode(false);
		readFully(prim_field_data);
		for (int i = 0; i < objs.length; ++i)
			objs[i] = readObject();
		setBlockDataMode(oldmode);

		return new GetField() {
			public ObjectStreamClass getObjectStreamClass() {
				return clazz;
			}

			public boolean defaulted(String name)
				throws IOException, IllegalArgumentException {
				return clazz.getField(name) == null;
			}

			public boolean get(String name, boolean defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Boolean.TYPE);

				if (field == null)
					return defvalue;

				return prim_field_data[field.getOffset()] == 0 ? false : true;
			}

			public char get(String name, char defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Character.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return (char)
					(((prim_field_data[off++] & 0xFF) << 8)
						| (prim_field_data[off] & 0xFF));
			}

			public byte get(String name, byte defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Byte.TYPE);

				if (field == null)
					return defvalue;

				return prim_field_data[field.getOffset()];
			}

			public short get(String name, short defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Short.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return (short)
					(((prim_field_data[off++] & 0xFF) << 8)
						| (prim_field_data[off] & 0xFF));
			}

			public int get(String name, int defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Integer.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return ((prim_field_data[off++] & 0xFF) << 24)
					| ((prim_field_data[off++] & 0xFF) << 16)
					| ((prim_field_data[off++] & 0xFF) << 8)
					| (prim_field_data[off] & 0xFF);
			}

			public long get(String name, long defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Long.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return (long)
					(((prim_field_data[off++] & 0xFF) << 56)
						| ((prim_field_data[off++] & 0xFF) << 48)
						| ((prim_field_data[off++] & 0xFF) << 40)
						| ((prim_field_data[off++] & 0xFF) << 32)
						| ((prim_field_data[off++] & 0xFF) << 24)
						| ((prim_field_data[off++] & 0xFF) << 16)
						| ((prim_field_data[off++] & 0xFF) << 8)
						| (prim_field_data[off] & 0xFF));
			}

			public float get(String name, float defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Float.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return Float.intBitsToFloat(
					((prim_field_data[off++] & 0xFF) << 24)
						| ((prim_field_data[off++] & 0xFF) << 16)
						| ((prim_field_data[off++] & 0xFF) << 8)
						| (prim_field_data[off] & 0xFF));
			}

			public double get(String name, double defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field = getField(name, Double.TYPE);

				if (field == null)
					return defvalue;

				int off = field.getOffset();

				return Double.longBitsToDouble(
					(long) (((prim_field_data[off++] & 0xFF) << 56)
						| ((prim_field_data[off++] & 0xFF) << 48)
						| ((prim_field_data[off++] & 0xFF) << 40)
						| ((prim_field_data[off++] & 0xFF) << 32)
						| ((prim_field_data[off++] & 0xFF) << 24)
						| ((prim_field_data[off++] & 0xFF) << 16)
						| ((prim_field_data[off++] & 0xFF) << 8)
						| (prim_field_data[off] & 0xFF)));
			}

			public Object get(String name, Object defvalue)
				throws IOException, IllegalArgumentException {
				ObjectStreamField field =
					getField(
						name,
						defvalue == null ? null : defvalue.getClass());

				if (field == null)
					return defvalue;

				return objs[field.getOffset()];
			}

			private ObjectStreamField getField(String name, Class type)
				throws IllegalArgumentException {
				ObjectStreamField field = clazz.getField(name);

				if (field == null)
					return null;

				Class field_type = field.getType();

				if (type == field_type
					|| (type == null && !field_type.isPrimitive()))
					return field;

				throw new IllegalArgumentException(
					"Field requested is of type "
						+ field_type.getName()
						+ ", but requested type was "
						+ (type == null ? "Object" : type.getName()));
			}
		};

	}

	/**
	   Protected constructor that allows subclasses to override
	   deserialization.  This constructor should be called by subclasses
	   that wish to override <code>readObject (Object)</code>.  This
	   method does a security check <i>NOTE: currently not
	   implemented</i>, then sets a flag that informs
	   <code>readObject (Object)</code> to call the subclasses
	   <code>readObjectOverride (Object)</code> method.
	
	   @see readObjectOverride (Object)
	*/
	protected ObjectInputStream() throws IOException, SecurityException {
		SecurityManager sec_man = System.getSecurityManager();
		if (sec_man != null)
			sec_man.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
		this.useSubclassMethod = true;
	}

	/**
	   This method allows subclasses to override the default
	   de serialization mechanism provided by
	   <code>ObjectInputStream</code>.  To make this method be used for
	   writing objects, subclasses must invoke the 0-argument
	   constructor on this class from their constructor.
	
	   @see ObjectInputStream ()
	*/
	protected Object readObjectOverride()
		throws ClassNotFoundException, IOException, OptionalDataException {
		throw new IOException("Subclass of ObjectInputStream must implement readObjectOverride");
	}

	// assigns the next availible handle to OBJ
	private int assignNewHandle(Object obj) {
		this.objectLookupTable.put(
			new Integer(this.nextOID),
			new ObjectIdentityWrapper(obj));

		//    try
		//    {
		//      DEBUG ("Assigning handle " + this.nextOID);
		//      DEBUGln (" to " + obj);
		//    }
		//    catch (Throwable t) {}

		return this.nextOID++;
	}

	private Object processResolution(Object obj, int handle)
		throws IOException {
		if (obj instanceof Serializable) {
			Method m = null;
			try {
				Class classArgs[] = {
				};
				m = obj.getClass().getDeclaredMethod("readResolve", classArgs);
				// m can't be null by definition since an exception would
				// have been thrown so a check for null is not needed.
				obj = m.invoke(obj, new Object[] {
				});
			} catch (NoSuchMethodException ignore) {
			} catch (IllegalAccessException ignore) {
			} catch (InvocationTargetException ignore) {
			}
		}

		if (this.resolveEnabled)
			obj = resolveObject(obj);

		this.objectLookupTable.put(
			new Integer(handle),
			new ObjectIdentityWrapper(obj));

		return obj;
	}

	private void clearHandles() {
		this.objectLookupTable.clear();
		this.nextOID = baseWireHandle;
	}

	private void readNextBlock() throws IOException {
		//  DEBUGln ("In readNextBlock ");
		readNextBlock(this.realInputStream.readByte());
	}

	private void readNextBlock(byte marker) throws IOException {
		if (marker == TC_BLOCKDATA) {
			dumpElement("BLOCK DATA SIZE=");
			this.blockDataBytes = this.realInputStream.readUnsignedByte();
			dumpElementln(Integer.toString(this.blockDataBytes));
		} else if (marker == TC_BLOCKDATALONG) {
			dumpElement("BLOCK DATA LONG SIZE=");
			this.blockDataBytes = this.realInputStream.readInt();
			dumpElementln(Integer.toString(this.blockDataBytes));
		} else {
			throw new EOFException("Attempt to read primitive data, but no data block is active.");
		}

		if (this.blockData.length < this.blockDataBytes)
			this.blockData = new byte[this.blockDataBytes];

		this.realInputStream.readFully(this.blockData, 0, this.blockDataBytes);
		this.blockDataPosition = 0;
	}

	private void readArrayElements(Object array, Class clazz)
		throws ClassNotFoundException, IOException {
		if (clazz.isPrimitive()) {
			if (clazz == Boolean.TYPE) {
				boolean[] cast_array = (boolean[]) array;
				for (int i = 0; i < cast_array.length; i++)
					cast_array[i] = this.realInputStream.readBoolean();
				return;
			}
			if (clazz == Byte.TYPE) {
				byte[] cast_array = (byte[]) array;
				for (int i = 0; i < cast_array.length; i++)
					cast_array[i] = this.realInputStream.readByte();
				return;
			}
			if (clazz == Character.TYPE) {
				char[] cast_array = (char[]) array;
				for (int i = 0; i < cast_array.length; i++)
					cast_array[i] = this.realInputStream.readChar();
				return;
			}
			if (clazz == Double.TYPE) {
				double[] cast_array = (double[]) array;
				for (int i = 0; i < cast_array.length; i++)
					cast_array[i] = this.realInputStream.readDouble();
				return;
			}
			if (clazz == Float.TYPE) {
				float[] cast_array = (float[]) array;
				for (int i = 0; i < cast_array.length; i++)
					cast_array[i] = this.realInputStream.readFloat();
				return;
			}
			if (clazz == Integer.TYPE) {
				int[] cast_array = (int[]) array;

⌨️ 快捷键说明

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