home *** CD-ROM | disk | FTP | other *** search
- package java.io;
-
- import java.util.Stack;
- import sun.io.ObjectOutputStreamDelegate;
-
- public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants {
- private boolean blockDataMode;
- private byte[] buf;
- private int count;
- private OutputStream out;
- private DataOutputStream dos;
- private IOException abortIOException;
- private Object[] wireHandle2Object;
- private int[] wireNextHandle;
- private int[] wireHash2Handle;
- private int nextWireOffset;
- private Object currentObject;
- private ObjectStreamClass currentClassDesc;
- private Stack classDescStack;
- boolean enableReplace;
- private Object[] replaceObjects;
- private int nextReplaceOffset;
- private int recursionDepth;
- private boolean isTrustedSubclass = false;
-
- public ObjectOutputStream(OutputStream var1) throws IOException {
- if (this instanceof ObjectOutputStreamDelegate && this.getClass().getClassLoader() == null) {
- this.isTrustedSubclass = true;
- } else {
- this.out = var1;
- this.dos = new DataOutputStream(this);
- this.buf = new byte[1024];
- this.writeStreamHeader();
- this.resetStream();
- }
- }
-
- public final void writeObject(Object var1) throws IOException {
- if (this.isTrustedSubclass) {
- ((ObjectOutputStreamDelegate)this).writeObjectDelegate(var1);
- } else {
- Object var2 = this.currentObject;
- ObjectStreamClass var3 = this.currentClassDesc;
- boolean var4 = this.setBlockData(false);
- ++this.recursionDepth;
-
- try {
- if (this.serializeNullAndRepeat(var1)) {
- return;
- }
-
- if (this.checkSpecialClasses(var1)) {
- return;
- }
-
- if (this.enableReplace) {
- Object var17 = this.replaceObject(var1);
- if (var1 != var17) {
- if (var17 != null && !(var17 instanceof Serializable)) {
- String var8 = var17.getClass().getName();
- throw new NotSerializableException(var8);
- }
-
- if (this.serializeNullAndRepeat(var17)) {
- this.addReplacement(var1, var17);
- return;
- }
-
- this.addReplacement(var1, var17);
- if (this.checkSpecialClasses(var17)) {
- return;
- }
-
- var1 = var17;
- }
- }
-
- if (this.checkSubstitutableSpecialClasses(var1)) {
- return;
- }
-
- this.outputObject(var1);
- } catch (ObjectStreamException var14) {
- ObjectStreamException var7 = var14;
- if (this.abortIOException == null) {
- try {
- this.setBlockData(false);
- this.writeCode(123);
- this.resetStream();
- this.writeObject(var7);
- this.resetStream();
- this.abortIOException = var7;
- } catch (IOException var13) {
- this.abortIOException = new StreamCorruptedException(((Throwable)var13).getMessage());
- }
- }
- } catch (IOException var15) {
- if (this.abortIOException == null) {
- this.abortIOException = var15;
- }
- } finally {
- --this.recursionDepth;
- this.currentObject = var2;
- this.currentClassDesc = var3;
- this.setBlockData(var4);
- }
-
- IOException var5 = this.abortIOException;
- if (this.recursionDepth == 0) {
- this.abortIOException = null;
- }
-
- if (var5 != null) {
- throw var5;
- }
- }
- }
-
- private boolean checkSpecialClasses(Object var1) throws IOException {
- if (var1 instanceof Class) {
- this.outputClass((Class)var1);
- return true;
- } else if (var1 instanceof ObjectStreamClass) {
- this.outputClassDescriptor((ObjectStreamClass)var1);
- return true;
- } else {
- return false;
- }
- }
-
- private boolean checkSubstitutableSpecialClasses(Object var1) throws IOException {
- if (var1 instanceof String) {
- this.outputString((String)var1);
- return true;
- } else if (var1.getClass().isArray()) {
- this.outputArray(var1);
- return true;
- } else {
- return false;
- }
- }
-
- public final void defaultWriteObject() throws IOException {
- if (this.isTrustedSubclass) {
- ((ObjectOutputStreamDelegate)this).defaultWriteObjectDelegate();
- } else if (this.currentObject != null && this.currentClassDesc != null) {
- if (this.currentClassDesc.getFieldSequence() != null) {
- boolean var1 = this.setBlockData(false);
- this.outputClassFields(this.currentObject, this.currentClassDesc.forClass(), this.currentClassDesc.getFieldSequence());
- this.setBlockData(var1);
- }
-
- } else {
- throw new NotActiveException("defaultWriteObject");
- }
- }
-
- public void reset() throws IOException {
- if (this.currentObject == null && this.currentClassDesc == null) {
- this.setBlockData(false);
- this.writeCode(121);
- this.resetStream();
- this.abortIOException = null;
- } else {
- throw new IOException("Illegal call to reset");
- }
- }
-
- private void resetStream() throws IOException {
- if (this.wireHandle2Object == null) {
- this.wireHandle2Object = new Object[100];
- this.wireNextHandle = new int[100];
- this.wireHash2Handle = new int[101];
- } else {
- for(int var1 = 0; var1 < this.nextWireOffset; ++var1) {
- this.wireHandle2Object[var1] = null;
- this.wireNextHandle[var1] = 0;
- }
- }
-
- this.nextWireOffset = 0;
-
- for(int var3 = 0; var3 < this.wireHash2Handle.length; ++var3) {
- this.wireHash2Handle[var3] = -1;
- }
-
- if (this.classDescStack == null) {
- this.classDescStack = new Stack();
- } else {
- this.classDescStack.setSize(0);
- }
-
- for(int var2 = 0; var2 < this.nextReplaceOffset; ++var2) {
- this.replaceObjects[var2] = null;
- }
-
- this.nextReplaceOffset = 0;
- this.setBlockData(true);
- }
-
- protected void annotateClass(Class var1) throws IOException {
- }
-
- protected Object replaceObject(Object var1) throws IOException {
- return var1;
- }
-
- protected final boolean enableReplaceObject(boolean var1) throws SecurityException {
- if (this.isTrustedSubclass) {
- return ((ObjectOutputStreamDelegate)this).enableReplaceObjectDelegate(var1);
- } else {
- boolean var2 = this.enableReplace;
- if (var1) {
- ClassLoader var3 = this.getClass().getClassLoader();
- if (var3 == null) {
- this.enableReplace = true;
- return var2;
- } else {
- throw new SecurityException("Not trusted class");
- }
- } else {
- this.enableReplace = false;
- return var2;
- }
- }
- }
-
- protected void writeStreamHeader() throws IOException {
- this.writeShort(-21267);
- this.writeShort(5);
- }
-
- private void outputString(String var1) throws IOException {
- this.assignWireOffset(var1);
- this.writeCode(116);
- this.writeUTF(var1);
- }
-
- private void outputClass(Class var1) throws IOException {
- this.writeCode(118);
- ObjectStreamClass var2 = ObjectStreamClass.lookup(var1);
- if (var2 == null) {
- throw new NotSerializableException(var1.getName());
- } else {
- this.outputClassDescriptor(var2);
- this.assignWireOffset(var1);
- }
- }
-
- private void outputClassDescriptor(ObjectStreamClass var1) throws IOException {
- if (!this.serializeNullAndRepeat(var1)) {
- this.writeCode(114);
- String var2 = var1.getName();
- this.writeUTF(var2);
- this.writeLong(var1.getSerialVersionUID());
- this.assignWireOffset(var1);
- var1.write(this);
- boolean var3 = this.setBlockData(true);
- this.annotateClass(var1.forClass());
- this.setBlockData(var3);
- this.writeCode(120);
- ObjectStreamClass var4 = var1.getSuperclass();
- this.outputClassDescriptor(var4);
- }
- }
-
- private void outputArray(Object var1) throws IOException {
- Class var2 = var1.getClass();
- ObjectStreamClass var3 = ObjectStreamClass.lookup(var2);
- this.writeCode(117);
- this.outputClassDescriptor(var3);
- this.assignWireOffset(var1);
- Class var6 = var2.getComponentType();
- if (var6.isPrimitive()) {
- if (var6 == Integer.TYPE) {
- int[] var30 = (int[])var1;
- int var22 = var30.length;
- this.writeInt(var22);
-
- for(int var14 = 0; var14 < var22; ++var14) {
- this.writeInt(var30[var14]);
- }
-
- } else if (var6 == Byte.TYPE) {
- byte[] var29 = (byte[])var1;
- int var21 = var29.length;
- this.writeInt(var21);
- this.writeInternal(var29, 0, var21, true);
- } else if (var6 == Long.TYPE) {
- long[] var28 = (long[])var1;
- int var20 = var28.length;
- this.writeInt(var20);
-
- for(int var13 = 0; var13 < var20; ++var13) {
- this.writeLong(var28[var13]);
- }
-
- } else if (var6 == Float.TYPE) {
- float[] var27 = (float[])var1;
- int var19 = var27.length;
- this.writeInt(var19);
-
- for(int var12 = 0; var12 < var19; ++var12) {
- this.writeFloat(var27[var12]);
- }
-
- } else if (var6 == Double.TYPE) {
- double[] var26 = (double[])var1;
- int var18 = var26.length;
- this.writeInt(var18);
-
- for(int var11 = 0; var11 < var18; ++var11) {
- this.writeDouble(var26[var11]);
- }
-
- } else if (var6 == Short.TYPE) {
- short[] var25 = (short[])var1;
- int var17 = var25.length;
- this.writeInt(var17);
-
- for(int var10 = 0; var10 < var17; ++var10) {
- this.writeShort(var25[var10]);
- }
-
- } else if (var6 == Character.TYPE) {
- char[] var24 = (char[])var1;
- int var16 = var24.length;
- this.writeInt(var16);
-
- for(int var9 = 0; var9 < var16; ++var9) {
- this.writeChar(var24[var9]);
- }
-
- } else if (var6 != Boolean.TYPE) {
- throw new InvalidClassException(var2.getName());
- } else {
- boolean[] var23 = (boolean[])var1;
- int var15 = var23.length;
- this.writeInt(var15);
-
- for(int var8 = 0; var8 < var15; ++var8) {
- this.writeBoolean(var23[var8]);
- }
-
- }
- } else {
- Object[] var7 = var1;
- int var5 = var7.length;
- this.writeInt(var5);
-
- for(int var4 = 0; var4 < var5; ++var4) {
- this.writeObject(var7[var4]);
- }
-
- }
- }
-
- private void outputObject(Object var1) throws IOException {
- this.currentObject = var1;
- Class var2 = var1.getClass();
- this.currentClassDesc = ObjectStreamClass.lookup(var2);
- if (this.currentClassDesc == null) {
- throw new NotSerializableException(var2.getName());
- } else {
- this.writeCode(115);
- this.outputClassDescriptor(this.currentClassDesc);
- this.assignWireOffset(var1);
- if (this.currentClassDesc.isExternalizable()) {
- Externalizable var9 = (Externalizable)var1;
- var9.writeExternal(this);
- } else {
- int var3 = this.classDescStack.size();
-
- try {
- ObjectStreamClass var6;
- while((var6 = this.currentClassDesc.getSuperclass()) != null) {
- this.classDescStack.push(this.currentClassDesc);
- this.currentClassDesc = var6;
- }
-
- do {
- if (this.currentClassDesc.hasWriteObject()) {
- this.setBlockData(true);
- this.invokeObjectWriter(var1, this.currentClassDesc.forClass());
- this.setBlockData(false);
- this.writeCode(120);
- } else {
- this.defaultWriteObject();
- }
- } while(this.classDescStack.size() > var3 && (this.currentClassDesc = (ObjectStreamClass)this.classDescStack.pop()) != null);
- } finally {
- this.classDescStack.setSize(var3);
- }
-
- }
- }
- }
-
- private boolean serializeNullAndRepeat(Object var1) throws IOException {
- if (var1 == null) {
- this.writeCode(112);
- return true;
- } else {
- if (this.replaceObjects != null) {
- for(int var2 = 0; var2 < this.nextReplaceOffset; var2 += 2) {
- if (this.replaceObjects[var2] == var1) {
- var1 = this.replaceObjects[var2 + 1];
- break;
- }
- }
- }
-
- int var3 = this.findWireOffset(var1);
- if (var3 >= 0) {
- this.writeCode(113);
- this.writeInt(var3 + 8257536);
- return true;
- } else {
- return false;
- }
- }
- }
-
- private int findWireOffset(Object var1) {
- int var2 = System.identityHashCode(var1);
- int var3 = (var2 & Integer.MAX_VALUE) % this.wireHash2Handle.length;
-
- for(int var4 = this.wireHash2Handle[var3]; var4 >= 0; var4 = this.wireNextHandle[var4]) {
- if (this.wireHandle2Object[var4] == var1) {
- return var4;
- }
- }
-
- return -1;
- }
-
- private void assignWireOffset(Object var1) throws IOException {
- if (this.nextWireOffset == this.wireHandle2Object.length) {
- Object[] var2 = this.wireHandle2Object;
- this.wireHandle2Object = new Object[this.nextWireOffset * 2];
- System.arraycopy(var2, 0, this.wireHandle2Object, 0, this.nextWireOffset);
- int[] var3 = this.wireNextHandle;
- this.wireNextHandle = new int[this.nextWireOffset * 2];
- System.arraycopy(var3, 0, this.wireNextHandle, 0, this.nextWireOffset);
- }
-
- this.wireHandle2Object[this.nextWireOffset] = var1;
- this.hashInsert(var1, this.nextWireOffset);
- ++this.nextWireOffset;
- }
-
- private void hashInsert(Object var1, int var2) {
- int var3 = System.identityHashCode(var1);
- int var4 = (var3 & Integer.MAX_VALUE) % this.wireHash2Handle.length;
- this.wireNextHandle[var2] = this.wireHash2Handle[var4];
- this.wireHash2Handle[var4] = var2;
- }
-
- private void addReplacement(Object var1, Object var2) {
- if (this.replaceObjects == null) {
- this.replaceObjects = new Object[10];
- }
-
- if (this.nextReplaceOffset == this.replaceObjects.length) {
- Object[] var3 = this.replaceObjects;
- this.replaceObjects = new Object[2 + this.nextReplaceOffset * 2];
- System.arraycopy(var3, 0, this.replaceObjects, 0, this.nextReplaceOffset);
- }
-
- this.replaceObjects[this.nextReplaceOffset++] = var1;
- this.replaceObjects[this.nextReplaceOffset++] = var2;
- }
-
- private void writeCode(int var1) throws IOException {
- this.writeByte(var1);
- }
-
- public void write(int var1) throws IOException {
- if (this.count >= this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)var1;
- }
-
- public void write(byte[] var1) throws IOException {
- this.write(var1, 0, var1.length);
- }
-
- private void writeInternal(byte[] var1, int var2, int var3, boolean var4) throws IOException {
- if (var3 < 0) {
- throw new IndexOutOfBoundsException();
- } else {
- int var5 = this.buf.length - this.count;
- if (var3 <= var5) {
- System.arraycopy(var1, var2, this.buf, this.count, var3);
- this.count += var3;
- } else if (!this.blockDataMode && var4) {
- this.bufferedWrite(var1, var2, var3);
- } else {
- this.drain();
- if (this.blockDataMode) {
- if (var3 <= 255) {
- this.out.write(119);
- this.out.write(var3);
- } else {
- this.out.write(122);
- this.out.write(var3 >> 24 & 255);
- this.out.write(var3 >> 16 & 255);
- this.out.write(var3 >> 8 & 255);
- this.out.write(var3 & 255);
- }
- }
-
- this.out.write(var1, var2, var3);
- }
- }
- }
-
- public void flush() throws IOException {
- this.drain();
- this.out.flush();
- }
-
- protected void drain() throws IOException {
- if (this.count != 0) {
- if (this.blockDataMode) {
- if (this.count <= 255) {
- this.out.write(119);
- this.out.write(this.count);
- } else {
- this.out.write(122);
- this.out.write(this.count >> 24 & 255);
- this.out.write(this.count >> 16 & 255);
- this.out.write(this.count >> 8 & 255);
- this.out.write(this.count & 255);
- }
- }
-
- this.out.write(this.buf, 0, this.count);
- this.count = 0;
- }
- }
-
- public void close() throws IOException {
- this.flush();
- this.out.close();
- }
-
- private boolean setBlockData(boolean var1) throws IOException {
- if (this.blockDataMode == var1) {
- return var1;
- } else {
- this.drain();
- this.blockDataMode = var1;
- return !var1;
- }
- }
-
- public void writeBoolean(boolean var1) throws IOException {
- if (this.count >= this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)(var1 ? 1 : 0);
- }
-
- public void write(byte[] var1, int var2, int var3) throws IOException {
- this.writeInternal(var1, var2, var3, false);
- }
-
- private void bufferedWrite(byte[] var1, int var2, int var3) throws IOException {
- int var4 = this.buf.length - this.count;
- int var5 = var3;
- if (var3 > var4) {
- System.arraycopy(var1, var2, this.buf, this.count, var4);
- var2 += var4;
- var5 = var3 - var4;
- this.out.write(this.buf, 0, this.buf.length);
-
- for(this.count = 0; var5 >= this.buf.length; var5 -= this.buf.length) {
- System.arraycopy(var1, var2, this.buf, 0, this.buf.length);
- this.out.write(this.buf, 0, this.buf.length);
- var2 += this.buf.length;
- }
- }
-
- if (var5 != 0) {
- System.arraycopy(var1, var2, this.buf, this.count, var5);
- this.count += var5;
- }
-
- }
-
- public void writeByte(int var1) throws IOException {
- if (this.count >= this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)var1;
- }
-
- public void writeShort(int var1) throws IOException {
- if (this.count + 2 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)(var1 >>> 8);
- this.buf[this.count++] = (byte)var1;
- }
-
- public void writeChar(int var1) throws IOException {
- if (this.count + 2 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)(var1 >>> 8);
- this.buf[this.count++] = (byte)var1;
- }
-
- public void writeInt(int var1) throws IOException {
- if (this.count + 4 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)(var1 >>> 24);
- this.buf[this.count++] = (byte)(var1 >>> 16);
- this.buf[this.count++] = (byte)(var1 >>> 8);
- this.buf[this.count++] = (byte)var1;
- }
-
- public void writeLong(long var1) throws IOException {
- if (this.count + 8 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)((int)(var1 >>> 56));
- this.buf[this.count++] = (byte)((int)(var1 >>> 48));
- this.buf[this.count++] = (byte)((int)(var1 >>> 40));
- this.buf[this.count++] = (byte)((int)(var1 >>> 32));
- this.buf[this.count++] = (byte)((int)(var1 >>> 24));
- this.buf[this.count++] = (byte)((int)(var1 >>> 16));
- this.buf[this.count++] = (byte)((int)(var1 >>> 8));
- this.buf[this.count++] = (byte)((int)var1);
- }
-
- public void writeFloat(float var1) throws IOException {
- int var2 = Float.floatToIntBits(var1);
- if (this.count + 4 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)(var2 >>> 24);
- this.buf[this.count++] = (byte)(var2 >>> 16);
- this.buf[this.count++] = (byte)(var2 >>> 8);
- this.buf[this.count++] = (byte)var2;
- }
-
- public void writeDouble(double var1) throws IOException {
- long var3 = Double.doubleToLongBits(var1);
- if (this.count + 8 > this.buf.length) {
- this.drain();
- }
-
- this.buf[this.count++] = (byte)((int)(var3 >>> 56));
- this.buf[this.count++] = (byte)((int)(var3 >>> 48));
- this.buf[this.count++] = (byte)((int)(var3 >>> 40));
- this.buf[this.count++] = (byte)((int)(var3 >>> 32));
- this.buf[this.count++] = (byte)((int)(var3 >>> 24));
- this.buf[this.count++] = (byte)((int)(var3 >>> 16));
- this.buf[this.count++] = (byte)((int)(var3 >>> 8));
- this.buf[this.count++] = (byte)((int)var3);
- }
-
- public void writeBytes(String var1) throws IOException {
- this.dos.writeBytes(var1);
- }
-
- public void writeChars(String var1) throws IOException {
- this.dos.writeChars(var1);
- }
-
- public void writeUTF(String var1) throws IOException {
- this.dos.writeUTF(var1);
- }
-
- private native void outputClassFields(Object var1, Class var2, int[] var3) throws IOException, InvalidClassException;
-
- private native boolean invokeObjectWriter(Object var1, Class var2) throws IOException;
- }
-