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);
}
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"));
}
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);
}
}
}
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);
}
}
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;
}
Aggregations