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:au.com.gridstone.rxstore.converters.JacksonConverter.java

@Override
public <T> T read(File file, Type type) throws ConverterException {
    JavaType javaType = objectMapper.getTypeFactory().constructType(type);

    try {/*from  www .j a v  a2s  .  com*/
        Reader reader = new FileReader(file);
        T value;

        if (!reader.ready()) {
            value = null;
        } else {
            value = objectMapper.readValue(reader, javaType);
        }

        reader.close();
        return value;
    } catch (Exception e) {
        throw new ConverterException(e);
    }
}

From source file:com.github.rnewson.couchdb.lucene.Tika.java

public void parse(final InputStream in, final String contentType, final String fieldName, final Document doc)
        throws IOException {
    final AutoDetectParser parser = new AutoDetectParser();
    final Metadata md = new Metadata();
    md.set(Metadata.CONTENT_TYPE, contentType);

    final Reader reader = new ParsingReader(parser, in, md);
    final String body;
    try {// ww w . j ava  2s  .  c o  m
        try {
            body = IOUtils.toString(reader);
        } finally {
            reader.close();
        }
    } catch (final IOException e) {
        log.warn("Failed to index an attachment.", e);
        return;
    }

    // Add body text.
    doc.add(text(fieldName, body, false));

    // Add DC attributes.
    addDublinCoreAttributes(md, doc);

    // Detect language.
    final String language = LanguageIdentifier.identifyLanguage(body);
    if (language != null && language.length() > 0)
        doc.add(text(DC + DublinCore.LANGUAGE, language, false));
}

From source file:freemarker.test.CopyrightCommentRemoverTemplateLoader.java

@Override
public Reader getReader(Object templateSource, String encoding) throws IOException {
    Reader reader = innerTemplateLoader.getReader(templateSource, encoding);
    try {//from  w w w.  ja  v  a 2  s  .  co m
        String content = IOUtils.toString(reader);
        return new StringReader(TestUtil.removeCopyrightCommentFromFTL(content));
    } finally {
        reader.close();
    }
}

From source file:com.migratebird.script.runner.impl.BaseNativeScriptRunner.java

protected File createTemporaryScriptFile(Script script) throws IOException {
    File temporaryScriptsDir = createTemporaryScriptsDir();
    File temporaryScriptFile = new File(temporaryScriptsDir, getTemporaryScriptName(script));
    temporaryScriptFile.deleteOnExit();/*from  w w  w.  j  av a 2 s. c  om*/

    Reader scriptContentReader = script.getScriptContentHandle().openScriptContentReader();
    try {
        createFile(temporaryScriptFile, scriptContentReader);
    } finally {
        scriptContentReader.close();
    }
    return temporaryScriptFile;
}

From source file:cz.vse.fis.keg.entityclassifier.core.entitylinking.SFISearch.java

public LinkedEntity findWikipediaArticle(String mention, String lang) {

    LinkedEntity linkedEntity = null;/*from   ww  w.ja  v  a2 s  . co  m*/
    OutputStream os = null;
    try {
        String url = Settings.SEMITAGS_LINKING_ENDPOINT;
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        String data = "lang=" + lang + "&surfaceForm=" + mention;
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setDoOutput(true);
        os = con.getOutputStream();
        os.write(data.getBytes("UTF-8"));
        os.flush();
        os.close();

        int responseCode = con.getResponseCode();

        int code = con.getResponseCode();

        if (code == 500) {
            return linkedEntity;
        }
        StringBuffer buffer = new StringBuffer();
        InputStream is = con.getInputStream();
        Reader isr = new InputStreamReader(is, "UTF-8");
        Reader in = new BufferedReader(isr);
        int ch;
        while ((ch = in.read()) > -1) {
            buffer.append((char) ch);
        }
        in.close();
        isr.close();
        String result = buffer.toString();

        Object linkObj = null;
        Object scoreObj = null;
        String link = "";
        double score;
        JSONObject jsonObj = new JSONObject(result);

        linkObj = jsonObj.get("link");
        scoreObj = jsonObj.get("socre");
        if (!linkObj.toString().equals("null")) {
            link = jsonObj.getString("link");
            score = jsonObj.getDouble("socre");
            linkedEntity = new LinkedEntity();
            linkedEntity.setPageTitle(link.split("/")[link.split("/").length - 1].replace("_", " "));
            linkedEntity.setConfidence(score);
            return linkedEntity;
        } else {
            return null;
        }
    } catch (MalformedURLException ex) {
        Logger.getLogger(SFISearch.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(SFISearch.class.getName()).log(Level.SEVERE, null, ex);
    }

    return linkedEntity;
}

From source file:de.hstsoft.sdeep.NoteManager.java

public void loadNotes() throws IOException, ParseException {

    File file = new File(directory + FILENAME);
    if (file.exists()) {

        Reader reader = new BufferedReader(new FileReader(file));
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);
        reader.close();

        int version = Integer.parseInt(json.get("version").toString());
        if (VERSION != version) {
            System.out.println("Can not load Notes. invalid version.");
        }/*  w  w w.j  av  a 2 s. c o m*/

        JSONArray notesArray = (JSONArray) json.get("notes");

        this.notes = new ArrayList<>();
        @SuppressWarnings("rawtypes")
        Iterator iterator = notesArray.iterator();
        while (iterator.hasNext()) {
            JSONObject noteJson = (JSONObject) iterator.next();
            Note note = Note.fromJson(noteJson);
            notes.add(note);
        }
    }

}

From source file:com.github.sdbg.core.test.util.PlainTestProject.java

/**
 * @return the {@link String} content of the {@link IFile} with given path.
 *//*from  w  w  w  .  j av a 2s  . c  o  m*/
public String getFileString(String path) throws Exception {
    IFile file = getFile(path);
    Reader reader = new InputStreamReader(file.getContents(), file.getCharset());
    try {
        return CharStreams.toString(reader);
    } finally {
        reader.close();
    }
}

From source file:com.camel.drools.expert.sample.service.OrderAutoFlowService.java

/**
 * DSLDSLR?DRL//from  ww w .  ja  va  2 s .co  m
 * @param dslrReader
 * @param dslReader
 * @return
 */
public String getDrlFromDsl(Reader dslrReader, Reader dslReader) {
    DSLMappingFile dslFile = new DSLTokenizedMappingFile();
    String drl = "";
    try {
        final boolean parseResult = dslFile.parseAndLoad(dslReader);
        dslReader.close();
        DefaultExpander expander = new DefaultExpander();
        expander.addDSLMapping(dslFile.getMapping());
        drl = expander.expand(dslrReader);
        dslrReader.close();
        if (expander.hasErrors()) {
            throw new RuntimeException("expanding drl from dslr error");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return drl;
}

From source file:PatchyMIDlet.java

public void run() {
    try {//from   w  ww. j a va 2  s.c  o m
        mServerSocketConnection = (ServerSocketConnection) Connector.open("socket://:80");
        SocketConnection sc = null;
        while (mTrucking) {
            sc = (SocketConnection) mServerSocketConnection.acceptAndOpen();
            Reader in = new InputStreamReader(sc.openInputStream());

            PrintStream out = new PrintStream(sc.openOutputStream());
            out.print("HTTP/1.1 200 OK\r\n\r\n");
            out.print("Message");
            out.close();
            in.close();
            sc.close();
        }
    } catch (Exception e) {

    }
}

From source file:com.hypirion.io.ClosingPipeTest.java

/**
 * Tests that a ClosingPipe closes a Writer after the Reader is properly
 * consumed.//from  w ww  . ja  va2 s .  c  om
 */
@Test(timeout = 1000)
public void testBasicWriteClosingCapabilities() throws Exception {
    String input = RandomStringUtils.random(4023);
    Reader in = new StringReader(input);
    StringWriter out = new StringWriter();
    CloseCheckingReader wrapper = new CloseCheckingReader(out);
    Pipe p = new ClosingPipe(in, wrapper);
    p.start();
    p.join();
    in.close();
    String output = out.toString();
    assertEquals(input, output);
    assertTrue(String.format("Failed to close for %s", input), wrapper.isClosed);
}