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:org.limy.common.xml.XmlUtils.java

/**
 * XML??DOM Document???//from w  ww .j a v  a2s .  com
 * @param in XML
 * @return DOM Document
 * @throws IOException I/O
 */
public static Document parseDoc(InputStream in) throws IOException {
    try {
        return createBuilder().parse(in);
    } catch (SAXException e) {
        IOException exception = new IOException(e.getMessage());
        exception.setStackTrace(e.getStackTrace());
        throw exception;
    }
}

From source file:org.limy.common.xml.XmlUtils.java

/**
 * XML??XMLElement???//www .j  a v a 2  s .com
 * @param in XML
 * @return XMLElement
 * @throws IOException I/O
 */
public static XmlElement parse(InputStream in) throws IOException {
    try {
        Document document = createBuilder().parse(in);
        Element root = document.getDocumentElement();
        SimpleElement el = new SimpleElement(root.getNodeName());
        parse(el, root);
        return el;
    } catch (SAXException e) {
        IOException exception = new IOException(e.getMessage());
        exception.setStackTrace(e.getStackTrace());
        throw exception;
    }
}

From source file:ly.stealth.punxsutawney.Marathon.java

private static JSONObject sendRequest(String uri, String method, JSONObject json) throws IOException {
    URL url = new URL(Marathon.url + uri);
    HttpURLConnection c = (HttpURLConnection) url.openConnection();
    try {//from  ww w. j  a  v a2s .c om
        c.setRequestMethod(method);

        if (method.equalsIgnoreCase("POST")) {
            byte[] body = json.toString().getBytes("utf-8");
            c.setDoOutput(true);
            c.setRequestProperty("Content-Type", "application/json");
            c.setRequestProperty("Content-Length", "" + body.length);
            c.getOutputStream().write(body);
        }

        return (JSONObject) JSONValue.parse(new InputStreamReader(c.getInputStream(), "utf-8"));
    } catch (IOException e) {
        if (c.getResponseCode() == 404 && method.equals("GET"))
            return null;

        ByteArrayOutputStream response = new ByteArrayOutputStream();
        InputStream err = c.getErrorStream();
        if (err == null)
            throw e;

        Util.copyAndClose(err, response);
        IOException ne = new IOException(e.getMessage() + ": " + response.toString("utf-8"));
        ne.setStackTrace(e.getStackTrace());
        throw ne;
    } finally {
        c.disconnect();
    }
}

From source file:org.nuxeo.ecm.platform.audit.io.IOLogEntryBase.java

private static Document loadXML(InputStream in) throws IOException {
    try {//from  w  ww . jav  a  2  s.  com
        // the SAXReader is closing the stream so that we need to copy the
        // content somewhere
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileUtils.copy(in, baos);
        return new SAXReader().read(new ByteArrayInputStream(baos.toByteArray()));
    } catch (DocumentException e) {
        IOException ioe = new IOException("Failed to read log entry " + ": " + e.getMessage());
        ioe.setStackTrace(e.getStackTrace());
        throw ioe;
    }
}

From source file:org.apache.hadoop.mapreduce.v2.jobhistory.FileNameIndexUtils.java

/**
 * Helper function to decode the URL of the filename of the job-history
 * log file./* w w  w  . j ava2  s  .  c  o  m*/
 * 
 * @param logFileName file name of the job-history file
 * @return URL decoded filename
 * @throws IOException
 */
public static String decodeJobHistoryFileName(String logFileName) throws IOException {
    String decodedFileName = null;
    try {
        decodedFileName = URLDecoder.decode(logFileName, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        IOException ioe = new IOException();
        ioe.initCause(uee);
        ioe.setStackTrace(uee.getStackTrace());
        throw ioe;
    }
    return decodedFileName;
}

From source file:de.ingrid.iplug.csw.dsc.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.
 * @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.
 * // w w  w. ja va  2 s .  c om
 * 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 (int i = 0; i < list.length; i++) {
            File dest1 = new File(dest, list[i]);
            File src1 = new File(src, list[i]);
            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.mapreduce.v2.jobhistory.FileNameIndexUtils.java

/**
 * Helper function to encode the URL of the filename of the job-history
 * log file./*www .j a v  a  2 s .  co m*/
 * 
 * @param logFileName file name of the job-history file
 * @return URL encoded filename
 * @throws IOException
 */
public static String encodeJobHistoryFileName(String logFileName) throws IOException {
    String replacementDelimiterEscape = null;

    // Temporarily protect the escape delimiters from encoding
    if (logFileName.contains(DELIMITER_ESCAPE)) {
        replacementDelimiterEscape = nonOccursString(logFileName);

        logFileName = logFileName.replaceAll(DELIMITER_ESCAPE, replacementDelimiterEscape);
    }

    String encodedFileName = null;
    try {
        encodedFileName = URLEncoder.encode(logFileName, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        IOException ioe = new IOException();
        ioe.initCause(uee);
        ioe.setStackTrace(uee.getStackTrace());
        throw ioe;
    }

    // Restore protected escape delimiters after encoding
    if (replacementDelimiterEscape != null) {
        encodedFileName = encodedFileName.replaceAll(replacementDelimiterEscape, DELIMITER_ESCAPE);
    }

    return encodedFileName;
}

From source file:morphy.utils.FileUtils.java

/**
 * This code was obtained from://from   w  w  w  .j  av a 2 s.c  om
 * http://www.dreamincode.net/code/snippet1443.htm
 * 
 * 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.
 * 
 * @param src
 *            -- A File object that represents the source for the copy
 * @param dest
 *            -- A File object that represents the destination for the copy.
 * @throws IOException
 *             if unable to copy.
 */
public static void copyFiles(File src, File dest) throws IOException {
    if (src.getName().startsWith(".")) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Ignoring " + src.getAbsolutePath() + " because name started with .");
        }
        return;
    }

    // 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() + ".");
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Created directory " + 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);
            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);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Copied " + src.getAbsolutePath() + " to " + dest.getAbsolutePath());
            }

        } 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) {
                try {
                    fin.close();
                } catch (Throwable t) {
                }
            }

            if (fout != null) {
                try {
                    fout.close();
                } catch (Throwable t) {
                }
            }
        }
    }
}

From source file:org.apache.hadoop.hdfs.tools.DelegationTokenFetcher.java

static private IOException getExceptionFromResponse(String resp) {
    String exceptionClass = "", exceptionMsg = "";
    if (resp != null && !resp.isEmpty()) {
        String[] rs = resp.split(";");
        exceptionClass = rs[0];/*from w  w  w .  j a  v  a 2s . co  m*/
        exceptionMsg = rs[1];
    }
    LOG.info("Error response from HTTP request=" + resp + ";ec=" + exceptionClass + ";em=" + exceptionMsg);
    IOException e = null;
    if (exceptionClass != null && !exceptionClass.isEmpty()) {
        if (exceptionClass.contains("InvalidToken")) {
            e = new org.apache.hadoop.security.token.SecretManager.InvalidToken(exceptionMsg);
            e.setStackTrace(new StackTraceElement[0]); // stack is not relevant
        } else if (exceptionClass.contains("AccessControlException")) {
            e = new org.apache.hadoop.security.AccessControlException(exceptionMsg);
            e.setStackTrace(new StackTraceElement[0]); // stack is not relevant
        }
    }
    LOG.info("Exception from HTTP response=" + e.getLocalizedMessage());
    return e;
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.ZKStore.java

private static IOException convertToIOException(KeeperException ke) {
    IOException io = new IOException();
    io.setStackTrace(ke.getStackTrace());
    return io;/*  ww w .  j  a  va 2 s .  c o  m*/
}