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.lexicalintelligence.admin.add.AddEntryRequest.java

public AddResponse execute() {
    List<BasicNameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("name", entry.getName()));
    params.add(new BasicNameValuePair("synonym", entry.getSynonym()));
    params.add(new BasicNameValuePair("matchWordOrder", Boolean.toString(entry.isOrderSensitive())));
    params.add(new BasicNameValuePair("caseSensitive", Boolean.toString(entry.isCaseSensitive())));
    params.add(new BasicNameValuePair("matchStopwords", Boolean.toString(entry.isMatchStopwords())));
    params.add(new BasicNameValuePair("matchPunctuation", Boolean.toString(entry.isMatchPunctuation())));
    params.add(new BasicNameValuePair("stem", Boolean.toString(entry.isStemmed())));

    AddResponse addResponse;//from  w w w .j  a  va2  s  . co  m
    Reader reader = null;
    try {
        HttpResponse response = client.getHttpClient().execute(new HttpGet(client.getUrl() + PATH + getPath()
                + "?" + URLEncodedUtils.format(params, StandardCharsets.UTF_8)));
        reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
        addResponse = new AddResponse(Boolean.valueOf(IOUtils.toString(reader)));
    } catch (Exception e) {
        addResponse = new AddResponse(false);
        log.error(e);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            log.error(e);
        }
    }
    return addResponse;
}

From source file:com.lexicalintelligence.admin.remove.RemoveEntryRequest.java

@Override
public RemoveResponse execute() {
    RemoveResponse removeResponse;/*  w w  w. j a v a 2s  .  co m*/

    List<BasicNameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("name", entry.getName()));
    params.add(new BasicNameValuePair("synonym", entry.getSynonym()));

    Reader reader = null;
    try {
        HttpResponse response = client.getHttpClient().execute(new HttpGet(client.getUrl() + PATH + getPath()
                + "?" + URLEncodedUtils.format(params, StandardCharsets.UTF_8)));
        reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
        removeResponse = new RemoveResponse(Boolean.valueOf(IOUtils.toString(reader)));
    } catch (Exception e) {
        removeResponse = new RemoveResponse(false);
        log.error(e);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {

        }
    }
    return removeResponse;
}

From source file:com.varaneckas.hawkscope.plugin.PluginManager.java

private void processPlugin(final File pluginDir, final String jar) {
    try {//from   w  ww  . j a va  2  s .c  o m
        final File jarFile = new File(pluginDir.getAbsolutePath() + "/" + jar);
        final URLClassLoader classLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() });
        final Reader r = new InputStreamReader(classLoader.getResourceAsStream("plugin.loader"));
        String pluginClass = "";
        int c = 0;
        while ((c = r.read()) != -1) {
            pluginClass += (char) c;
        }
        r.close();
        log.debug("Processing Plugin: " + pluginClass);
        if (!isPluginEnabled(pluginClass)) {
            log.debug("Plugin disabled, skipping");
            getAllPlugins().add(new DisabledPlugin(pluginClass));
            return;
        }
        Class<?> p = classLoader.loadClass(pluginClass);
        Method creator = null;
        try {
            creator = p.getMethod("getInstance", new Class[] {});
        } catch (final NoSuchMethodException no) {
            log.debug("Plugin does not implement a singleton getter");
        }
        Plugin plugin;
        if (creator == null) {
            plugin = (Plugin) p.newInstance();
        } else {
            plugin = (Plugin) creator.invoke(p, new Object[] {});
        }
        creator = null;
        p = null;
        if (plugin != null) {
            log.debug("Adding plugin: " + plugin);
            getAllPlugins().add(plugin);
        }
    } catch (final Exception e) {
        log.warn("Failed loading plugin: " + jar, e);
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.FileListReader.java

public List<String> readFileWithList(InputStream is) throws IOException {
    Reader isr = new InputStreamReader(is);
    BufferedReader in = new BufferedReader(isr);
    try {// ww w  . j  av  a 2s  .  c  o m
        while (in.ready()) {
            try {
                final String text = in.readLine().trim();
                if (text.trim().length() > 0 && !text.trim().equals("->")) {
                    fileToReadList.add(text.trim());
                }
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
        }
    } finally {
        try {
            in.close();
            isr.close();
            is.close();
            in = null;
            isr = null;
            is = null;
        } catch (IOException ignored) {
            //Do nothing here just keep going
        }
    }
    return fileToReadList;
}

From source file:com.mhise.util.MHISEUtil.java

public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException {

    if (entity == null) {
        throw new IllegalArgumentException("HTTP entity may not be null");
    }//w  w  w . ja va 2  s.c om

    InputStream instream = entity.getContent();

    if (instream == null) {
        return "";
    }

    if (entity.getContentLength() > Integer.MAX_VALUE) {
        throw new IllegalArgumentException(

                "HTTP entity too large to be buffered in memory");
    }

    String charset = getContentCharSet(entity);

    if (charset == null) {

        charset = HTTP.DEFAULT_CONTENT_CHARSET;

    }

    Reader reader = new InputStreamReader(instream, charset);

    StringBuilder buffer = new StringBuilder();

    try {

        char[] tmp = new char[1024];

        int l;

        while ((l = reader.read(tmp)) != -1) {

            buffer.append(tmp, 0, l);

        }

    } finally {

        reader.close();

    }

    return buffer.toString();

}

From source file:name.livitski.tools.proper2.Configuration.java

/**
 * Reads configuration settings from a properties file with optional
 * defaults.//from  w ww .  j a  v a  2s  .  c  o  m
 * @return configuration settings map
 * @throws ConfigurationException if there was an error reading the
 * settings
 */
protected Properties readConfigurationFromFile() throws ConfigurationException {
    Reader input = null;
    try {
        final Properties defaults = new Properties();
        InputStream res = null == defaultsResource ? null : forClass.getResourceAsStream(defaultsResource);
        if (null != res) {
            input = new InputStreamReader(res);
            try {
                defaults.load(input);
                input.close();
                input = null;
            } catch (IOException ioerr) {
                throw new ConfigurationException(
                        "Error reading configuration defaults from " + forClass.getResource(defaultsResource),
                        ioerr);
            }
        }
        Properties config = new Properties(defaults);
        if (null != configFile)
            try {
                input = new FileReader(configFile);
                config.load(input);
                input.close();
                input = null;
            } catch (IOException ioerr) {
                throw new ConfigurationException("Error reading configuration file " + configFile, ioerr);
            }
        return config;
    } finally {
        if (null != input)
            try {
                input.close();
            } catch (Exception fail) {
                log.warn("Could not close a configuration file: " + fail.getMessage(), fail);
            }
    }
}

From source file:com.zyeeda.framework.template.internal.FreemarkerTemplateServiceProvider.java

@Override
public String render(String template, Map<String, Object> args) throws IOException {
    Reader reader = null;
    Writer writer = null;/*from  w  w w. ja v  a 2s. c  o  m*/
    try {
        reader = new StringReader(template);
        Template tpl = new Template(null, reader, this.config);
        writer = new StringWriter();
        this.putBuildinVariables(args);
        tpl.process(args, writer);
        writer.flush();
        return writer.toString();
    } catch (TemplateException e) {
        throw new TemplateServiceException(e);
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.lexicalintelligence.admin.get.ListGetRequest.java

@SuppressWarnings("unchecked")
public <T extends LexicalResponse> T execute() {
    ListGetResponse searchResponse;/*from www. ja v a 2  s  . co m*/
    Reader reader = null;
    try {
        HttpResponse response = client.getHttpClient().execute(new HttpGet(client.getUrl() + PATH + getPath()));
        reader = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8);
        List<String> items = client.getObjectMapper().readValue(reader, new TypeReference<List<String>>() {
        });
        searchResponse = new ListGetResponse(true);
        searchResponse.setItems(items);
    } catch (Exception e) {
        searchResponse = new ListGetResponse(false);
        log.error(e);
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
            log.error(e);
        }
    }
    return (T) searchResponse;
}

From source file:com.isencia.passerelle.runtime.repos.impl.filesystem.FlowRepositoryServiceImpl.java

/**
 * @param flowCode//  www.j  a v  a2s  .com
 * @return metadata props for the given flow; returns empty properties when no metadata is found
 * @throws EntryNotFoundException
 */
private Properties readMetaData(String flowCode) throws EntryNotFoundException {
    Properties flowMetaDataProps = new Properties();
    File flowRootFolder = new File(rootFolder, flowCode);
    File metaDataFile = new File(flowRootFolder, ".metadata");
    if (!flowRootFolder.isDirectory()) {
        throw new EntryNotFoundException("Flow not managed by this repository " + flowCode);
    } else {
        if (metaDataFile.exists()) {
            Reader metaDataReader = null;
            try {
                metaDataReader = new FileReader(metaDataFile);
                flowMetaDataProps.load(metaDataReader);
            } catch (Exception e) {

            } finally {
                if (metaDataReader != null) {
                    try {
                        metaDataReader.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        return flowMetaDataProps;
    }
}