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:de.quist.samy.remocon.RemoteSession.java

private static char[] readCharArray(Reader reader) throws IOException {
    if (reader.markSupported())
        reader.mark(1024);// www.j  a  va  2s . c  om
    int length = reader.read();
    int delimiter = reader.read();
    if (delimiter != 0) {
        if (reader.markSupported())
            reader.reset();
        throw new IOException("Unsupported reply exception");
    }
    char[] buffer = new char[length];
    reader.read(buffer);
    return buffer;
}

From source file:com.example.mediastock.util.Utilities.java

/**
* It reads the content of the input stream.
 *
* @return the string from the stream//from   w  w  w  .  ja  v a  2  s .c o  m
*/
public 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:co.raveesh.yspages.YSScraper.java

/**
 * Returns String data read from URL. Copied from Stack Overflow answer by Rolan Illig. URL:http://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java 
 * @param rd//from   w  w  w .ja v a  2  s  .  c o  m
 * @return
 * @throws IOException
 */
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:gov.nih.nci.ncicb.tcga.dcc.dam.dao.FileComparer.java

public static void compareFiles(String fname1, String fname2) throws IOException {

    Reader r1 = null;
    Reader r2 = null;//from  w w  w.  j av a  2  s. co  m

    try {
        File f1 = new File(fname1);
        File f2 = new File(fname2);
        //noinspection IOResourceOpenedButNotSafelyClosed
        r1 = new BufferedReader(new FileReader(f1));
        //noinspection IOResourceOpenedButNotSafelyClosed
        r2 = new BufferedReader(new FileReader(f2));
        int c1, c2;
        c1 = r1.read();
        c2 = r2.read();
        assertFalse("file1 has no content", c1 < 0);
        int i = 0;
        while (c1 != -1) {
            assertEquals("files are not equal at index " + i, c1, c2);
            c1 = r1.read();
            c2 = r2.read();
            ++i;
        }
    } finally {
        IOUtils.closeQuietly(r1);
        IOUtils.closeQuietly(r2);
    }
}

From source file:edu.cmu.cs.lti.util.general.BasicConvenience.java

public static String read(Reader reader) throws IOException {
    StringBuilder buf = new StringBuilder();

    int readInt;//from w  ww  . j  a  va2 s. c  o  m

    while ((readInt = reader.read()) != -1)
        buf.append((char) readInt);

    return buf.toString();
}

From source file:org.ednovo.gooru.service.ResourceCassandraServiceImpl.java

private static String readAll(Reader rd) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    int cp;// w  w  w.  j  a va  2 s. c o  m
    while ((cp = rd.read()) != -1) {
        stringBuilder.append((char) cp);
    }
    return stringBuilder.toString();
}

From source file:de.uzk.hki.da.sb.Utilities.java

/**
 * Deserializes the given file and returns its content as a string
 * /*from   w  w w  .  ja  v a  2s . co  m*/
 * @param file The file to read
 * @return The file content as a string
 * @throws Exception
 */
public static String readFile(File file) throws Exception {

    Reader reader;
    try {
        reader = new FileReader(file.getAbsolutePath());
    } catch (FileNotFoundException e) {
        throw new Exception("Couldn't create reader", e);
    }
    String text = "";

    try {
        for (int c; (c = reader.read()) != -1;)
            text += (char) c;

        reader.close();

    } catch (IOException e) {
        throw new Exception("Couldn't read file " + file.getAbsolutePath(), e);
    }

    return text;
}

From source file:com.github.magicsky.sya.checkers.TestSourceReader.java

public static int getLineNumber(int offset, Path fullPath) throws Exception {
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath);
    Reader reader = new BufferedReader(new InputStreamReader(file.getContents(), file.getCharset()));
    try {//from   w w  w .jav  a2s .c o  m
        int line = 1;
        for (int i = 0; i < offset; i++) {
            int c = reader.read();
            Assert.assertTrue(c >= 0);
            if (c == '\n')
                line++;
        }
        return line;
    } finally {
        reader.close();
    }
}

From source file:com.athena.chameleon.engine.utils.FileUtil.java

/**
 * //from  w w w.  java2 s  .  c om
 * ? ??  .
 *
 * @param zipFilePath ? 
 * @param unzipDirPath ?   
 * @param declaredEncoding ?  
 * @return String  ?  
 * @throws IOException
 */
public static String extract(String zipFilePath, String unzipDirPath, String declaredEncoding)
        throws IOException {
    ZipFile zipFile = null;

    try {
        if (logger.isDebugEnabled()) {
            logger.debug("[ZipUtil] zipFilePath :" + zipFilePath);
        }

        String unZipDir = (unzipDirPath == null)
                ? zipFilePath.substring(0, zipFilePath.lastIndexOf('.')) + File.separator
                : unzipDirPath + File.separator;
        if (logger.isDebugEnabled()) {
            logger.debug("[ZipUtil] unzipDirPath :" + unZipDir);
        }

        zipFile = new ZipFile(zipFilePath, "EUC-KR");

        Enumeration<?> e = zipFile.getEntries();
        for (; e.hasMoreElements();) {
            FileOutputStream output = null;
            InputStream input = null;
            String entryName;

            try {
                byte[] data;
                ZipEntry entry = (ZipEntry) e.nextElement();

                if (logger.isDebugEnabled()) {
                    logger.debug("[ZipUtil] current entry:" + entry.getName());
                }

                entryName = entry.getName().replace('\\', '/');
                if (entry.isDirectory()) {
                    File dir = new File(unZipDir + entry.getName());
                    if (!dir.exists() && !dir.mkdirs()) {
                        throw new IOException("[ZipUtil] make directory fail");
                    }
                } else {
                    File outputFile = null;

                    try {
                        outputFile = new File(unZipDir + entry.getName());

                        if (entry.getSize() == 0 && entry.getName().indexOf('.') == -1) {
                            throw new Exception("[ZipUtil] This file may be a directory");
                        }

                        File parentDir = outputFile.getParentFile();
                        if (!parentDir.exists() && !parentDir.mkdirs()) {
                            throw new IOException("[ZipUtil] make directory fail");
                        }

                        input = zipFile.getInputStream(entry);
                        output = new FileOutputStream(outputFile);

                        String changeTarget = PropertyUtil.getProperty("unzip.change.target");

                        if (changeTarget.indexOf(
                                entryName.substring(entryName.lastIndexOf(".") + 1, entryName.length())) > -1) {

                            OutputStreamWriter osr = new OutputStreamWriter(output, declaredEncoding);

                            CharsetDetector detector = new CharsetDetector();
                            data = IOUtils.toByteArray(input, entry.getSize());
                            IOUtils.closeQuietly(input);
                            detector.setDeclaredEncoding(declaredEncoding);
                            detector.setText(data);
                            Reader reader = detector.detect().getReader();

                            int buffer = 0;
                            while (true) {
                                buffer = reader.read();
                                if (buffer == -1)
                                    break;

                                osr.write(buffer);
                            }
                            osr.close();

                        } else {
                            int len = 0;
                            byte buffer[] = new byte[1024];
                            while ((len = input.read(buffer)) > 0)
                                output.write(buffer, 0, len);
                        }

                    } catch (Exception ex2) {
                        ex2.printStackTrace();
                        if (!outputFile.exists() && !outputFile.mkdirs()) {
                            throw new IOException("[ZipUtil] make directory fail");
                        }
                    }
                }
            }

            catch (IOException ex) {
                throw ex;
            }

            catch (Exception ex) {
                throw new IOException("[ZipUtil] extract fail", ex);
            }

            finally {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            }
        }
        return unZipDir;
    }

    catch (IOException e) {
        e.printStackTrace();
        throw e;
    }

    finally {
        if (zipFile != null) {
            zipFile.close();
        }
    }
}

From source file:com.redhat.lightblue.util.JsonUtils.java

public static JsonNode json(Reader reader, boolean systemPropertySubstitution) throws IOException {
    StringBuilder bld = new StringBuilder(512);
    int c;/*from w w w .j  a v a2  s  .c o m*/
    while ((c = reader.read()) >= 0) {
        bld.append((char) c);
    }
    return json(bld.toString(), systemPropertySubstitution);
}