Search in sources :

Example 31 with FileNotFoundException

use of java.io.FileNotFoundException in project groovy by apache.

the class AntlrParserPlugin method outputASTInVariousFormsIfNeeded.

private void outputASTInVariousFormsIfNeeded(SourceUnit sourceUnit, SourceBuffer sourceBuffer) {
    // straight xstream output of AST
    // uppercase to hide from jarjar
    String formatProp = System.getProperty("ANTLR.AST".toLowerCase());
    if ("xml".equals(formatProp)) {
        saveAsXML(sourceUnit.getName(), ast);
    }
    // 'pretty printer' output of AST
    if ("groovy".equals(formatProp)) {
        try {
            PrintStream out = new PrintStream(new FileOutputStream(sourceUnit.getName() + ".pretty.groovy"));
            Visitor visitor = new SourcePrinter(out, tokenNames);
            AntlrASTProcessor treewalker = new SourceCodeTraversal(visitor);
            treewalker.process(ast);
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create " + sourceUnit.getName() + ".pretty.groovy");
        }
    }
    // which is a really nice way of seeing the AST, folding nodes etc
    if ("mindmap".equals(formatProp)) {
        try {
            PrintStream out = new PrintStream(new FileOutputStream(sourceUnit.getName() + ".mm"));
            Visitor visitor = new MindMapPrinter(out, tokenNames);
            AntlrASTProcessor treewalker = new PreOrderTraversal(visitor);
            treewalker.process(ast);
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create " + sourceUnit.getName() + ".mm");
        }
    }
    // include original line/col info and source code on the mindmap output
    if ("extendedMindmap".equals(formatProp)) {
        try {
            PrintStream out = new PrintStream(new FileOutputStream(sourceUnit.getName() + ".mm"));
            Visitor visitor = new MindMapPrinter(out, tokenNames, sourceBuffer);
            AntlrASTProcessor treewalker = new PreOrderTraversal(visitor);
            treewalker.process(ast);
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create " + sourceUnit.getName() + ".mm");
        }
    }
    // html output of AST
    if ("html".equals(formatProp)) {
        try {
            PrintStream out = new PrintStream(new FileOutputStream(sourceUnit.getName() + ".html"));
            List<VisitorAdapter> v = new ArrayList<VisitorAdapter>();
            v.add(new NodeAsHTMLPrinter(out, tokenNames));
            v.add(new SourcePrinter(out, tokenNames));
            Visitor visitors = new CompositeVisitor(v);
            AntlrASTProcessor treewalker = new SourceCodeTraversal(visitors);
            treewalker.process(ast);
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create " + sourceUnit.getName() + ".html");
        }
    }
}
Also used : SourcePrinter(org.codehaus.groovy.antlr.treewalker.SourcePrinter) PrintStream(java.io.PrintStream) NodeAsHTMLPrinter(org.codehaus.groovy.antlr.treewalker.NodeAsHTMLPrinter) PreOrderTraversal(org.codehaus.groovy.antlr.treewalker.PreOrderTraversal) CompositeVisitor(org.codehaus.groovy.antlr.treewalker.CompositeVisitor) Visitor(org.codehaus.groovy.antlr.treewalker.Visitor) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) MindMapPrinter(org.codehaus.groovy.antlr.treewalker.MindMapPrinter) FileOutputStream(java.io.FileOutputStream) VisitorAdapter(org.codehaus.groovy.antlr.treewalker.VisitorAdapter) SourceCodeTraversal(org.codehaus.groovy.antlr.treewalker.SourceCodeTraversal) CompositeVisitor(org.codehaus.groovy.antlr.treewalker.CompositeVisitor)

Example 32 with FileNotFoundException

use of java.io.FileNotFoundException in project hadoop by apache.

the class SFTPFileSystem method getFileStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
@SuppressWarnings("unchecked")
private FileStatus getFileStatus(ChannelSftp client, Path file) throws IOException {
    FileStatus fileStat = null;
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    Path parentPath = absolute.getParent();
    if (parentPath == null) {
        // root directory
        // Length of root directory on server not known
        long length = -1;
        boolean isDir = true;
        int blockReplication = 1;
        // Block Size not known.
        long blockSize = DEFAULT_BLOCK_SIZE;
        // Modification time of root directory not known.
        long modTime = -1;
        Path root = new Path("/");
        return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
    }
    String pathName = parentPath.toUri().getPath();
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(pathName);
    } catch (SftpException e) {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    if (sftpFiles != null) {
        for (LsEntry sftpFile : sftpFiles) {
            if (sftpFile.getFilename().equals(file.getName())) {
                // file found in directory
                fileStat = getFileStatus(client, sftpFile, parentPath);
                break;
            }
        }
        if (fileStat == null) {
            throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
        }
    } else {
        throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
    }
    return fileStat;
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 33 with FileNotFoundException

use of java.io.FileNotFoundException in project hadoop by apache.

the class RawLocalFileSystem method truncate.

@Override
public boolean truncate(Path f, final long newLength) throws IOException {
    FileStatus status = getFileStatus(f);
    if (status == null) {
        throw new FileNotFoundException("File " + f + " not found");
    }
    if (status.isDirectory()) {
        throw new IOException("Cannot truncate a directory (=" + f + ")");
    }
    long oldLength = status.getLen();
    if (newLength > oldLength) {
        throw new IllegalArgumentException("Cannot truncate to a larger file size. Current size: " + oldLength + ", truncate size: " + newLength + ".");
    }
    try (FileOutputStream out = new FileOutputStream(pathToFile(f), true)) {
        try {
            out.getChannel().truncate(newLength);
        } catch (IOException e) {
            throw new FSError(e);
        }
    }
    return true;
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 34 with FileNotFoundException

use of java.io.FileNotFoundException in project hadoop by apache.

the class RawLocalFileSystem method listStatus.

/**
   * {@inheritDoc}
   *
   * (<b>Note</b>: Returned list is not sorted in any given order,
   * due to reliance on Java's {@link File#list()} API.)
   */
@Override
public FileStatus[] listStatus(Path f) throws IOException {
    File localf = pathToFile(f);
    FileStatus[] results;
    if (!localf.exists()) {
        throw new FileNotFoundException("File " + f + " does not exist");
    }
    if (localf.isDirectory()) {
        String[] names = FileUtil.list(localf);
        results = new FileStatus[names.length];
        int j = 0;
        for (int i = 0; i < names.length; i++) {
            try {
                // Assemble the path using the Path 3 arg constructor to make sure
                // paths with colon are properly resolved on Linux
                results[j] = getFileStatus(new Path(f, new Path(null, null, names[i])));
                j++;
            } catch (FileNotFoundException e) {
            // ignore the files not found since the dir list may have have
            // changed since the names[] list was generated.
            }
        }
        if (j == names.length) {
            return results;
        }
        return Arrays.copyOf(results, j);
    }
    if (!useDeprecatedFileStatus) {
        return new FileStatus[] { getFileStatus(f) };
    }
    return new FileStatus[] { new DeprecatedRawLocalFileStatus(localf, getDefaultBlockSize(f), this) };
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File)

Example 35 with FileNotFoundException

use of java.io.FileNotFoundException in project hadoop by apache.

the class FTPFileSystem method delete.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
private boolean delete(FTPClient client, Path file, boolean recursive) throws IOException {
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    String pathName = absolute.toUri().getPath();
    try {
        FileStatus fileStat = getFileStatus(client, absolute);
        if (fileStat.isFile()) {
            return client.deleteFile(pathName);
        }
    } catch (FileNotFoundException e) {
        //the file is not there
        return false;
    }
    FileStatus[] dirEntries = listStatus(client, absolute);
    if (dirEntries != null && dirEntries.length > 0 && !(recursive)) {
        throw new IOException("Directory: " + file + " is not empty.");
    }
    for (FileStatus dirEntry : dirEntries) {
        delete(client, new Path(absolute, dirEntry.getPath()), recursive);
    }
    return client.removeDirectory(pathName);
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)3218 IOException (java.io.IOException)1836 File (java.io.File)1277 FileInputStream (java.io.FileInputStream)814 FileOutputStream (java.io.FileOutputStream)492 InputStream (java.io.InputStream)466 BufferedReader (java.io.BufferedReader)262 FileReader (java.io.FileReader)230 ArrayList (java.util.ArrayList)205 Path (org.apache.hadoop.fs.Path)202 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)189 InputStreamReader (java.io.InputStreamReader)171 Test (org.junit.Test)171 XmlPullParser (org.xmlpull.v1.XmlPullParser)166 BufferedInputStream (java.io.BufferedInputStream)138 ParcelFileDescriptor (android.os.ParcelFileDescriptor)131 Properties (java.util.Properties)120 URL (java.net.URL)119 FileStatus (org.apache.hadoop.fs.FileStatus)119 RandomAccessFile (java.io.RandomAccessFile)101