Java Code Examples for org.eclipse.jdt.internal.core.util.Util#getResourceContentsAsByteArray()

The following examples show how to use org.eclipse.jdt.internal.core.util.Util#getResourceContentsAsByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaHotCodeReplaceProvider.java    From java-debug with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns a mapping of class files to the bytes that make up those class files.
 *
 * @param resources
 *            the classfiles
 * @param qualifiedNames
 *            the fully qualified type names corresponding to the classfiles.
 *            The typeNames correspond to the resources on a one-to-one basis.
 * @return a mapping of class files to bytes key: class file value: the bytes
 *         which make up that classfile
 */
private Map<ReferenceType, byte[]> getTypesToBytes(List<IResource> resources, List<String> qualifiedNames) {
    Map<ReferenceType, byte[]> typesToBytes = new HashMap<>(resources.size());
    Iterator<IResource> resourceIter = resources.iterator();
    Iterator<String> nameIter = qualifiedNames.iterator();
    IResource resource;
    String name;
    while (resourceIter.hasNext()) {
        resource = resourceIter.next();
        name = nameIter.next();
        List<ReferenceType> classes = getJdiClassesByName(name);
        byte[] bytes = null;
        try {
            bytes = Util.getResourceContentsAsByteArray((IFile) resource);
        } catch (CoreException e) {
            continue;
        }
        for (ReferenceType type : classes) {
            typesToBytes.put(type, bytes);
        }
    }
    return typesToBytes;
}
 
Example 2
Source File: IncrementalImageBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
protected boolean writeClassFileCheck(IFile file, String fileName, byte[] newBytes) throws CoreException {
	try {
		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;
}
 
Example 3
Source File: JavaSearchDocument.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public byte[] getByteContents() {
	if (this.byteContents != null) return this.byteContents;
	try {
		return Util.getResourceContentsAsByteArray(getFile());
	} catch (JavaModelException e) {
		if (BasicSearchEngine.VERBOSE || JobManager.VERBOSE) { // used during search and during indexing
			e.printStackTrace();
		}
		return null;
	}
}
 
Example 4
Source File: ClassFile.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public byte[] getBytes() throws JavaModelException {
	JavaElement pkg = (JavaElement) getParent();
	if (pkg instanceof JarPackageFragment) {
		JarPackageFragmentRoot root = (JarPackageFragmentRoot) pkg.getParent();
		ZipFile zip = null;
		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 {
			JavaModelManager.getJavaModelManager().closeZipFile(zip);
		}
	} else {
		IFile file = (IFile) resource();
		return Util.getResourceContentsAsByteArray(file);
	}
}