Search in sources :

Example 6 with Reader

use of java.io.Reader in project camel by apache.

the class XmlConverter method toDOMSourceFromStream.

@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    Document document;
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Reader reader = source.getReader();
    if (reader != null) {
        document = builder.parse(new InputSource(reader));
    } else {
        InputStream inputStream = source.getInputStream();
        if (inputStream != null) {
            InputSource inputsource = new InputSource(inputStream);
            inputsource.setSystemId(systemId);
            document = builder.parse(inputsource);
        } else {
            throw new IOException("No input stream or reader available on StreamSource: " + source);
        }
    }
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 7 with Reader

use of java.io.Reader in project camel by apache.

the class IOConverterTest method testToPropertiesFromReader.

public void testToPropertiesFromReader() throws Exception {
    Reader br = IOHelper.buffered(new StringReader("foo=123\nbar=456"));
    Properties p = IOConverter.toProperties(br);
    assertNotNull(p);
    assertEquals(2, p.size());
    assertEquals("123", p.get("foo"));
    assertEquals("456", p.get("bar"));
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Properties(java.util.Properties)

Example 8 with Reader

use of java.io.Reader in project camel by apache.

the class FallbackTypeConverter method unmarshal.

protected Object unmarshal(Unmarshaller unmarshaller, Exchange exchange, Object value) throws JAXBException, UnsupportedEncodingException, XMLStreamException {
    try {
        XMLStreamReader xmlReader;
        if (value instanceof XMLStreamReader) {
            xmlReader = (XMLStreamReader) value;
        } else if (value instanceof InputStream) {
            if (needFiltering(exchange)) {
                xmlReader = staxConverter.createXMLStreamReader(new NonXmlFilterReader(new InputStreamReader((InputStream) value, IOHelper.getCharsetName(exchange))));
            } else {
                xmlReader = staxConverter.createXMLStreamReader((InputStream) value, exchange);
            }
        } else if (value instanceof Reader) {
            Reader reader = (Reader) value;
            if (needFiltering(exchange)) {
                if (!(value instanceof NonXmlFilterReader)) {
                    reader = new NonXmlFilterReader((Reader) value);
                }
            }
            xmlReader = staxConverter.createXMLStreamReader(reader);
        } else if (value instanceof Source) {
            xmlReader = staxConverter.createXMLStreamReader((Source) value);
        } else {
            throw new IllegalArgumentException("Cannot convert from " + value.getClass());
        }
        return unmarshaller.unmarshal(xmlReader);
    } finally {
        if (value instanceof Closeable) {
            IOHelper.close((Closeable) value, "Unmarshalling", LOG);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Source(javax.xml.transform.Source)

Example 9 with Reader

use of java.io.Reader in project hadoop by apache.

the class LdapGroupsMapping method extractPassword.

String extractPassword(String pwFile) {
    if (pwFile.isEmpty()) {
        // an anonymous bind
        return "";
    }
    StringBuilder password = new StringBuilder();
    try (Reader reader = new InputStreamReader(new FileInputStream(pwFile), StandardCharsets.UTF_8)) {
        int c = reader.read();
        while (c > -1) {
            password.append((char) c);
            c = reader.read();
        }
        return password.toString().trim();
    } catch (IOException ioe) {
        throw new RuntimeException("Could not read password file: " + pwFile, ioe);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 10 with Reader

use of java.io.Reader in project hadoop by apache.

the class FileBasedIPList method readLines.

/**
   * Reads the lines in a file.
   * @param fileName
   * @return lines in a String array; null if the file does not exist or if the
   * file name is null
   * @throws IOException
   */
private static String[] readLines(String fileName) throws IOException {
    try {
        if (fileName != null) {
            File file = new File(fileName);
            if (file.exists()) {
                try (Reader fileReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
                    BufferedReader bufferedReader = new BufferedReader(fileReader)) {
                    List<String> lines = new ArrayList<String>();
                    String line = null;
                    while ((line = bufferedReader.readLine()) != null) {
                        lines.add(line);
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Loaded IP list of size = " + lines.size() + " from file = " + fileName);
                    }
                    return (lines.toArray(new String[lines.size()]));
                }
            } else {
                LOG.debug("Missing ip list file : " + fileName);
            }
        }
    } catch (IOException ioe) {
        LOG.error(ioe);
        throw ioe;
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Reader (java.io.Reader)1498 InputStreamReader (java.io.InputStreamReader)526 StringReader (java.io.StringReader)498 IOException (java.io.IOException)348 BufferedReader (java.io.BufferedReader)242 InputStream (java.io.InputStream)219 TokenStream (org.apache.lucene.analysis.TokenStream)171 Test (org.junit.Test)170 SqlSessionFactoryBuilder (org.apache.ibatis.session.SqlSessionFactoryBuilder)159 Connection (java.sql.Connection)137 ScriptRunner (org.apache.ibatis.jdbc.ScriptRunner)126 FileReader (java.io.FileReader)108 FileInputStream (java.io.FileInputStream)107 File (java.io.File)105 BeforeClass (org.junit.BeforeClass)99 Tokenizer (org.apache.lucene.analysis.Tokenizer)91 SqlSession (org.apache.ibatis.session.SqlSession)83 StringWriter (java.io.StringWriter)81 ArrayList (java.util.ArrayList)77 Writer (java.io.Writer)63