Search in sources :

Example 61 with InputStreamReader

use of java.io.InputStreamReader in project platform_frameworks_base by android.

the class NetworkManagementService method readRouteList.

private ArrayList<String> readRouteList(String filename) {
    FileInputStream fstream = null;
    ArrayList<String> list = new ArrayList<>();
    try {
        fstream = new FileInputStream(filename);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String s;
        while (((s = br.readLine()) != null) && (s.length() != 0)) {
            list.add(s);
        }
    } catch (IOException ex) {
    // return current list, possibly empty
    } finally {
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException ex) {
            }
        }
    }
    return list;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Example 62 with InputStreamReader

use of java.io.InputStreamReader in project fastjson by alibaba.

the class TestKlutz method setUp.

@Override
protected void setUp() throws Exception {
    InputStreamReader isr = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("epub.json"));
    BufferedReader reader = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    String temp;
    while ((temp = reader.readLine()) != null) {
        sb.append(temp);
    }
    String s = sb.toString();
    this.book = JSON.parseObject(s, EpubViewBook.class);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Example 63 with InputStreamReader

use of java.io.InputStreamReader in project jstorm by alibaba.

the class ShellUtils method runCommand.

/** Run a command */
private void runCommand() throws IOException {
    ProcessBuilder builder = new ProcessBuilder(getExecString());
    Timer timeOutTimer = null;
    ShellTimeoutTimerTask timeoutTimerTask = null;
    timedOut = new AtomicBoolean(false);
    completed = new AtomicBoolean(false);
    if (environment != null) {
        builder.environment().putAll(this.environment);
    }
    if (dir != null) {
        builder.directory(this.dir);
    }
    builder.redirectErrorStream(redirectErrorStream);
    process = builder.start();
    if (timeOutInterval > 0) {
        timeOutTimer = new Timer("Shell command timeout");
        timeoutTimerTask = new ShellTimeoutTimerTask(this);
        // One time scheduling.
        timeOutTimer.schedule(timeoutTimerTask, timeOutInterval);
    }
    final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
    final StringBuffer errMsg = new StringBuffer();
    // read error and input streams as this would free up the buffers
    // free the error stream buffer
    Thread errThread = new Thread() {

        @Override
        public void run() {
            try {
                String line = errReader.readLine();
                while ((line != null) && !isInterrupted()) {
                    errMsg.append(line);
                    errMsg.append(System.getProperty("line.separator"));
                    line = errReader.readLine();
                }
            } catch (IOException ioe) {
                LOG.warn("Error reading the error stream", ioe);
            }
        }
    };
    try {
        errThread.start();
    } catch (IllegalStateException ise) {
    }
    try {
        // parse the output
        parseExecResult(inReader);
        // clear the input stream buffer
        String line = inReader.readLine();
        while (line != null) {
            line = inReader.readLine();
        }
        // wait for the process to finish and check the exit code
        exitCode = process.waitFor();
        // make sure that the error thread exits
        joinThread(errThread);
        completed.set(true);
        // taken care in finally block
        if (exitCode != 0) {
            throw new ExitCodeException(exitCode, errMsg.toString());
        }
    } catch (InterruptedException ie) {
        throw new IOException(ie.toString());
    } finally {
        if (timeOutTimer != null) {
            timeOutTimer.cancel();
        }
        // close the input stream
        try {
            // JDK 7 tries to automatically drain the input streams for us
            // when the process exits, but since close is not synchronized,
            // it creates a race if we close the stream first and the same
            // fd is recycled. the stream draining thread will attempt to
            // drain that fd!! it may block, OOM, or cause bizarre behavior
            // see: https://bugs.openjdk.java.net/browse/JDK-8024521
            // issue is fixed in build 7u60
            InputStream stdout = process.getInputStream();
            synchronized (stdout) {
                inReader.close();
            }
        } catch (IOException ioe) {
            LOG.warn("Error while closing the input stream", ioe);
        }
        if (!completed.get()) {
            errThread.interrupt();
            joinThread(errThread);
        }
        try {
            InputStream stderr = process.getErrorStream();
            synchronized (stderr) {
                errReader.close();
            }
        } catch (IOException ioe) {
            LOG.warn("Error while closing the error stream", ioe);
        }
        process.destroy();
        lastTime = System.currentTimeMillis();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Timer(java.util.Timer) BufferedReader(java.io.BufferedReader)

Example 64 with InputStreamReader

use of java.io.InputStreamReader in project jstorm by alibaba.

the class Utils method getBuildTime.

public static String getBuildTime() {
    String ret = "";
    InputStream input = null;
    try {
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream("build");
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
        String s = in.readLine();
        if (s != null) {
            ret = s.trim();
        } else {
            LOG.warn("Failed to get build time");
        }
    } catch (Exception e) {
        LOG.warn("Failed to get build time", e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (Exception ignored) {
            }
        }
    }
    return ret;
}
Also used : InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileInputStream(java.io.FileInputStream) ClassLoaderObjectInputStream(org.apache.commons.io.input.ClassLoaderObjectInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.json.simple.parser.ParseException) TException(org.apache.thrift.TException) IOException(java.io.IOException)

Example 65 with InputStreamReader

use of java.io.InputStreamReader in project jstorm by alibaba.

the class Utils method fromCompressedJsonConf.

public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) {
    try {
        ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis));
        Object ret = JSONValue.parseWithException(in);
        in.close();
        return (Map<String, Object>) ret;
    } catch (IOException | ParseException ioe) {
        throw new RuntimeException(ioe);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ComponentObject(backtype.storm.generated.ComponentObject) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Aggregations

InputStreamReader (java.io.InputStreamReader)4861 BufferedReader (java.io.BufferedReader)3402 IOException (java.io.IOException)2108 InputStream (java.io.InputStream)1272 FileInputStream (java.io.FileInputStream)857 URL (java.net.URL)605 ArrayList (java.util.ArrayList)559 Reader (java.io.Reader)518 File (java.io.File)514 Test (org.junit.Test)451 HttpURLConnection (java.net.HttpURLConnection)290 ByteArrayInputStream (java.io.ByteArrayInputStream)282 OutputStreamWriter (java.io.OutputStreamWriter)241 FileNotFoundException (java.io.FileNotFoundException)240 URLConnection (java.net.URLConnection)227 HashMap (java.util.HashMap)192 Socket (java.net.Socket)178 OutputStream (java.io.OutputStream)175 StringWriter (java.io.StringWriter)148 BufferedWriter (java.io.BufferedWriter)138