Example usage for org.eclipse.jdt.core IJavaModelStatusConstants IO_EXCEPTION

List of usage examples for org.eclipse.jdt.core IJavaModelStatusConstants IO_EXCEPTION

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModelStatusConstants IO_EXCEPTION.

Prototype

int IO_EXCEPTION

To view the source code for org.eclipse.jdt.core IJavaModelStatusConstants IO_EXCEPTION.

Click Source Link

Document

Status constant indicating that an java.io.IOException occurred.

Usage

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

License:Open Source License

public IBinaryType getBinaryTypeInfo(IFile file, boolean fullyInitialize) throws JavaModelException {
    JavaElement pkg = (JavaElement) getParent();
    if (pkg instanceof JarPackageFragment) {
        try {/*from   w  w w.ja v a  2  s . c o  m*/
            IBinaryType info = getJarBinaryTypeInfo((PackageFragment) pkg, fullyInitialize);
            if (info == null) {
                throw newNotPresentException();
            }
            return info;
        } catch (ClassFormatException cfe) {
            //the structure remains unknown
            if (JavaCore.getPlugin().isDebugging()) {
                cfe.printStackTrace(System.err);
            }
            return null;
        } 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);
            }
        }
    } else {
        //      byte[] contents = Util.getResourceContentsAsByteArray(file);
        //      try {
        //         return new ClassFileReader(contents, file.getFullPath().toString().toCharArray(), fullyInitialize);
        //      } catch (ClassFormatException cfe) {
        //         //the structure remains unknown
        //         return null;
        //      }
        throw new UnsupportedOperationException();
    }
}

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. j  av  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:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getContents()
 *///from w  w w .  j  a v a2  s .c o m
public char[] getContents() {
    IBuffer buffer = getBufferManager().getBuffer(this);
    if (buffer == null) {
        // no need to force opening of CU to get the content
        // also this cannot be a working copy, as its buffer is never closed while the working copy is alive
        File file = resource();
        // Get encoding from file
        String encoding;
        encoding = "UTF-8"; //file.getCharset();
        try {
            return Util.getResourceContentsAsCharArray(file, encoding);
        } catch (JavaModelException e) {
            if (manager.abortOnMissingSource.get() == Boolean.TRUE) {
                IOException ioException = e.getJavaModelStatus()
                        .getCode() == IJavaModelStatusConstants.IO_EXCEPTION ? (IOException) e.getException()
                                : new IOException(e.getMessage());
                throw new AbortCompilationUnit(null, ioException, encoding);
            } else {
                Util.log(e, Messages.bind(Messages.file_notFound, file.getAbsolutePath()));
            }
            return CharOperation.NO_CHAR;
        }
    }
    char[] contents = buffer.getCharacters();
    if (contents == null) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=129814
        if (manager.abortOnMissingSource.get() == Boolean.TRUE) {
            IOException ioException = new IOException(Messages.buffer_closed);
            IFile file = (IFile) getResource();
            // Get encoding from file
            String encoding;
            try {
                encoding = file.getCharset();
            } catch (CoreException ce) {
                // do not use any encoding
                encoding = null;
            }
            throw new AbortCompilationUnit(null, ioException, encoding);
        }
        return CharOperation.NO_CHAR;
    }
    return contents;
}

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

License:Open Source License

public InputStream getContents() throws CoreException {
    ZipFile zipFile = null;/*from w  w  w.  ja  va 2s.  co  m*/
    try {
        zipFile = getZipFile();
        if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) {
            System.out.println("(" + Thread.currentThread()
                    + ") [JarEntryFile.getContents()] Creating ZipFile on " + zipFile.getName()); //$NON-NLS-1$   //$NON-NLS-2$
        }
        String entryName = getEntryName();
        ZipEntry zipEntry = zipFile.getEntry(entryName);
        if (zipEntry == null) {
            throw new JavaModelException(
                    new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, entryName));
        }
        byte[] contents = Util.getZipEntryByteContent(zipEntry, zipFile);
        return new ByteArrayInputStream(contents);
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    } finally {
        // avoid leaking ZipFiles
        manager.closeZipFile(zipFile);
    }
}

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

License:Open Source License

protected String getURLContents(URL baseLoc, String docUrlValue) throws JavaModelException {
    InputStream stream = null;/*from   ww w .  j  av  a 2  s . c o  m*/
    JarURLConnection connection2 = null;
    try {
        URL docUrl = new URL(docUrlValue);
        URLConnection connection = docUrl.openConnection();
        Class[] parameterTypes = new Class[] { int.class };
        Integer timeoutVal = new Integer(10000);
        // set the connect and read timeouts using reflection since these methods are not available in java 1.4
        Class URLClass = connection.getClass();
        try {
            Method connectTimeoutMethod = URLClass.getDeclaredMethod("setConnectTimeout", parameterTypes); //$NON-NLS-1$
            Method readTimeoutMethod = URLClass.getDeclaredMethod("setReadTimeout", parameterTypes); //$NON-NLS-1$
            connectTimeoutMethod.invoke(connection, new Object[] { timeoutVal });
            readTimeoutMethod.invoke(connection, new Object[] { timeoutVal });
        } catch (SecurityException e) {
            // ignore
        } catch (IllegalArgumentException e) {
            // ignore
        } catch (NoSuchMethodException e) {
            // ignore
        } catch (IllegalAccessException e) {
            // ignore
        } catch (InvocationTargetException e) {
            // ignore
        }

        if (connection instanceof JarURLConnection) {
            connection2 = (JarURLConnection) connection;
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=156307
            connection.setUseCaches(false);
        }
        try {
            stream = new BufferedInputStream(connection.getInputStream());
        } catch (IllegalArgumentException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304316
            return null;
        } catch (NullPointerException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304316
            return null;
        }
        String encoding = connection.getContentEncoding();
        byte[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream,
                connection.getContentLength());
        if (encoding == null) {
            int index = getIndexOf(contents, META_START, 0, -1);
            if (index != -1) {
                int end = getIndexOf(contents, META_END, index, -1);
                if (end != -1) {
                    if ((end + 1) <= contents.length)
                        end++;
                    int charsetIndex = getIndexOf(contents, CHARSET_HTML5, index, end);
                    if (charsetIndex == -1) {
                        charsetIndex = getIndexOf(contents, CHARSET, index, end);
                        if (charsetIndex != -1)
                            charsetIndex = charsetIndex + CHARSET.length;
                    } else {
                        charsetIndex = charsetIndex + CHARSET_HTML5.length;
                    }
                    if (charsetIndex != -1) {
                        end = getIndexOf(contents, CLOSING_DOUBLE_QUOTE, charsetIndex, end);
                        encoding = new String(contents, charsetIndex, end - charsetIndex,
                                org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
                    }
                }
            }
        }
        try {
            if (encoding == null) {
                encoding = getJavaProject().getProject().getDefaultCharset();
            }
        } catch (CoreException e) {
            // ignore
        }
        if (contents != null) {
            if (encoding != null) {
                return new String(contents, encoding);
            } else {
                // platform encoding is used
                return new String(contents);
            }
        }
    } catch (SocketTimeoutException e) {
        throw new JavaModelException(
                new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT, this));
    } catch (MalformedURLException e) {
        throw new JavaModelException(
                new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
    } catch (FileNotFoundException e) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=403154
        validateAndCache(baseLoc, e);
    } catch (SocketException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (UnknownHostException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (ProtocolException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // ignore
            }
        }
        if (connection2 != null) {
            try {
                connection2.getJarFile().close();
            } catch (IOException e) {
                // ignore
            } catch (IllegalStateException e) {
                /*
                * ignore. Can happen in case the stream.close() did close the jar file
                * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=140750
                */
            }
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

protected IBinaryType getBinaryInfo(ClassFile classFile, IResource resource) throws CoreException {
    BinaryType binaryType = (BinaryType) classFile.getType();
    if (classFile.isOpen())
        return (IBinaryType) binaryType.getElementInfo(); // reuse the info from the java model cache

    // create a temporary info
    IBinaryType info;/*from  www . j a  v  a  2  s.  com*/
    try {
        PackageFragment pkg = (PackageFragment) classFile.getParent();
        PackageFragmentRoot root = (PackageFragmentRoot) pkg.getParent();
        if (root.isArchive()) {
            // class file in a jar
            String classFileName = classFile.getElementName();
            String classFilePath = Util.concatWith(pkg.names, classFileName, '/');
            ZipFile zipFile = null;
            try {
                zipFile = ((JarPackageFragmentRoot) root).getJar();
                info = ClassFileReader.read(zipFile, classFilePath);
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
            }
        } else {
            // class file in a directory
            info = Util.newClassFileReader(resource);
        }
        if (info == null)
            throw binaryType.newNotPresentException();
        return info;
    } catch (ClassFormatException e) {
        //e.printStackTrace();
        return null;
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    }
}

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

License:Open Source License

public char[] getContents() {
    if (this.contents != null)
        return this.contents; // answer the cached source

    // otherwise retrieve it
    try {//ww  w .  j  a va2  s. c  o m
        // Get resource contents
        InputStream stream = null;
        try {
            stream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
        }
        try {
            return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream,
                    (int) file.length(), "UTF-8");
        } catch (IOException e) {
            throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                // ignore
            }
        }
    } catch (CoreException e) {
        return CharOperation.NO_CHAR;
    }
}

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

License:Open Source License

public static char[] getResourceContentsAsCharArray(File file, String encoding) throws JavaModelException {
    // Get file length
    // workaround https://bugs.eclipse.org/bugs/show_bug.cgi?id=130736 by using java.io.File if possible
    //        IPath location = file.getLocation();
    long length;//w w w  .  ja  v a2  s.  c  o m
    //        if (location == null) {
    //            // non local file
    //            try {
    //                URI locationURI = file.getLocationURI();
    //                if (locationURI == null)
    //                    throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, Messages
    //                            .bind(Messages.file_notFound, file.getFullPath().toString())));
    //                length = EFS.getStore(locationURI).fetchInfo().getLength();
    //            } catch (CoreException e) {
    //                throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
    //            }
    //        } else {
    //            // local file
    length = file.length();
    //        }

    // Get resource contents
    InputStream stream = null;
    try {
        stream = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
    }
    try {
        return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, (int) length,
                encoding);
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            // ignore
        }
    }
}

From source file:org.eclipse.che.jdt.internal.core.search.Util.java

License:Open Source License

public static char[] getResourceContentsAsCharArray(File file) throws JavaModelException {
    InputStream stream = null;/*from  www. j  a va2 s .  co m*/
    try {
        stream = new FileInputStream(file);
        return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream,
                (int) file.length(), "UTF-8");
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    }

}

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

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.compiler.env.ICompilationUnit#getContents()
 *//*from   www. j  a  v a  2s.  co  m*/
public char[] getContents() {
    IBuffer buffer = getBufferManager().getBuffer(this);
    if (buffer == null) {
        // no need to force opening of CU to get the content
        // also this cannot be a working copy, as its buffer is never closed while the working copy is alive
        IFile file = (IFile) getResource();
        // Get encoding from file
        String encoding;
        try {
            encoding = file.getCharset();
        } catch (CoreException ce) {
            // do not use any encoding
            encoding = null;
        }
        try {
            return Util.getResourceContentsAsCharArray(file, encoding);
        } catch (JavaModelException e) {
            if (JavaModelManager.getJavaModelManager().abortOnMissingSource.get() == Boolean.TRUE) {
                IOException ioException = e.getJavaModelStatus()
                        .getCode() == IJavaModelStatusConstants.IO_EXCEPTION ? (IOException) e.getException()
                                : new IOException(e.getMessage());
                throw new AbortCompilationUnit(null, ioException, encoding);
            } else {
                Util.log(e, Messages.bind(Messages.file_notFound, file.getFullPath().toString()));
            }
            return CharOperation.NO_CHAR;
        }
    }
    char[] contents = buffer.getCharacters();
    if (contents == null) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=129814
        if (JavaModelManager.getJavaModelManager().abortOnMissingSource.get() == Boolean.TRUE) {
            IOException ioException = new IOException(Messages.buffer_closed);
            IFile file = (IFile) getResource();
            // Get encoding from file
            String encoding;
            try {
                encoding = file.getCharset();
            } catch (CoreException ce) {
                // do not use any encoding
                encoding = null;
            }
            throw new AbortCompilationUnit(null, ioException, encoding);
        }
        return CharOperation.NO_CHAR;
    }
    return contents;
}