Example usage for javax.tools JavaFileObject openInputStream

List of usage examples for javax.tools JavaFileObject openInputStream

Introduction

In this page you can find the example usage for javax.tools JavaFileObject openInputStream.

Prototype

InputStream openInputStream() throws IOException;

Source Link

Document

Returns an InputStream for this file object.

Usage

From source file:org.cdmckay.coffeep.Program.java

private static ClassFile getClassFile(JavaFileObject fileObject, CoffeepSystemInfo systemInfo)
        throws IOException, ConstantPoolException {
    if (fileObject == null) {
        throw new IllegalArgumentException("fileObject cannot be null");
    }//from w ww  . j  a va  2  s  .  co  m
    if (systemInfo == null) {
        throw new IllegalArgumentException("systemInfo cannot be null");
    }

    InputStream inputStream = fileObject.openInputStream();
    try {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            logger.warn("Exception while getting MD5 MessageDigest", e);
        }

        DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest);
        CountingInputStream countingInputStream = new CountingInputStream(digestInputStream);

        Attribute.Factory attributeFactory = new Attribute.Factory();
        ClassFile classFile = ClassFile.read(countingInputStream, attributeFactory);

        systemInfo.classFileUri = fileObject.toUri();
        systemInfo.classFileSize = countingInputStream.getSize();
        systemInfo.lastModifiedTimestamp = fileObject.getLastModified();
        if (messageDigest != null) {
            systemInfo.digestAlgorithm = messageDigest.getAlgorithm();
            systemInfo.digest = new BigInteger(1, messageDigest.digest()).toString(16);
        }

        return classFile;
    } finally {
        inputStream.close();
    }
}