objectinputstream.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 1,847 行 · 第 1/4 页
JAVA
1,847 行
setBlockDataMode(oldmode); return value; } public short readShort() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); short value = this.dataInputStream.readShort(); if (switchmode) setBlockDataMode(oldmode); return value; } public int readUnsignedShort() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); int value = this.dataInputStream.readUnsignedShort(); if (switchmode) setBlockDataMode(oldmode); return value; } public char readChar() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 2) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); char value = this.dataInputStream.readChar(); if (switchmode) setBlockDataMode(oldmode); return value; } public int readInt() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); int value = this.dataInputStream.readInt(); if (switchmode) setBlockDataMode(oldmode); return value; } public long readLong() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 8) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); long value = this.dataInputStream.readLong(); if (switchmode) setBlockDataMode(oldmode); return value; } public float readFloat() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 4) switchmode = false; 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; } /** * This method should be called by a method called 'readObject' in the * deserializing class (if present). It cannot (and should not)be called * outside of it. Its goal is to read all fields in the real input stream * and keep them accessible through the {@link #GetField} class. Calling * this method will not alter the deserializing object. * * @return A valid freshly created 'GetField' instance to get access to * the deserialized stream. * @throws IOException An input/output exception occured. * @throws ClassNotFoundException * @throws NotActiveException */ 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 (prereadFields != null) return prereadFields; 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); prereadFields = new GetField() { public ObjectStreamClass getObjectStreamClass() { return clazz; } public boolean defaulted(String name) throws IOException, IllegalArgumentException { ObjectStreamField f = clazz.getField(name); /* First if we have a serialized field use the descriptor */ if (f != null) { /* It is in serialPersistentFields but setClass tells us * it should not be set. This value is defaulted. */ if (f.isPersistent() && !f.isToSet()) return true; return false; } /* This is not a serialized field. There should be * a default value only if the field really exists. */ try { return (clazz.forClass().getDeclaredField (name) != null); } catch (NoSuchFieldException e) { throw new IllegalArgumentException(e.getMessage()); } } 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); boolean illegal = false; try { try { Class field_type = field.getType(); if (type == field_type || (type == null && !field_type.isPrimitive())) { /* See defaulted */ return field; } illegal = true; throw new IllegalArgumentException ("Field requested is of type " + field_type.getName() + ", but requested type was " + (type == null ? "Object" : type.getName())); } catch (NullPointerException _) { /* Here we catch NullPointerException, because it may only come from the call 'field.getType()'. If field is null, we have to return null and classpath ethic say we must try to avoid 'if (xxx == null)'. */ } catch (IllegalArgumentException e) { throw e; } return null; } finally { /* If this is an unassigned field we should return * the default value. */ if (!illegal && field != null && !field.isToSet() && field.isPersistent()) return null; /* We do not want to modify transient fields. They should * be left to 0. */ try { Field f = clazz.forClass().getDeclaredField(name); if (Modifier.isTransient(f.getModifiers())) throw new IllegalArgumentException ("no such field (non transient) " + name); if (field == null && f.getType() != type) throw new IllegalArgumentException ("Invalid requested type for field " + name); } catch (NoSuchFieldException e) { if (field == null) throw new IllegalArgumentException(e.getMessage()); } } } }; fieldsAlreadyRead = true; return prereadFields; } /** * Protected constructor that allows subclasses to override
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?