Example usage for org.apache.commons.io IOUtils closeQuietly

List of usage examples for org.apache.commons.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(OutputStream output) 

Source Link

Document

Unconditionally close an OutputStream.

Usage

From source file:com.jsuper.compiler.parser.InputSource.java

private char[] readFile(File file) throws IOException {
    char[] result = null;

    InputStream input = null;/*from   w w  w . j  a  v a 2s .c  o  m*/
    try {
        input = new BufferedInputStream(new FileInputStream(file));
        result = IOUtils.toCharArray(input);
    } finally {
        IOUtils.closeQuietly(input);
    }

    return result;
}

From source file:lyonlancer5.pfsteel.util.ModUpdater.java

@Override
public void run() {
    InputStream in = null;/*  w  w w.j  a  v  a2  s .  co m*/
    try {
        in = new URL(urlUpdateFile).openStream();
        LL5_PerfectSteel.pfsLogger.info("Checking for updates...");
    } catch (Exception e) {
        LL5_PerfectSteel.pfsLogger
                .info("The update checker encountered an error while checking for updates : " + e.toString());
        return;
    }

    try {
        updateParams = IOUtils.readLines(in);
        latestVersion = Integer.parseInt(updateParams.get(0));
        latestVersionName = updateParams.get(2);
        forumThread = updateParams.get(3);
    } catch (Exception e) {
        LL5_PerfectSteel.pfsLogger
                .info("The update checker encountered an error while checking for updates : " + e.toString());
        return;
    } finally {
        IOUtils.closeQuietly(in);
    }

    LL5_PerfectSteel.pfsLogger.info("Got version info from GitHub - " + latestVersionName);
    isLatestVersion = ModVersion.getBuildNumber() == latestVersion;

    if (!isLatestVersion) {
        if (ModVersion.getBuildNumber() > latestVersion) {
            LL5_PerfectSteel.pfsLogger.info(
                    "The build number specified in this version is greater than the one specified on the update!");
            LL5_PerfectSteel.pfsLogger.info("Are you running a custom version?");
        } else {
            LL5_PerfectSteel.pfsLogger.info("There is a new update available!");
        }
    } else {
        LL5_PerfectSteel.pfsLogger.info("No updates available");
    }

    hasChecked = true;
}

From source file:com.ait.tooling.server.core.json.binder.JSONBinder.java

@Override
public void send(final File file, final Object object) throws ParserException {
    Objects.requireNonNull(object);

    try {/*from   w w  w .  ja  v  a 2 s. c  o m*/
        if (object instanceof JSONObject) {
            final Writer writer = new FileWriter(file);

            ((JSONObject) object).writeJSONString(writer, isStrict());

            IOUtils.closeQuietly(writer);
        } else {
            super.send(file, object);
        }
    } catch (Exception e) {
        throw new ParserException(e);
    }
}

From source file:com.casker.portfolio.controller.FileController.java

/**
 *  //from  www.j  a v a2s. c  o m
 * 
 * @return
 */
@ResponseBody
@RequestMapping("/portfolio/{portfolioNo}/{imageType}")
public void editPassword(HttpServletResponse response, @PathVariable long portfolioNo,
        @PathVariable String imageType) throws Exception {

    Portfolio portfolio = portfolioService.getPortfolioDetail(portfolioNo);

    File file = portfolioService.getImageFile(portfolio, imageType);

    response.setContentLength((int) file.length());

    String fileName = URLEncoder.encode(file.getName(), "utf-8");

    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";");
    response.setHeader("Content-Transfer-Encoding", "binary");

    InputStream inputStream = new FileInputStream(file);
    IOUtils.copy(inputStream, response.getOutputStream());
    IOUtils.closeQuietly(inputStream);

    response.flushBuffer();
}

From source file:com.atlassian.connector.eclipse.team.ui.AbstractTeamUiConnector.java

protected byte[] getResourceContent(InputStream is) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {/*from   w w  w  . j a va  2  s.  co m*/
        IOUtils.copy(is, out);
        return out.toByteArray();
    } catch (IOException e) {
        return new byte[0];
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.eden.image.simpleimage.ScaleRenderTest.java

public void write(ImageRender sr) throws Exception {
    OutputStream output = null;/*from   w w w . ja va2s. co  m*/
    ImageRender wr = null;
    try {
        output = new FileOutputStream(new File("C:/Users/eden/Desktop/1245.jpg"));
        wr = new WriteRender(sr, output, ImageFormat.JPEG);
        wr.render();
    } finally {
        if (wr != null) {
            wr.dispose();
        }
        IOUtils.closeQuietly(output);
    }
}

From source file:com.googlecode.wickedcharts.showcase.StringFromResourceModel.java

public StringFromResourceModel(Class<?> scope, String resourceName) {
    InputStream in = null;//from   w  w  w  .j av  a 2 s. c o  m
    BufferedReader reader = null;
    try {
        in = scope.getResourceAsStream(resourceName);
        reader = new BufferedReader(new InputStreamReader(in));
        StringBuffer stringBuffer = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            stringBuffer.append(line);
            stringBuffer.append("\n");
        }
        this.modelObject = stringBuffer.toString();
    } catch (IOException e) {
        throw new RuntimeException(
                "Error reading resource '" + resourceName + "' from class '" + scope.getName() + "'.", e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(reader);
    }
}

From source file:com.tc.util.factory.AbstractFactory.java

private static String findFactoryClassName(String id) {
    String serviceId = "META-INF/services/" + id;
    InputStream is = null;//from  w w  w .  j  a  va2s. co m

    try {
        ClassLoader cl = AbstractFactory.class.getClassLoader();
        if (cl != null) {
            is = cl.getResourceAsStream(serviceId);
        }

        if (is == null) {
            return System.getProperty(id);
        }

        BufferedReader rd;
        try {
            rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        String factoryClassName = null;
        try {
            factoryClassName = rd.readLine();
            rd.close();
        } catch (IOException x) {
            return System.getProperty(id);
        }

        if (factoryClassName != null && !"".equals(factoryClassName)) {
            return factoryClassName;
        }

        return System.getProperty(id);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.ctriposs.r2.filter.compression.Bzip2Compressor.java

@Override
public byte[] inflate(InputStream data) throws CompressionException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BZip2CompressorInputStream bzip2 = null;

    try {/*  w  ww .  j  a  v  a 2s .c om*/
        bzip2 = new BZip2CompressorInputStream(data);
        IOUtils.copy(bzip2, out);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
    } finally {
        if (bzip2 != null) {
            IOUtils.closeQuietly(bzip2);
        }
    }

    return out.toByteArray();
}

From source file:com.gameminers.mav.firstrun.TeachSphinxThread.java

@Override
public void run() {
    try {// www .j  a  v  a  2 s.  c  o  m
        File training = new File(Mav.configDir, "training-data");
        training.mkdirs();
        while (Mav.silentFrames < 30) {
            sleep(100);
        }
        Mav.listening = true;
        InputStream prompts = ClassLoader.getSystemResourceAsStream("resources/sphinx/train/arcticAll.prompts");
        List<String> arctic = IOUtils.readLines(prompts);
        IOUtils.closeQuietly(prompts);
        Mav.audioManager.playClip("listen1");
        byte[] buf = new byte[2048];
        int start = 0;
        int end = 21;
        AudioInputStream in = Mav.audioManager.getSource().getAudioInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while (true) {
            for (int i = start; i < end; i++) {
                baos.reset();
                String prompt = arctic.get(i);
                RenderState.setText("\u00A7LRead this aloud:\n" + Fonts.wrapStringToFit(
                        prompt.substring(prompt.indexOf(':') + 1), Fonts.base[1], Display.getWidth()));
                File file = new File(training, prompt.substring(0, prompt.indexOf(':')) + ".wav");
                file.createNewFile();
                int read = 0;
                while (Mav.silentListenFrames > 0) {
                    read = Mav.audioManager.getSource().getAudioInputStream().read(buf);
                }
                baos.write(buf, 0, read);
                while (Mav.silentListenFrames < 60) {
                    in.read(buf);
                    if (read == -1) {
                        RenderState.setText(
                                "\u00A7LAn error occurred\nUnexpected end of stream\nPlease restart Mav");
                        RenderState.targetHue = 0;
                        return;
                    }
                    baos.write(buf, 0, read);
                }
                AudioSystem.write(new AudioInputStream(new ByteArrayInputStream(baos.toByteArray()),
                        in.getFormat(), baos.size() / 2), AudioFileFormat.Type.WAVE, file);
                Mav.audioManager.playClip("notif2");
            }
            Mav.ttsInterface.say(Mav.phoneticUserName
                    + ", that should be enough for now. Do you want to keep training anyway?");
            RenderState.setText("\u00A7LOkay, " + Mav.userName
                    + "\nI think that should be\nenough. Do you want to\nkeep training anyway?\n\u00A7s(Say 'Yes' or 'No' out loud)");
            break;
            //start = end+1;
            //end += 20;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}