Search in sources :

Example 16 with Reader

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));
    }
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Collection(java.util.Collection) Writable(groovy.lang.Writable) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 17 with Reader

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);
            }
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) OutputStreamWriter(java.io.OutputStreamWriter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) FileInputStream(java.io.FileInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 18 with Reader

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JobTraceReader(org.apache.hadoop.tools.rumen.JobTraceReader) List(java.util.List) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet)

Example 19 with Reader

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;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Socket(java.net.Socket)

Example 20 with Reader

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();
}
Also used : ReusableStringReader(org.apache.hadoop.hive.ql.udf.xml.UDFXPathUtil.ReusableStringReader) ReusableStringReader(org.apache.hadoop.hive.ql.udf.xml.UDFXPathUtil.ReusableStringReader) Reader(java.io.Reader) IOException(java.io.IOException) Test(org.junit.Test)

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