Example usage for org.apache.commons.io.input ReaderInputStream close

List of usage examples for org.apache.commons.io.input ReaderInputStream close

Introduction

In this page you can find the example usage for org.apache.commons.io.input ReaderInputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Close the stream.

Usage

From source file:de.dfki.iui.mmds.core.emf.persistence.EmfPersistence.java

public static Resource readFromString(String input) throws IOException {
    StringReader stringReader = new StringReader(input);
    ReaderInputStream is = new ReaderInputStream(stringReader, Charset.forName("UTF-8"));
    Resource resource = readFromStream(is);
    is.close();
    return resource;
}

From source file:com.codealot.textstore.FileStore.java

@Override
public String storeText(final Reader reader) throws IOException {
    Objects.requireNonNull(reader, "No reader provided");

    // make the digester
    final MessageDigest digester = getDigester();

    // make temp file
    final Path textPath = Paths.get(this.storeRoot, UUID.randomUUID().toString());

    // stream to file, building digest
    final ReaderInputStream readerAsBytes = new ReaderInputStream(reader, StandardCharsets.UTF_8);
    try {/*from  w w w  . ja  va2  s.  co m*/
        final byte[] bytes = new byte[1024];
        int readLength = 0;
        long totalRead = 0L;

        while ((readLength = readerAsBytes.read(bytes)) > 0) {
            totalRead += readLength;

            digester.update(bytes, 0, readLength);

            final byte[] readBytes = Arrays.copyOf(bytes, readLength);
            Files.write(textPath, readBytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
                    StandardOpenOption.APPEND);
        }
        // check that something was read
        if (totalRead == 0L) {
            return this.storeText("");
        }
        // make the hash
        final String hash = byteToHex(digester.digest());

        // store the text, if new
        final Path finalPath = Paths.get(this.storeRoot, hash);
        if (Files.exists(finalPath)) {
            // already existed, so delete uuid named one
            Files.deleteIfExists(textPath);
        } else {
            // rename the file
            Files.move(textPath, finalPath);
        }
        return hash;
    } finally {
        if (readerAsBytes != null) {
            readerAsBytes.close();
        }
    }
}

From source file:org.apache.nifi.processors.standard.util.TestJdbcCommon.java

@Test
public void testClob() throws Exception {
    try (final Statement stmt = con.createStatement()) {
        stmt.executeUpdate("CREATE TABLE clobtest (id INT, text CLOB(64 K))");
        stmt.execute("INSERT INTO blobtest VALUES (41, NULL)");
        PreparedStatement ps = con.prepareStatement("INSERT INTO clobtest VALUES (?, ?)");
        ps.setInt(1, 42);//from w  w w  . j  ava  2  s . c  om
        final char[] buffer = new char[4002];
        IntStream.range(0, 4002).forEach((i) -> buffer[i] = String.valueOf(i % 10).charAt(0));
        ReaderInputStream isr = new ReaderInputStream(new CharArrayReader(buffer), Charset.defaultCharset());

        // - set the value of the input parameter to the input stream
        ps.setAsciiStream(2, isr, 4002);
        ps.execute();
        isr.close();

        final ResultSet resultSet = stmt.executeQuery("select * from clobtest");

        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        JdbcCommon.convertToAvroStream(resultSet, outStream, false);

        final byte[] serializedBytes = outStream.toByteArray();
        assertNotNull(serializedBytes);

        // Deserialize bytes to records
        final InputStream instream = new ByteArrayInputStream(serializedBytes);

        final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
        try (final DataFileStream<GenericRecord> dataFileReader = new DataFileStream<>(instream, datumReader)) {
            GenericRecord record = null;
            while (dataFileReader.hasNext()) {
                // Reuse record object by passing it to next(). This saves us from
                // allocating and garbage collecting many objects for files with
                // many items.
                record = dataFileReader.next(record);
                Integer id = (Integer) record.get("ID");
                Object o = record.get("TEXT");
                if (id == 41) {
                    assertNull(o);
                } else {
                    assertNotNull(o);
                    assertEquals(4002, o.toString().length());
                }
            }
        }
    }
}

From source file:org.zanata.adapter.glossary.GlossaryPoReader.java

@Override
public Map<LocaleId, List<GlossaryEntry>> extractGlossary(Reader reader, String qualifiedName)
        throws IOException {
    ReaderInputStream ris = new ReaderInputStream(reader);
    try {/* w w  w .j av  a2 s.com*/
        InputSource potInputSource = new InputSource(ris);
        potInputSource.setEncoding("utf8");
        return extractTemplate(potInputSource, qualifiedName);
    } finally {
        ris.close();
    }
}