Open Source Repository

Home /hibernate/hibernate-3.2.7.ga | Repository Home



org/hibernate/bytecode/util/ByteCodeHelper.java
package org.hibernate.bytecode.util;

import java.io.InputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.ByteArrayOutputStream;
import java.io.BufferedInputStream;
import java.util.zip.ZipInputStream;

/**
 * A helper for reading byte code from various input sources.
 *
 @author Steve Ebersole
 */
public class ByteCodeHelper {
  private ByteCodeHelper() {
  }

  /**
   * Reads class byte array info from the given input stream.
   <p/>
   * The stream is closed within this method!
   *
   @param inputStream
   @return
   @throws IOException
   */
  public static byte[] readByteCode(InputStream inputStreamthrows IOException {
    if inputStream == null ) {
      throw new IOException"null input stream" );
    }

    byte[] buffer = new byte[409600];
    byte[] classBytes = new byte[0];
    int r = 0;

    try {
      r = inputStream.readbuffer );
      while r >= buffer.length ) {
        byte[] temp = new byteclassBytes.length + buffer.length ];
        System.arraycopyclassBytes, 0, temp, 0, classBytes.length );
        System.arraycopybuffer, 0, temp, classBytes.length, buffer.length );
        classBytes = temp;
      }
      if r != -) {
        byte[] temp = new byteclassBytes.length + r ];
        System.arraycopyclassBytes, 0, temp, 0, classBytes.length );
        System.arraycopybuffer, 0, temp, classBytes.length, r );
        classBytes = temp;
      }
    }
    finally {
      try {
        inputStream.close();
      }
      catch (IOException ignore) {
        // intentionally empty
      }
    }

    return classBytes;
  }

  public static byte[] readByteCode(File filethrows IOException {
    return ByteCodeHelper.readByteCodenew FileInputStreamfile ) );
  }

  public static byte[] readByteCode(ZipInputStream zipthrows IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        InputStream in = new BufferedInputStreamzip );
        int b;
        while ( ( b = in.read() ) != -) {
            bout.write);
        }
        return bout.toByteArray();
    }
}