Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.objenesis.instantiator.basic;
  
 
Instantiates a class by using a dummy input stream that always feeds data for an empty object of the same kind. NOTE: This instantiator may not work properly if the class being instantiated defines a "readResolve" method, since it may return objects that have been returned previously (i.e., there's no guarantee that the returned object is a new one), or even objects from a completely different class.

Author(s):
Leonardo Mesquita
See also:
org.objenesis.instantiator.ObjectInstantiator
 
 public class ObjectInputStreamInstantiator implements ObjectInstantiator {
    private static class MockStream extends InputStream {
 
       private int pointer;
       private byte[] data;
       private int sequence;
       private static final int[] NEXT = new int[] {1, 2, 2};
       private byte[][] buffers;
 
       private final byte[] FIRST_DATA;
       private static byte[] HEADER;
       private static byte[] REPEATING_DATA;
 
       static {
          initialize();
       }
 
       private static void initialize() {
          try {
             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
             DataOutputStream dout = new DataOutputStream(byteOut);
             dout.writeShort(.);
             dout.writeShort(.);
              = byteOut.toByteArray();
 
             byteOut = new ByteArrayOutputStream();
             dout = new DataOutputStream(byteOut);
 
             dout.writeByte(.);
             dout.writeByte(.);
             dout.writeInt(.);
              = byteOut.toByteArray();
          }
          catch(IOException e) {
             throw new Error("IOException: " + e.getMessage());
          }
 
       }
 
       public MockStream(Class clazz) {
          this. = 0;
          this. = 0;
          this. = ;
 
          // (byte) TC_OBJECT
          // (byte) TC_CLASSDESC
          // (short length)
          // (byte * className.length)
          // (long)serialVersionUID
          // (byte) SC_SERIALIZABLE
          // (short)0 <fields>
          // TC_ENDBLOCKDATA
          // TC_NULL
          ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
          DataOutputStream dout = new DataOutputStream(byteOut);
          try {
             dout.writeByte(.);
             dout.writeByte(.);
             dout.writeUTF(clazz.getName());
             dout.writeLong(ObjectStreamClass.lookup(clazz).getSerialVersionUID());
             dout.writeByte(.);
             dout.writeShort((short) 0); // Zero fields
             dout.writeByte(.);
             dout.writeByte(.);
          }
          catch(IOException e) {
             throw new Error("IOException: " + e.getMessage());
          }
          this. = byteOut.toByteArray();
           = new byte[][] {};
       }
 
       private void advanceBuffer() {
           = 0;
          = [];
          = [];
      }
      public int read() throws IOException {
         int result = [++];
         if( >= .) {
            advanceBuffer();
         }
         return result;
      }
      public int available() throws IOException {
         return .;
      }
      public int read(byte[] bint offint lenthrows IOException {
         int left = len;
         int remaining = . - ;
         while(remaining <= left) {
            System.arraycopy(boffremaining);
            off += remaining;
            left -= remaining;
            advanceBuffer();
            remaining = . - ;
         }
         if(left > 0) {
            System.arraycopy(boffleft);
             += left;
         }
         return len;
      }
   }
   public ObjectInputStreamInstantiator(Class clazz) {
      if(Serializable.class.isAssignableFrom(clazz)) {
         try {
            this. = new ObjectInputStream(new MockStream(clazz));
         }
         catch(IOException e) {
            throw new Error("IOException: " + e.getMessage());
         }
      }
      else {
    	  throw new ObjenesisException(new NotSerializableException(clazz+" not serializable"));
      }
   }
   public Object newInstance() {
      try {
         return .readObject();
      }      
      catch(ClassNotFoundException e) {
         throw new Error("ClassNotFoundException: " + e.getMessage());
      }
      catch(Exception e) {
         throw new ObjenesisException(e);
      }
   }
New to GrepCode? Check out our FAQ X