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.google.mr4c.content.AbstractContentFactory.java

public String readContentAsString(URI uri) throws IOException {
    Reader reader = readContentAsReader(uri);
    try {/*from  www . j a va  2s . c  om*/
        return IOUtils.toString(reader);
    } finally {
        reader.close();
    }
}

From source file:br.com.pontocontrol.controleponto.controller.impl.ArquivoController.java

@Override
public boolean fecharArquivoLeitura(Reader reader) {
    try {/*from  w  w w . j  av a  2 s . co  m*/
        reader.close();
        return true;
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, "Erro ao fechar leitor de arquivos", ex);
        return false;
    }
}

From source file:mitm.common.util.MetaReaderTest.java

@SuppressWarnings("unchecked")
@Test//from ww w  .  j a  va  2  s  .c o m
public void testMetaReader() throws IOException {
    File file1 = new File(base, "other/ends-with-lf.txt");
    File file2 = new File(base, "other/ends-with-text.txt");

    Collection<Reader> readers = new LinkedList<Reader>();

    readers.add(new FileReader(file1));
    readers.add(new FileReader(file2));

    Reader metaReader = new MetaReader(readers);

    List<String> lines = IOUtils.readLines(metaReader);

    assertEquals(15, lines.size());
    assertEquals("123", lines.get(14));

    metaReader.close();
}

From source file:com.google.mr4c.content.AbstractContentFactory.java

public Properties readContentAsProperties(URI uri) throws IOException {
    Properties props = new Properties();
    Reader reader = readContentAsReader(uri);
    try {/*from ww w.  j  a  v a2s. co m*/
        props.load(reader);
        return props;
    } finally {
        reader.close();
    }
}

From source file:org.springframework.restdocs.payload.FieldSnippetResultHandler.java

@SuppressWarnings("unchecked")
private Map<String, Object> extractPayload(MvcResult result) throws IOException {
    Reader payloadReader = getPayloadReader(result);
    try {/*from   w  ww  . j  av  a2s . c  o m*/
        return this.objectMapper.readValue(payloadReader, Map.class);
    } finally {
        payloadReader.close();
    }
}

From source file:net.hamnaberg.json.parser.CollectionParser.java

public Collection parse(Reader reader) throws IOException {
    try {/*from ww  w  .  j av  a  2  s  . c o  m*/
        return parse(mapper.readTree(reader));
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:im.vector.util.BugReporter.java

/**
 * Read the file content as String/*from ww w . j  ava 2s.  c  om*/
 *
 * @param fin the input file
 * @return the file content as String
 */
private static String convertStreamToString(File fin) {
    Reader reader = null;

    try {
        Writer writer = new StringWriter();
        InputStream inputStream = new FileInputStream(fin);
        try {
            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            int n;

            char[] buffer = new char[2048];
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            try {
                if (null != reader) {
                    reader.close();
                }
            } catch (Exception e) {
                Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
            }
        }
        return writer.toString();
    } catch (Exception e) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + e.getMessage());
    } catch (OutOfMemoryError oom) {
        Log.e(LOG_TAG, "## convertStreamToString() failed " + oom.getMessage());
    } finally {
        try {
            if (null != reader) {
                reader.close();
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, "## convertStreamToString() failed to close inputStream " + e.getMessage());
        }
    }

    return "";
}

From source file:org.openmrs.module.logmanager.web.controller.ConfigViewerController.java

/**
 * @see org.springframework.web.servlet.mvc.ParameterizableViewController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 *//*from   ww  w .j ava 2s.co m*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    Map<String, Object> model = new HashMap<String, Object>();
    Document document = null;
    String filename = "log4j.xml";
    String source = request.getParameter("src");
    if (source == null)
        source = "current";

    if (source.equals("internal")) {
        // Get internal configuration
        URL url = ConfigurationManager.class.getResource("/" + Constants.INTERNAL_CONFIG_NAME);
        Reader reader = new InputStreamReader(url.openStream());
        document = LogManagerUtils.readDocument(reader, new Log4jEntityResolver());
        reader.close();
    } else if (source.equals("external")) {
        String path = OpenmrsUtil.getApplicationDataDirectory() + File.separator
                + Constants.EXTERNAL_CONFIG_NAME;
        File file = new File(path);
        if (file.exists()) {
            Reader reader = new FileReader(file);
            document = LogManagerUtils.readDocument(reader, new Log4jEntityResolver());
            reader.close();
            filename = Constants.EXTERNAL_CONFIG_NAME;
        }
    } else if (source.equals("current")) {
        // Get the current configuration
        document = ConfigurationBuilder.currentConfiguration();
    } else {
        // Look for a module with that as its id
        Module module = ModuleFactory.getModuleById(source);
        if (module != null)
            document = module.getLog4j();
    }

    model.put(xmlView.getSourceKey(), document);
    model.put(xmlView.getFilenameKey(), filename);

    return new ModelAndView(xmlView, model);
}

From source file:com.google.mr4c.sources.ConfiguredDiffSource.java

public void loadConfig() throws IOException {
    s_log.info("Reading diff config from [{}]", m_confFile);
    ConfigSerializer ser = SerializerFactories.getSerializerFactory("application/json")
            .createConfigSerializer(); // assume json config for now
    ser = new ParameterizedConfigSerializer(ser);

    Reader reader = ContentFactories.readContentAsReader(m_confFile);
    try {//  ww  w . j  a v a 2  s  .co m
        m_diffConfig = ser.deserializeDiffConfig(reader);
    } finally {
        reader.close();
    }
}

From source file:fr.opensagres.xdocreport.core.io.IOUtils.java

/**
 * Unconditionally close an <code>Reader</code>.
 * <p>/* w w  w.jav a  2  s.c  o  m*/
 * Equivalent to {@link Reader#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * 
 * @param input the Reader to close, may be null or already closed
 */
public static void closeQuietly(Reader input) {
    try {
        if (input != null) {
            input.close();
        }
    } catch (IOException ioe) {
        logger.warning("Reader - exception ignored - exception: " + ioe); //$NON-NLS-1$

        // ignore
    }
}