/*
 * Copyright 2015 GeekSaga.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.geeksaga.light.profiler;

import com.geeksaga.light.profiler.asm.ClassNodeWrapper;
import com.geeksaga.light.profiler.util.ASMUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author geeksaga
 */
public class TestUtil {
    public static byte[] load(String name) throws Exception {
        URL url = TestUtil.class.getResource("/" + convert(name));
        File file = new File(url.toURI());
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        while (true) {
            byte[] buffer = new byte[1024];
            int read = fileInputStream.read(buffer);
            if (read == -1) {
                break;
            }

            byteArrayOutputStream.write(buffer, 0, read);
        }

        fileInputStream.close();

        return byteArrayOutputStream.toByteArray();
    }

    public static ClassNodeWrapper loadClass(String fileName) throws Exception {
        return ASMUtil.parse(load(fileName));
    }

    private static String convert(String value) {
        Pattern pattern = Pattern.compile("(\\w+).(\\w+)");
        Matcher matcher = pattern.matcher(value);

        if(matcher.find())
        {
            return matcher.group(0).replace('.', '/') + ".class";
        }

        return value.replace('.', '/');
    }
}

@@ -814,32 +814,60 @@
 ileInputStream;%0A
+import java.io.InputStream;%0A
 import java.net.
@@ -871,16 +871,16 @@
 et.URL;%0A
-
 import j
@@ -1062,18 +1062,20 @@
     
-URL url = 
+return load(
 Test
@@ -1121,30 +1121,120 @@
 me))
+)
 ;%0A    
+%7D%0A%0A
     
-File file = 
+public static byte%5B%5D load(URL url) throws Exception%0A    %7B%0A        return load(new FileInputStream(
 new 
@@ -1254,22 +1254,49 @@
 I())
+))
 ;%0A    
+%7D%0A%0A
     
-File
+public static byte%5B%5D load(
 Inpu
@@ -1303,21 +1303,17 @@
 tStream 
-fileI
+i
 nputStre
@@ -1318,37 +1318,32 @@
 ream
- = new FileInputStream(file);
+) throws Exception%0A    %7B
 %0A   
@@ -1443,16 +1443,24 @@
 e (true)
+%0A       
  %7B%0A     
@@ -1521,21 +1521,17 @@
  read = 
-fileI
+i
 nputStre
@@ -1574,16 +1574,28 @@
 d == -1)
+%0A           
  %7B%0A     
@@ -1708,13 +1708,9 @@
     
-fileI
+i
 nput
@@ -2016,21 +2016,19 @@
 le(%22
+%5B
 (%5C%5Cw+).
-(%5C%5Cw+)
+)%5D+
 %22);%0A

update test util