| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2014 Red Hat, Inc., and individual contributors |
| 4 |
* as indicated by the @author tags. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.jboss.modules.util; |
| 20 |
|
| 21 |
import java.io.ByteArrayOutputStream; |
| 22 |
import java.io.File; |
| 23 |
import java.io.FileInputStream; |
| 24 |
import java.io.IOException; |
| 25 |
import java.io.InputStream; |
| 26 |
import java.net.URL; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Enumeration; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
/** |
| 32 |
* Utility class providing commonly used test utilities. |
| 33 |
* |
| 34 |
* @author John E. Bailey |
| 35 |
*/ |
| 36 |
public class Util { |
| 37 |
|
| 38 |
public static byte[] readBytes(final InputStream is) throws IOException { |
| 39 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 40 |
try { |
| 41 |
byte[] buff = new byte[1024]; |
| 42 |
int read; |
| 43 |
while((read = is.read(buff)) > -1) { |
| 44 |
os.write(buff, 0, read); |
| 45 |
} |
| 46 |
} finally { |
| 47 |
is.close(); |
| 48 |
} |
| 49 |
return os.toByteArray(); |
| 50 |
} |
| 51 |
|
| 52 |
public static URL getResource(final Class<?> baseClass, final String path) throws Exception { |
| 53 |
final URL url = baseClass.getClassLoader().getResource(path); |
| 54 |
return url; |
| 55 |
} |
| 56 |
|
| 57 |
public static File getResourceFile(final Class<?> baseClass, final String path) throws Exception { |
| 58 |
return new File(getResource(baseClass, path).toURI()); |
| 59 |
} |
| 60 |
|
| 61 |
public static <T> List<T> toList(Enumeration<T> enumeration) { |
| 62 |
final List<T> list = new ArrayList<T>(); |
| 63 |
while(enumeration.hasMoreElements()) { |
| 64 |
list.add(enumeration.nextElement()); |
| 65 |
} |
| 66 |
return list; |
| 67 |
} |
| 68 |
|
| 69 |
public static byte[] getClassBytes(final Class<?> aClass) throws Exception { |
| 70 |
final String resourcePath = getResourceNameOfClass(aClass); |
| 71 |
final File classFile = Util.getResourceFile(aClass, resourcePath); |
| 72 |
byte[] classBytes = Util.readBytes(new FileInputStream(classFile)); |
| 73 |
return classBytes; |
| 74 |
} |
| 75 |
|
| 76 |
public static String getResourceNameOfClass(final Class<?> aClass) throws IllegalArgumentException { |
| 77 |
final String nameAsResourcePath = aClass.getName().replace('.', '/'); |
| 78 |
final String resourceName = nameAsResourcePath + ".class"; |
| 79 |
return resourceName; |
| 80 |
} |
| 81 |
|
| 82 |
} |