Java Byte Array from getBytes(Class clazz)

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

Description

get Bytes

License

Open Source License

Declaration

private static byte[] getBytes(Class clazz) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    private static byte[] getBytes(Class clazz) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try (InputStream in = getResourceAsStrem(getBytecodeRelativePath(clazz))) {
            byte[] buffer = new byte[1024];
            for (int read = in.read(buffer); read > -1; read = in.read(buffer)) {
                result.write(buffer, 0, read);
            }/*from  w w w .j a v  a  2  s.c om*/
        }
        return result.toByteArray();
    }

    private static InputStream getResourceAsStrem(String name) {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
    }

    private static String getBytecodeRelativePath(Class clazz) {
        return clazz.getName().replace('.', '/') + ".class";
    }
}

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)