Example usage for java.io Reader read

List of usage examples for java.io Reader read

Introduction

In this page you can find the example usage for java.io Reader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:com.scandit.simplesample.SimpleSampleActivity.java

public static String readAll(Reader r) throws IOException {
    StringBuilder s = new StringBuilder();
    try {/* www.  ja va  2 s .  c  om*/
        int i = r.read();
        while (i != -1) {
            s.append((char) (i));
            i = r.read();
        }
        return s.toString();
    } finally {

    }
}

From source file:com.moss.posixfifosockets.PosixFifoSocket.java

public static PosixFifoSocket newClientConnection(PosixFifoSocketAddress address, int timeoutMillis)
        throws IOException {
    final Log log = LogFactory.getLog(PosixFifoSocket.class);

    if (!address.controlPipe().exists()) {
        throw new IOException("There is no server at " + address);
    }//from w  w  w . java 2  s  . co m

    File in;
    File out;
    File control;
    long id;
    do {
        id = r.nextLong();
        in = new File(address.socketsDir(), id + ".fifo.out");
        out = new File(address.socketsDir(), id + ".fifo.in");
        control = new File(address.socketsDir(), id + ".fifo.control");
    } while (out.exists() || in.exists());

    createFifo(in);
    createFifo(control);

    final String registrationString = "{" + id + "}";
    if (log.isDebugEnabled())
        log.debug("Sending registration " + registrationString);
    Writer w = new FileWriter(address.controlPipe());
    w.write(registrationString);
    w.flush();

    if (log.isDebugEnabled())
        log.debug("Sent Registration " + registrationString);

    PosixFifoSocket socket = new PosixFifoSocket(id, in, out);
    Reader r = new FileReader(control);

    StringBuilder text = new StringBuilder();
    for (int c = r.read(); c != -1 && c != '\n'; c = r.read()) {
        // READ UNTIL THE FIRST LINE BREAK
        text.append((char) c);
    }
    r.close();
    if (!control.delete()) {
        throw new RuntimeException("Could not delete file:" + control.getAbsolutePath());
    }
    if (!text.toString().equals("OK")) {
        throw new RuntimeException("Connection error: received \"" + text + "\"");
    }
    return socket;
}

From source file:Main.java

/**
 * Compare the contents of two Readers to determine if they are equal or
 * not.//from   w w  w . j a  v a  2 s  . c  o  m
 * <p/>
 * This method buffers the input internally using
 * <code>BufferedReader</code> if they are not already buffered.
 *
 * @param input1 the first reader
 * @param input2 the second reader
 * @return true if the content of the readers are equal or they both don't
 * exist, false otherwise
 * @throws NullPointerException if either input is null
 * @throws IOException  if an I/O error occurs
 * @since 1.1
 */
public static boolean contentEquals(Reader input1, Reader input2) throws IOException {

    input1 = toBufferedReader(input1);
    input2 = toBufferedReader(input2);

    int ch = input1.read();
    while (EOF != ch) {
        int ch2 = input2.read();
        if (ch != ch2) {
            return false;
        }
        ch = input1.read();
    }

    int ch2 = input2.read();
    return ch2 == EOF;
}

From source file:Main.java

@SuppressLint("NewApi")
public static void getURL(String path) {
    String fileName = "";
    String dir = "/IndoorNavi/";
    File sdRoot = Environment.getExternalStorageDirectory();
    try {/* w  w  w. ja v a2 s. com*/
        // Open the URLConnection for reading
        URL u = new URL(path);
        // URL u = new URL("http://www.baidu.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();

        int code = uc.getResponseCode();
        String response = uc.getResponseMessage();
        //System.out.println("HTTP/1.x " + code + " " + response);
        for (int j = 1;; j++) {
            String key = uc.getHeaderFieldKey(j);
            String header = uc.getHeaderField(j);
            if (!(key == null)) {
                if (key.equals("Content-Name"))
                    fileName = header;
            }
            if (header == null || key == null)
                break;
            //System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
        }
        Log.i("zhr", fileName);
        //System.out.println();

        try (InputStream in = new BufferedInputStream(uc.getInputStream())) {

            // chain the InputStream to a Reader
            Reader r = new InputStreamReader(in);
            int c;
            File mapFile = new File(sdRoot, dir + fileName);
            mapFile.createNewFile();
            FileOutputStream filecon = new FileOutputStream(mapFile);
            while ((c = r.read()) != -1) {
                //System.out.print((char) c);
                filecon.write(c);
                filecon.flush();

            }
            filecon.close();
        }

    } catch (MalformedURLException ex) {
        System.err.println(path + " is not a parseable URL");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:com.prowidesoftware.swift.utils.Lib.java

/**
 * Read the content of the given reader into a string.
 *
 * @param reader the contents to read//from www  . ja v  a2s  . c  om
 * @return the read content
 * @throws IOException
 * @since 7.7
 */
public static String readReader(final Reader reader) throws IOException {
    if (reader == null) {
        return null;
    }
    final StringBuilder out = new StringBuilder();
    try {
        int c = 0;
        while ((c = reader.read()) != -1) {
            out.append((char) c);
        }
    } finally {
        reader.close();
    }
    return out.toString();
}

From source file:net.sf.cb2xml.convert.MainframeToXml.java

private static String stripNullChars(String in) {
    try {//ww w  . j a  v a 2 s. c om
        Reader reader = new BufferedReader(new StringReader(in));
        StringBuffer buffer = new StringBuffer();
        int ch;
        while ((ch = reader.read()) > -1) {
            if (ch != 0) {
                buffer.append((char) ch);
            } else {
                buffer.append(' ');
            }
        }
        reader.close();
        return buffer.toString();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.scaniatv.ChallongeAPI.java

/**
 * @function readAll/*www  .  j a v a  2  s  .  c  o  m*/
 *
 * @param {Reader} rd
 */
private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;

    while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
    }
    return sb.toString();
}

From source file:Main.java

public static String highlight(final List<String> lines, final String meta, final String prog,
        final String encoding) throws IOException {
    final File tmpIn = new File(System.getProperty("java.io.tmpdir"),
            String.format("txtmark_code_%d_%d.in", ID, IN_COUNT.incrementAndGet()));
    final File tmpOut = new File(System.getProperty("java.io.tmpdir"),
            String.format("txtmark_code_%d_%d.out", ID, OUT_COUNT.incrementAndGet()));

    try {/*w  ww .  ja  v a  2s.c o  m*/

        final Writer w = new OutputStreamWriter(new FileOutputStream(tmpIn), encoding);

        try {
            for (final String s : lines) {
                w.write(s);
                w.write('\n');
            }
        } finally {
            w.close();
        }

        final List<String> command = new ArrayList<String>();

        command.add(prog);
        command.add(meta);
        command.add(tmpIn.getAbsolutePath());
        command.add(tmpOut.getAbsolutePath());

        final ProcessBuilder pb = new ProcessBuilder(command);
        final Process p = pb.start();
        final InputStream pIn = p.getInputStream();
        final byte[] buffer = new byte[2048];

        int exitCode = 0;
        for (;;) {
            if (pIn.available() > 0) {
                pIn.read(buffer);
            }
            try {
                exitCode = p.exitValue();
            } catch (final IllegalThreadStateException itse) {
                continue;
            }
            break;
        }

        if (exitCode == 0) {
            final Reader r = new InputStreamReader(new FileInputStream(tmpOut), encoding);
            try {
                final StringBuilder sb = new StringBuilder();
                for (;;) {
                    final int c = r.read();
                    if (c >= 0) {
                        sb.append((char) c);
                    } else {
                        break;
                    }
                }
                return sb.toString();
            } finally {
                r.close();
            }
        }

        throw new IOException("Exited with exit code: " + exitCode);
    } finally {
        tmpIn.delete();
        tmpOut.delete();
    }
}

From source file:Main.java

/**
 * Reads an XML declaration to get the encoding declaration value.
 * //from  w  w  w.  j av a2s.  c o m
 * @param r
 *            a reader positioned at the start of an xml document
 * @param e
 *            the encoding to return by default or on error.
 */
public static String getXMLDeclarationEncoding(Reader r, String e) throws IOException {
    int c;

    while (isXMLSpace((char) (c = r.read()))) {
        // skip space
    }

    if (c != '<') {
        return e;
    }
    if ((c = r.read()) != '?') {
        return e;
    }
    if ((c = r.read()) != 'x') {
        return e;
    }
    if ((c = r.read()) != 'm') {
        return e;
    }
    if ((c = r.read()) != 'l') {
        return e;
    }

    if (!isXMLSpace((char) (c = r.read()))) {
        return e;
    }

    while (isXMLSpace((char) (c = r.read()))) {
        // skip space
    }

    if (c != 'v') {
        return e;
    }
    if ((c = r.read()) != 'e') {
        return e;
    }
    if ((c = r.read()) != 'r') {
        return e;
    }
    if ((c = r.read()) != 's') {
        return e;
    }
    if ((c = r.read()) != 'i') {
        return e;
    }
    if ((c = r.read()) != 'o') {
        return e;
    }
    if ((c = r.read()) != 'n') {
        return e;
    }

    c = r.read();
    while (isXMLSpace((char) c)) {
        c = r.read();
    }

    if (c != '=') {
        return e;
    }

    while (isXMLSpace((char) (c = r.read()))) {
        // skip space
    }

    if (c != '"' && c != '\'') {
        return e;
    }
    char sc = (char) c;

    for (;;) {
        c = r.read();
        if (c == sc) {
            break;
        }
        if (!isXMLVersionCharacter((char) c)) {
            return e;
        }
    }

    if (!isXMLSpace((char) (c = r.read()))) {
        return e;
    }
    while (isXMLSpace((char) (c = r.read()))) {
        // skip space
    }

    if (c != 'e') {
        return e;
    }
    if ((c = r.read()) != 'n') {
        return e;
    }
    if ((c = r.read()) != 'c') {
        return e;
    }
    if ((c = r.read()) != 'o') {
        return e;
    }
    if ((c = r.read()) != 'd') {
        return e;
    }
    if ((c = r.read()) != 'i') {
        return e;
    }
    if ((c = r.read()) != 'n') {
        return e;
    }
    if ((c = r.read()) != 'g') {
        return e;
    }

    c = r.read();
    while (isXMLSpace((char) c)) {
        c = r.read();
    }

    if (c != '=') {
        return e;
    }

    while (isXMLSpace((char) (c = r.read()))) {
        // skip space
    }

    if (c != '"' && c != '\'') {
        return e;
    }
    sc = (char) c;

    StringBuffer enc = new StringBuffer();
    for (;;) {
        c = r.read();
        if (c == -1) {
            return e;
        }
        if (c == sc) {
            return enc.toString();
        }
        enc.append((char) c);
    }
}

From source file:eu.sisob.uma.footils.File.FileFootils.java

public static String readStream(InputStream is, String encoding) {
    StringBuilder sb = new StringBuilder(512);
    try {/* w ww  .  j  av  a  2  s .c om*/
        Reader r = new InputStreamReader(is, encoding);
        int c = 0;
        while (c != -1) {
            c = r.read();
            sb.append((char) c);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return sb.toString();
}