📄 objectinputstream.java
字号:
return length; } else return this.realInputStream.read(data, offset, length); } public int available() throws IOException { if (this.readDataFromBlock) { if (this.blockDataPosition >= this.blockDataBytes) readNextBlock (); return this.blockDataBytes - this.blockDataPosition; } else return this.realInputStream.available(); } public void close() throws IOException { this.realInputStream.close(); } public boolean readBoolean() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1) switchmode = false; if (switchmode) oldmode = setBlockDataMode (true); boolean value = this.dataInputStream.readBoolean (); if (switchmode) setBlockDataMode (oldmode); return value; } public byte readByte() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); byte value = this.dataInputStream.readByte(); if (switchmode) setBlockDataMode(oldmode); return value; } public int readUnsignedByte() throws IOException { boolean switchmode = true; boolean oldmode = this.readDataFromBlock; if (!oldmode || this.blockDataBytes - this.blockDataPosition >= 1) switchmode = false; if (switchmode) oldmode = setBlockDataMode(true); int value = this.dataInputStream.readUnsignedByte(); if (switchmode) 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 abstract static 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++] & 0xFFL) << 56) | ((prim_field_data[off++] & 0xFFL) << 48) | ((prim_field_data[off++] & 0xFFL) << 40) | ((prim_field_data[off++] & 0xFFL) << 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++] & 0xFFL) << 56) | ((prim_field_data[off++] & 0xFFL) << 48) | ((prim_field_data[off++] & 0xFFL) << 40) | ((prim_field_data[off++] & 0xFFL) << 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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -