Example usage for java.io Reader close

List of usage examples for java.io Reader close

Introduction

In this page you can find the example usage for java.io Reader close.

Prototype

public abstract void close() throws IOException;

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:com.walmartlabs.mupd8.application.Config.java

private JSONObject applyFiles(JSONObject destination, File... configFiles) throws IOException {
    for (File file : configFiles) {
        Reader reader = new FileReader(file);
        // TODO Switch to try (Reader reader = new FileReader(file)) { ... } for Java 7.
        try {/*from   w  w w .j av a 2  s .  c o m*/
            JSONObject parsedFile = (JSONObject) JSONValue.parse(readWithPreprocessing(reader));
            apply(destination, parsedFile);
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
    return destination;
}

From source file:org.osiam.client.OsiamUserMeTest.java

private User get_expected_user() throws Exception {
    Reader reader = null;
    StringBuilder jsonUser = null;
    User expectedUser;// w w  w  . ja v a2 s.  c om
    try {
        reader = new FileReader("src/test/resources/__files/user_" + USER_ID + ".json");
        jsonUser = new StringBuilder();
        for (int c; (c = reader.read()) != -1;)
            jsonUser.append((char) c);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }

    expectedUser = new ObjectMapper().readValue(jsonUser.toString(), User.class);
    return expectedUser;
}

From source file:io.github.jeddict.jcode.parser.ejs.EJSParser.java

public Consumer<FileTypeStream> getParserManager(List<String> skipFile) {
    return (fileType) -> {
        try {// ww w.  j a  v a  2s.c om
            if (SKIP_FILE_TYPE.contains(fileType.getFileType())
                    || (skipFile != null && skipFile.contains(fileType.getFileName()))
                    || fileType.isSkipParsing()) {
                IOUtils.copy(fileType.getInputStream(), fileType.getOutputStream());
                if (!(fileType.getInputStream() instanceof ZipInputStream)) {
                    fileType.getInputStream().close();
                }
                fileType.getOutputStream().close();
            } else {
                Charset charset = Charset.forName("UTF-8");
                Reader reader = new BufferedReader(new InputStreamReader(fileType.getInputStream(), charset));
                Writer writer = new BufferedWriter(new OutputStreamWriter(fileType.getOutputStream(), charset));
                IOUtils.write(parse(reader), writer);
                if (!(fileType.getInputStream() instanceof ZipInputStream)) {
                    reader.close();
                }
                writer.flush();
                writer.close();
            }

        } catch (ScriptException | IOException ex) {
            Exceptions.printStackTrace(ex);
            System.out.println("Error in template : " + fileType.getFileName());
        }
    };
}

From source file:com.github.jknack.handlebars.TemplateLoader.java

/**
 * Load the template as string from a template repository.
 *
 * @param uri The resource's uri. Required.
 * @return The requested resource./*from w  w w . j  a  v  a2 s . com*/
 * @throws IOException If the resource cannot be loaded.
 */
public String loadAsString(final URI uri) throws IOException {
    Reader reader = new BufferedReader(load(uri));
    final int bufferSize = 1024;
    try {
        char[] cbuf = new char[bufferSize];
        StringBuilder sb = new StringBuilder(bufferSize);
        int len;
        while ((len = reader.read(cbuf, 0, bufferSize)) != -1) {
            sb.append(cbuf, 0, len);
        }
        return sb.toString();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:com.daphne.es.maintain.staticresource.web.controller.utils.YuiCompressorUtils.java

/**
 * js/css  ??.min.js/css/*from  w  w  w.  ja va 2s  .  co m*/
 * @param fileName
 * @return ??
 */
public static String compress(final String fileName) {
    String minFileName = null;
    final String extension = FilenameUtils.getExtension(fileName);
    if ("js".equalsIgnoreCase(extension)) {
        minFileName = fileName.substring(0, fileName.length() - 3) + ".min.js";
    } else if ("css".equals(extension)) {
        minFileName = fileName.substring(0, fileName.length() - 4) + ".min.css";
    } else {
        throw new RuntimeException(
                "file type only is js/css, but was fileName:" + fileName + ", extension:" + extension);
    }
    Reader in = null;
    Writer out = null;
    try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), Constants.ENCODING));
        out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(minFileName), Constants.ENCODING));

        if ("js".equals(extension)) {

            CustomErrorReporter errorReporter = new CustomErrorReporter();

            JavaScriptCompressor compressor = new JavaScriptCompressor(in, errorReporter);
            compressor.compress(out, 10, true, false, false, false);

            if (errorReporter.hasError()) {
                throw new RuntimeException(errorReporter.getErrorMessage());
            }

        } else if ("css".equals(extension)) {

            CssCompressor compressor = new CssCompressor(in);
            compressor.compress(out, 10);
        }
    } catch (Exception e) {
        //?js/css
        try {
            FileUtils.copyFile(new File(fileName), new File(minFileName));
        } catch (IOException ioException) {
            throw new RuntimeException("compress error:" + ioException.getMessage(), ioException);
        }
        throw new RuntimeException("compress error:" + e.getMessage(), e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }

    if (FileUtils.sizeOf(new File(minFileName)) == 0) {
        try {
            FileUtils.copyFile(new File(fileName), new File(minFileName));
        } catch (IOException ioException) {
            throw new RuntimeException("compress error:" + ioException.getMessage(), ioException);
        }
    }

    return minFileName;
}

From source file:com.plusub.lib.other.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;//from w  w  w .  j a v  a  2s.  c  om
    try {
        writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        try {
            reader.close();
            writer.close();
        } catch (Throwable e) {
        }
    }
}

From source file:Repackage.java

StringBuffer readInputStream(InputStream is) throws IOException {
    Reader r = new InputStreamReader(is);
    StringWriter w = new StringWriter();

    copy(r, w);/*from ww  w. j  ava  2s  . com*/

    w.close();
    r.close();

    return w.getBuffer();
}

From source file:Repackage.java

StringBuffer readFile(File f) throws IOException {
    InputStream in = new FileInputStream(f);
    Reader r = new InputStreamReader(in);
    StringWriter w = new StringWriter();

    copy(r, w);//from   w w w.  ja v a2 s .c o  m

    w.close();
    r.close();
    in.close();

    return w.getBuffer();
}

From source file:fr.paris.lutece.plugins.helpdesk.modules.solr.search.SolrHelpdeskIndexer.java

/**
 * Builds a {@link SolrItem} element which will be used by Solr during the indexing of the subject list
 *
 * @param subject the {@link Subject} to index
 * @param strUrl the url of the subject/*from   www  . j a v a  2 s . co m*/
 * @param strRoleKey The role key
 * @param plugin The {@link Plugin}
 * @return The Solr {@link SolrItem} containing Subject data
 * @throws IOException The IO Exception
 */
private SolrItem getDocument(Subject subject, String strRoleKey, String strUrl, Plugin plugin)
        throws IOException {
    // make a new, empty document
    SolrItem item = new SolrItem();

    // Setting the URL field
    item.setUrl(strUrl);

    // Setting the Uid field
    String strIdSubject = String.valueOf(subject.getId());
    item.setUid(getResourceUid(strIdSubject, HelpdeskIndexerUtils.CONSTANT_SUBJECT_TYPE_RESOURCE));

    //Setting the Content field
    String strContentToIndex = subject.getText();
    StringReader readerPage = new StringReader(strContentToIndex);
    HTMLParser parser = new HTMLParser(readerPage);

    Reader reader = parser.getReader();
    int c;
    StringBuffer sb = new StringBuffer();

    while ((c = reader.read()) != -1) {
        sb.append(String.valueOf((char) c));
    }

    reader.close();
    item.setContent(sb.toString());

    // Setting the Title field
    item.setTitle(subject.getText());

    // Setting the Site field
    item.setSite(SolrIndexerService.getWebAppName());

    // Setting the Type field
    item.setType(HelpdeskPlugin.PLUGIN_NAME);

    // Setting the Role field
    item.setRole(strRoleKey);

    // return the document
    return item;
}