use of java.io.Reader in project groovy by apache.
the class InvokerHelper method append.
/**
* Appends an object to an Appendable using Groovy's default representation for the object.
*/
public static void append(Appendable out, Object object) throws IOException {
if (object instanceof String) {
out.append((String) object);
} else if (object instanceof Object[]) {
out.append(toArrayString((Object[]) object));
} else if (object instanceof Map) {
out.append(toMapString((Map) object));
} else if (object instanceof Collection) {
out.append(toListString((Collection) object));
} else if (object instanceof Writable) {
Writable writable = (Writable) object;
StringWriter stringWriter = new StringWriter();
writable.writeTo(stringWriter);
out.append(stringWriter.toString());
} else if (object instanceof InputStream || object instanceof Reader) {
// Copy stream to stream
Reader reader;
if (object instanceof InputStream) {
reader = new InputStreamReader((InputStream) object);
} else {
reader = (Reader) object;
}
char[] chars = new char[8192];
int i;
while ((i = reader.read(chars)) != -1) {
for (int j = 0; j < i; j++) {
out.append(chars[j]);
}
}
reader.close();
} else {
out.append(toString(object));
}
}
use of java.io.Reader in project hadoop by apache.
the class RumenToSLSConverter method generateSLSLoadFile.
private static void generateSLSLoadFile(String inputFile, String outputFile) throws IOException {
try (Reader input = new InputStreamReader(new FileInputStream(inputFile), "UTF-8")) {
try (Writer output = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) {
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
Iterator<Map> i = mapper.readValues(new JsonFactory().createParser(input), Map.class);
while (i.hasNext()) {
Map m = i.next();
output.write(writer.writeValueAsString(createSLSJob(m)) + EOL);
}
}
}
}
use of java.io.Reader in project hadoop by apache.
the class SLSUtils method parseNodesFromSLSTrace.
/**
* parse the sls trace file, return each host name
*/
public static Set<String> parseNodesFromSLSTrace(String jobTrace) throws IOException {
Set<String> nodeSet = new HashSet<String>();
JsonFactory jsonF = new JsonFactory();
ObjectMapper mapper = new ObjectMapper();
Reader input = new InputStreamReader(new FileInputStream(jobTrace), "UTF-8");
try {
Iterator<Map> i = mapper.readValues(jsonF.createParser(input), Map.class);
while (i.hasNext()) {
Map jsonE = i.next();
List tasks = (List) jsonE.get("job.tasks");
for (Object o : tasks) {
Map jsonTask = (Map) o;
String hostname = jsonTask.get("container.host").toString();
nodeSet.add(hostname);
}
}
} finally {
input.close();
}
return nodeSet;
}
use of java.io.Reader in project hbase by apache.
the class MiniZooKeeperCluster method waitForServerUp.
// XXX: From o.a.zk.t.ClientBase
private static boolean waitForServerUp(int port, long timeout) throws IOException {
long start = System.currentTimeMillis();
while (true) {
try {
Socket sock = new Socket("localhost", port);
BufferedReader reader = null;
try {
OutputStream outstream = sock.getOutputStream();
outstream.write("stat".getBytes());
outstream.flush();
Reader isr = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isr);
String line = reader.readLine();
if (line != null && line.startsWith("Zookeeper version:")) {
return true;
}
} finally {
sock.close();
if (reader != null) {
reader.close();
}
}
} catch (IOException e) {
// ignore as this is expected
LOG.info("server localhost:" + port + " not up " + e);
}
if (System.currentTimeMillis() > start + timeout) {
break;
}
try {
Thread.sleep(250);
} catch (InterruptedException e) {
throw (InterruptedIOException) new InterruptedIOException().initCause(e);
}
}
return false;
}
use of java.io.Reader in project hive by apache.
the class TestReusableStringReader method testEmpty.
/**
* Test empty {@link ReusableStringReader}
*/
@Test
public void testEmpty() throws IOException {
Reader reader = new ReusableStringReader();
try {
int ch = reader.read();
fail("IOException expected.");
} catch (IOException ioe) {
// expected
}
try {
boolean ready = reader.ready();
fail("IOException expected.");
} catch (IOException ioe) {
// expected
}
reader.close();
}
Aggregations