Example usage for org.eclipse.jdt.internal.core.util Util getResourceContentsAsByteArray

List of usage examples for org.eclipse.jdt.internal.core.util Util getResourceContentsAsByteArray

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.util Util getResourceContentsAsByteArray.

Prototype

public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException 

Source Link

Document

Returns the given file's contents as a byte array.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

public byte[] getBytes() throws JavaModelException {
    JavaElement pkg = (JavaElement) getParent();
    if (pkg instanceof JarPackageFragment) {
        JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
        ZipFile zip = null;//from  w w  w.ja  v a 2 s. c om
        try {
            zip = root.getJar();
            String entryName = Util.concatWith(((PackageFragment) pkg).names, getElementName(), '/');
            ZipEntry ze = zip.getEntry(entryName);
            if (ze != null) {
                return org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(ze, zip);
            }
            throw new JavaModelException(
                    new JavaModelStatus(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, this));
        } catch (IOException ioe) {
            throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
        } catch (CoreException e) {
            if (e instanceof JavaModelException) {
                throw (JavaModelException) e;
            } else {
                throw new JavaModelException(e);
            }
        } finally {
            manager.closeZipFile(zip);
        }
    } else {
        IFile file = (IFile) resource();
        return Util.getResourceContentsAsByteArray(file);
    }
}

From source file:net.sf.j2s.core.builder.IncrementalImageBuilder.java

License:Open Source License

protected boolean writeClassFileCheck(IFile file, String fileName, byte[] newBytes) throws CoreException {
    try {//from w  ww . jav a 2s. co  m
        byte[] oldBytes = Util.getResourceContentsAsByteArray(file);
        notEqual: if (newBytes.length == oldBytes.length) {
            for (int i = newBytes.length; --i >= 0;)
                if (newBytes[i] != oldBytes[i])
                    break notEqual;
            return false; // bytes are identical so skip them
        }
        URI location = file.getLocationURI();
        if (location == null)
            return false; // unable to determine location of this class file
        String filePath = location.getSchemeSpecificPart();
        ClassFileReader reader = new ClassFileReader(oldBytes, filePath.toCharArray());
        // ignore local types since they're only visible inside a single method
        if (!(reader.isLocal() || reader.isAnonymous()) && reader.hasStructuralChanges(newBytes)) {
            if (JavaBuilder.DEBUG)
                System.out.println("Type has structural changes " + fileName); //$NON-NLS-1$
            addDependentsOf(new Path(fileName), true);
            this.newState.wasStructurallyChanged(fileName);
        }
    } catch (ClassFormatException e) {
        addDependentsOf(new Path(fileName), true);
        this.newState.wasStructurallyChanged(fileName);
    }
    return true;
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Retrieve a shared property on a project. If the property is not defined, answers null.
 * Note that it is orthogonal to IResource persistent properties, and client code has to decide
 * which form of storage to use appropriately. Shared properties produce real resource files which
 * can be shared through a VCM onto a server. Persistent properties are not shareable.
 *
 * @param key String/*from  w  w  w  . j a v a 2  s.  c o m*/
 * @see JavaProject#setSharedProperty(String, String)
 * @return String
 * @throws CoreException
 */
public String getSharedProperty(String key) throws CoreException {

    String property = null;
    IFile rscFile = this.project.getFile(key);
    if (rscFile.exists()) {
        byte[] bytes = Util.getResourceContentsAsByteArray(rscFile);
        try {
            property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
        } catch (UnsupportedEncodingException e) {
            Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
            // fallback to default
            property = new String(bytes);
        }
    } else {
        // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
        // so default to using java.io.File
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
        URI location = rscFile.getLocationURI();
        if (location != null) {
            File file = Util.toLocalFile(location, null/*no progress monitor available*/);
            if (file != null && file.exists()) {
                byte[] bytes;
                try {
                    bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
                } catch (IOException e) {
                    return null;
                }
                try {
                    property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
                } catch (UnsupportedEncodingException e) {
                    Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
                    // fallback to default
                    property = new String(bytes);
                }
            }
        }
    }
    return property;
}

From source file:org.eclipse.jdt.internal.core.JavaProject.java

License:Open Source License

/**
 * Reads the classpath file entries of this project's .classpath file.
 * Returns a two-dimensional array, where the number of elements in the row is fixed to 2.
 * The first element is an array of raw classpath entries, which includes the output entry,
 * and the second element is an array of referenced entries that may have been stored 
 * by the client earlier. /*from  w w w  .  j a v  a  2 s  . c  o  m*/
 * See {@link IJavaProject#getReferencedClasspathEntries()} for more details.
 * As a side effect, unknown elements are stored in the given map (if not null)
 * Throws exceptions if the file cannot be accessed or is malformed.
 */
public IClasspathEntry[][] readFileEntriesWithException(Map unknownElements)
        throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
    IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME);
    byte[] bytes;
    if (rscFile.exists()) {
        bytes = Util.getResourceContentsAsByteArray(rscFile);
    } else {
        // when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
        // so default to using java.io.File
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=96258
        URI location = rscFile.getLocationURI();
        if (location == null)
            throw new IOException("Cannot obtain a location URI for " + rscFile); //$NON-NLS-1$
        File file = Util.toLocalFile(location, null/*no progress monitor available*/);
        if (file == null)
            throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
        try {
            bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
        } catch (IOException e) {
            if (!file.exists())
                return new IClasspathEntry[][] { defaultClasspath(), ClasspathEntry.NO_ENTRIES };
            throw e;
        }
    }
    if (hasUTF8BOM(bytes)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
        int length = bytes.length - IContentDescription.BOM_UTF_8.length;
        System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
    }
    String xmlClasspath;
    try {
        xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
    } catch (UnsupportedEncodingException e) {
        Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
        // fallback to default
        xmlClasspath = new String(bytes);
    }
    return decodeClasspath(xmlClasspath, unknownElements);
}