package org.k13n.swtstubber.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

  public static byte[] writeStreamToArray(InputStream stream)
      throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = stream.read(buffer)) != -1)
      baos.write(buffer, 0, length);
    return baos.toByteArray();
  }

}

@@ -66,16 +66,70 @@
 Stream;%0A
+import java.io.File;%0Aimport java.io.FileOutputStream;%0A
 import j
@@ -531,10 +531,1335 @@
 );%0A  %7D%0A%0A
+  public static void writeBytecode(String targetDirectory, String className,%0A      byte%5B%5D bytecode) throws IOException %7B%0A    String packageName = packageName(className);%0A    createPackageHierarchy(targetDirectory, packageName);%0A    String file = targetDirectory + File.separator + className + %22.class%22;%0A    FileOutputStream stream = new FileOutputStream(new File(file));%0A    stream.write(bytecode, 0, bytecode.length);%0A    stream.flush();%0A    stream.close();%0A  %7D%0A%0A  private static String createPackageHierarchy(String targetDirectory,%0A      String packageName) %7B%0A    String path = targetDirectory;%0A    for (String folder : packageName.split(%22%5C%5C.%22)) %7B%0A      path = path + File.separator + folder;%0A      createDirectoryIfNotExists(path);%0A    %7D%0A    return path;%0A  %7D%0A%0A  private static void createDirectoryIfNotExists(String directory) %7B%0A    File file = new File(directory);%0A    if (file.exists()) %7B%0A      if (!file.isDirectory()) %7B%0A        String msg = %22Path %22 + directory + %22 exists, but is not a directory%22;%0A        throw new RuntimeException(msg);%0A      %7D%0A    %7D else %7B%0A      file.mkdir();%0A    %7D%0A  %7D%0A%0A  private static String packageName(String internalName) %7B%0A    int separatorPos = internalName.lastIndexOf(%22/%22);%0A    String packageName = internalName.substring(0, separatorPos);%0A    return packageName.replace('/', '.');%0A  %7D%0A%0A
 %7D%0A

Add methods to write the transformed bytecode to a file