Example usage for java.io IOException setStackTrace

List of usage examples for java.io IOException setStackTrace

Introduction

In this page you can find the example usage for java.io IOException setStackTrace.

Prototype

public void setStackTrace(StackTraceElement[] stackTrace) 

Source Link

Document

Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.

Usage

From source file:tectonicus.util.FileUtils.java

/**
 * This function will copy files or directories from one location to
 * another. note that the source and the destination must be mutually
 * exclusive. This function can not be used to copy a directory to a sub
 * directory of itself. The function will also have problems if the
 * destination files already exist.//  w w w  .j a  v  a2  s  .  c om
 * 
 * @param src
 *            -- A File object that represents the source for the copy
 * @param dest
 *            -- A File object that represnts the destination for the copy.
 * @throws IOException
 *             if unable to copy.
 */
public static void copyFiles(File src, File dest, Set<String> excludeExtensions) throws IOException {
    // Check to ensure that the source is valid...
    if (!src.exists()) {
        throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) {
        // check to ensure we have rights to the source...
        throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
    }

    // is this a directory copy?
    if (src.isDirectory()) {
        if (!dest.exists()) { // does the destination already exist?
                              // if not we need to make it exist if possible (note this is
                              // mkdirs not mkdir)
            if (!dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
        }
        // get a listing of files...
        String list[] = src.list();
        // copy all the files in the list.
        for (int i = 0; i < list.length; i++) {
            File dest1 = new File(dest, list[i]);
            File src1 = new File(src, list[i]);
            copyFiles(src1, dest1, excludeExtensions);
        }
    } else {
        String extension = getExtension(src.getName());
        if (!excludeExtensions.contains(extension)) {
            // This was not a directory, so lets just copy the file
            FileInputStream fin = null;
            FileOutputStream fout = null;
            byte[] buffer = new byte[4096]; // Buffer 4K at a time

            int bytesRead;
            try {
                // open the files for input and output
                fin = new FileInputStream(src);
                fout = new FileOutputStream(dest);
                // while bytesRead indicates a successful read, lets write...
                while ((bytesRead = fin.read(buffer)) >= 0) {
                    fout.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                // Error copying file...
                IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath()
                        + "to" + dest.getAbsolutePath() + ".");
                wrapper.initCause(e);
                wrapper.setStackTrace(e.getStackTrace());
                throw wrapper;
            } finally {
                // Ensure that the files are closed (if they were open).
                if (fin != null) {
                    fin.close();
                }
                if (fout != null) {
                    fout.close();
                }
            }
        } else {
            System.out.println("Skipping " + src.getAbsolutePath());
        }
    }
}

From source file:de.ingrid.interfaces.csw.tools.FileUtils.java

/**
 * This function will copy files or directories from one location to
 * another. note that the source and the destination must be mutually
 * exclusive. This function can not be used to copy a directory to a sub
 * directory of itself. The function will also have problems if the
 * destination files already exist./*from   w  w w. ja  v  a  2 s.  c  o m*/
 * 
 * @param src
 *            -- A File object that represents the source for the copy
 * @param dest
 *            -- A File object that represnts the destination for the copy.
 * @throws IOException
 *             if unable to copy.
 * 
 *             Source: http://www.dreamincode.net/code/snippet1443.htm
 */
public static void copyRecursive(File src, File dest) throws IOException {
    // Check to ensure that the source is valid...
    if (!src.exists()) {
        throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) { // check to ensure we have rights to the
        // source...
        throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
    }
    // is this a directory copy?
    if (src.isDirectory()) {
        if (!dest.exists()) { // does the destination already exist?
            // if not we need to make it exist if possible (note this is
            // mkdirs not mkdir)
            if (!dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
        }
        // get a listing of files...
        String list[] = src.list();
        // copy all the files in the list.
        for (String element : list) {
            File dest1 = new File(dest, element);
            File src1 = new File(src, element);
            copyRecursive(src1, dest1);
        }
    } else {
        // This was not a directory, so lets just copy the file
        FileInputStream fin = null;
        FileOutputStream fout = null;
        byte[] buffer = new byte[4096]; // Buffer 4K at a time (you can
        // change this).
        int bytesRead;
        try {
            // open the files for input and output
            fin = new FileInputStream(src);
            fout = new FileOutputStream(dest);
            // while bytesRead indicates a successful read, lets write...
            while ((bytesRead = fin.read(buffer)) >= 0) {
                fout.write(buffer, 0, bytesRead);
            }
            fin.close();
            fout.close();
            fin = null;
            fout = null;
        } catch (IOException e) { // Error copying file...
            IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath()
                    + "to" + dest.getAbsolutePath() + ".");
            wrapper.initCause(e);
            wrapper.setStackTrace(e.getStackTrace());
            throw wrapper;
        } finally { // Ensure that the files are closed (if they were open).
            if (fin != null) {
                fin.close();
            }
            if (fout != null) {
                fin.close();
            }
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.BlockSender.java

/**
 * Converts an IOExcpetion (not subclasses) to SocketException.
 * This is typically done to indicate to upper layers that the error 
 * was a socket error rather than often more serious exceptions like 
 * disk errors.// w w w.  j  a v a 2  s .  c  om
 */
private static IOException ioeToSocketException(IOException ioe) {
    if (ioe.getClass().equals(IOException.class)) {
        // "se" could be a new class in stead of SocketException.
        IOException se = new SocketException("Original Exception : " + ioe);
        se.initCause(ioe);
        /* Change the stacktrace so that original trace is not truncated
         * when printed.*/
        se.setStackTrace(ioe.getStackTrace());
        return se;
    }
    // otherwise just return the same exception.
    return ioe;
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * This function will copy files or directories from one location to another. note that the source and the destination must be mutually exclusive. This function can not be used to copy a directory
 * to a sub directory of itself. The function will also have problems if the destination files already exist.
 * //from ww w . j  a va2s .c  o m
 * @param src
 *            -- A File object that represents the source for the copy
 * @param dest
 *            -- A File object that represnts the destination for the copy.
 * @throws IOException
 *             if unable to copy.
 */
public static void copyFiles(File src, File dest) throws IOException {
    // Check to ensure that the source is valid...
    if (!src.exists()) {
        throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");
    } else if (!src.canRead()) { // check to ensure we have rights to the source...
        throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
    }
    // is this a directory copy?
    if (src.isDirectory()) {
        if (!dest.exists()) { // does the destination already exist?
            // if not we need to make it exist if possible (note this is mkdirs not mkdir)
            if (!dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
        }
        // get a listing of files...
        String list[] = src.list();
        // copy all the files in the list.
        for (int i = 0; i < list.length; i++) {
            File dest1 = new File(dest, list[i]);
            File src1 = new File(src, list[i]);
            copyFiles(src1, dest1);
        }
    } else {
        // This was not a directory, so lets just copy the file
        FileInputStream fin = null;
        FileOutputStream fout = null;
        byte[] buffer = new byte[4096]; // Buffer 4K at a time (you can change this).
        int bytesRead;
        try {
            // open the files for input and output
            fin = new FileInputStream(src);
            fout = new FileOutputStream(dest);
            // while bytesRead indicates a successful read, lets write...
            while ((bytesRead = fin.read(buffer)) >= 0) {
                fout.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) { // Error copying file...
            IOException wrapper = new IOException("copyFiles: Unable to copy file: " + src.getAbsolutePath()
                    + "to" + dest.getAbsolutePath() + ".");
            wrapper.initCause(e);
            wrapper.setStackTrace(e.getStackTrace());
            throw wrapper;
        } finally { // Ensure that the files are closed (if they were open).
            if (fin != null) {
                fin.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.CachingBlockSender.java

/**
 * Converts an IOExcpetion (not subclasses) to SocketException.
 * This is typically done to indicate to upper layers that the error
 * was a socket error rather than often more serious exceptions like
 * disk errors./*www  . ja  v a2  s.  c o m*/
 */
private static IOException ioeToSocketException(IOException ioe) {

    if (ioe.getClass().equals(IOException.class)) {
        // "se" could be a new class in stead of SocketException.
        final IOException se = new SocketException("Original Exception : " + ioe);
        se.initCause(ioe);
        /*
         * Change the stacktrace so that original trace is not truncated
         * when printed.
         */
        se.setStackTrace(ioe.getStackTrace());
        return se;
    }
    // otherwise just return the same exception.
    return ioe;
}

From source file:org.eclipse.ecr.core.io.impl.plugins.DocumentModelInjector.java

@Override
public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException {
    Path path = xdoc.getPath();//from ww  w  .j a  v a  2s .c  om
    if (path.isEmpty() || path.isRoot()) {
        return null; // TODO avoid to import the root
    }
    path = root.append(path); // compute target path

    try {
        DocumentModel doc = createDocument(xdoc, path);
        DocumentLocation source = xdoc.getSourceLocation();
        DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(),
                doc.getRepositoryName());
        map.put(source.getDocRef(), doc.getRef());
        return map;
    } catch (ClientException e) {
        IOException ioe = new IOException("Failed to import document in repository: " + e.getMessage());
        ioe.setStackTrace(e.getStackTrace());
        log.error(e, e);
        return null;
    }
}

From source file:org.eclipse.ecr.core.io.impl.plugins.DocumentModelUpdater.java

@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
@Override/*from  w w w  . jav  a 2  s .  c om*/
public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException {
    if (xdoc.getDocument() == null) {
        // not a valid doc -> this may be a regular folder for example the
        // root of the tree
        return null;
    }

    DocumentModel doc = null;
    String id = xdoc.getId();
    try {
        doc = session.getDocument(new IdRef(id));
    } catch (Exception e) {
        log.error("Cannot update document. No such document: " + id);
        return null;
    }

    try {
        doc = updateDocument(xdoc, doc);
        DocumentLocation source = xdoc.getSourceLocation();
        DocumentTranslationMap map = new DocumentTranslationMapImpl(source.getServerName(),
                doc.getRepositoryName());
        map.put(source.getDocRef(), doc.getRef());
        return map;
    } catch (ClientException e) {
        IOException ioe = new IOException("Failed to import document in repository: " + e.getMessage());
        ioe.setStackTrace(e.getStackTrace());
        log.error(e);
        return null;
    }
}

From source file:org.eclipse.ecr.core.io.impl.plugins.DocumentModelWriter.java

@Override
public DocumentTranslationMap write(ExportedDocument xdoc) throws IOException {
    if (xdoc.getDocument() == null) {
        // not a valid doc -> this may be a regular folder for example the
        // root of the tree
        return null;
    }//w  ww .ja  v a  2s .  c  o m
    Path path = xdoc.getPath();
    //        if (path.isEmpty() || path.isRoot()) {
    //            return; // TODO avoid to import the root
    //        }
    path = root.append(path); // compute target path

    try {
        return doWrite(xdoc, path);
    } catch (ClientException e) {
        IOException ioe = new IOException("Failed to import document in repository: " + e.getMessage());
        ioe.setStackTrace(e.getStackTrace());
        log.error(e, e);
        return null;
    }
}

From source file:org.apache.pig.backend.local.executionengine.POStore.java

@Override
public Tuple getNext() throws IOException {
    // get all tuples from input, and store them.
    DataBag b = BagFactory.getInstance().newDefaultBag();
    Tuple t;/*from  w  ww . j  av  a2  s.  c  om*/
    while ((t = (Tuple) ((PhysicalOperator) opTable.get(inputs[0])).getNext()) != null) {
        b.add(t);
    }
    try {
        StoreFunc func = (StoreFunc) PigContext.instantiateFuncFromSpec(funcSpec);
        f.store(b, func, pigContext);

        // a result has materialized, track it!
        LocalResult materializedResult = new LocalResult(this.outFileSpec);

        materializedResults.put(logicalKey, materializedResult);
    } catch (IOException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        IOException ne = new IOException(e.getClass().getName() + ": " + e.getMessage());
        ne.setStackTrace(e.getStackTrace());
        throw ne;
    }

    return null;
}

From source file:org.apache.pig.impl.logicalLayer.LOStore.java

public void setOutputFile(FileSpec outputFileSpec) throws IOException {
    try {/*from w ww.  j a va 2  s.c o  m*/
        mStoreFunc = (StoreFuncInterface) PigContext.instantiateFuncFromSpec(outputFileSpec.getFuncSpec());
    } catch (Exception e) {
        IOException ioe = new IOException(e.getMessage());
        ioe.setStackTrace(e.getStackTrace());
        throw ioe;
    }
    mOutputFile = outputFileSpec;
}