Java Byte Array from getBytes(Class clazz)

Here you can find the source of getBytes(Class clazz)

Description

get Bytes

License

Apache License

Declaration

public static byte[] getBytes(Class<?> clazz) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;

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

public class Main {
    public static byte[] getBytes(Class<?> clazz) throws IOException {

        String resourcePath = clazz.getName().replace('.', '/') + ".class";

        //      System.out.println("Util.getBytes():" + resourcePath);
        ClassLoader loader = clazz.getClassLoader();
        InputStream in;//from   ww  w .  ja v a  2s.  c om

        if (loader == null) {
            in = ClassLoader.getSystemResourceAsStream(resourcePath);
        } else {
            in = loader.getResourceAsStream(resourcePath);
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[100];
        while (true) {
            int len = in.read(buf);
            if (len < 0) {
                break;
            }
            out.write(buf, 0, len);
        }

        in.close();

        return out.toByteArray();

    }
}

Related

  1. getBytes(byte value)
  2. getBytes(byte[] buffer, int offset)
  3. getBytes(Class clazz)
  4. getBytes(Class cls, String resourceName)
  5. getBytes(final InputStream is)
  6. getBytes(final Serializable obj)
  7. getBytes(final String data, String charset)