Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:com.aliyun.odps.ship.DShip.java

protected static void showHelp(String filename) throws IOException {

    InputStream ins = DShip.class.getResourceAsStream("/" + filename);
    InputStreamReader reader = new InputStreamReader(ins, "utf-8");
    int c = reader.read();
    while (c != -1) {
        System.out.print((char) c);
        c = reader.read();//ww  w .java  2s.  c om
    }
    reader.close();
}

From source file:at.jku.rdfstats.hist.builder.HistogramCodec.java

public static String readString(ByteArrayInputStream stream) {
    try {//from  www. java2 s.  c o  m
        int next;
        String s;
        StringBuilder sb = new StringBuilder();
        stream.mark(0);
        InputStreamReader in = new InputStreamReader(stream);

        while (true) {
            next = in.read();
            if (next < 0 || (char) next == END_OF_STRING) {
                s = sb.toString();
                break;
            } else if ((char) next == EMPTY_STRING) {
                s = "";
                break;
            } else
                sb.append((char) next);
        }

        stream.reset();
        stream.skip(s.length() + 1);
        return s;
    } catch (IOException e) {
        throw new RuntimeException("Unexpected error: cannot read String from ByteArrayOutputStream.", e);
    }
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.io.InFile.java

public static String readFileText(String file, String encoding) throws IOException {
     File f = new File(file);
     InputStreamReader isr = new InputStreamReader(new FileInputStream(f), encoding);
     System.out.println("character encoding = " + isr.getEncoding());
     int c;/*  www  .ja v  a 2  s. c  o m*/
     StringBuffer res = new StringBuffer();
     while ((c = isr.read()) != -1) {
         res.append((char) c);
     }
     isr.close();
     return res.toString();
 }

From source file:com.panet.imeta.core.vfs.KettleVFS.java

/**
 * Read a text file (like an XML document).  WARNING DO NOT USE FOR DATA FILES.
 * //  w  ww .j av a 2  s . c o m
 * @param vfsFilename the filename or URL to read from
 * @param charSetName the character set of the string (UTF-8, ISO8859-1, etc)
 * @return The content of the file as a String
 * @throws IOException
 */
public static String getTextFileContent(String vfsFilename, String charSetName) throws IOException {
    InputStream inputStream = getInputStream(vfsFilename);
    InputStreamReader reader = new InputStreamReader(inputStream, charSetName);
    int c;
    StringBuffer stringBuffer = new StringBuffer();
    while ((c = reader.read()) != -1)
        stringBuffer.append((char) c);
    reader.close();
    inputStream.close();

    return stringBuffer.toString();
}

From source file:be.ibridge.kettle.trans.step.getfilenames.GetFileNames.java

public static final String getLine(LogWriter log, InputStreamReader reader, String format)
        throws KettleFileException {
    StringBuffer line = new StringBuffer(256);
    int c = 0;//from ww  w .j  a  va 2s.  co m

    try {
        while (c >= 0) {
            c = reader.read();
            if (c == '\n' || c == '\r') {
                if (format.equalsIgnoreCase("DOS")) {
                    c = reader.read(); // skip \n and \r
                    if (c != '\r' && c != '\n') {
                        // make sure its really a linefeed or cariage return
                        // raise an error this is not a DOS file 
                        // so we have pulled a character from the next line
                        throw new KettleFileException(
                                "DOS format was specified but only a single line feed character was found, not 2");
                    }
                }
                return line.toString();
            }
            if (c >= 0)
                line.append((char) c);
        }
    } catch (KettleFileException e) {
        throw e;
    } catch (Exception e) {
        if (line.length() == 0) {
            log.logError("get line", "Exception reading line: " + e.toString());
            return null;
        }
        return line.toString();
    }
    if (line.length() > 0)
        return line.toString();

    return null;
}

From source file:Main.java

/**
 * Read the text from a file//from   ww  w.j a v  a  2 s . co m
 *
 * @param file the file to read text from
 * @return the loaded text
 */
public static String loadTextFromFile(File file) {

    if (file.exists()) {

        InputStreamReader isr = null;
        FileInputStream fis = null;
        try {

            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis, StandardCharsets.UTF_8);

            StringBuilder stringBuilder = new StringBuilder();

            int i;
            while ((i = isr.read()) != -1) {
                stringBuilder.append((char) i);
            }
            return stringBuilder.toString();

        } catch (IOException ignored) {
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
        }
    }

    return "";
}

From source file:org.tolven.connectors.passwordstore.PasswordStoreImpl.java

private static char[] toCharArray(byte[] passwordBytes) {
    try {//from  w  w  w.ja  va  2s.  c  o  m
        ByteArrayInputStream bais = new ByteArrayInputStream(passwordBytes);
        InputStreamReader inputStreamReader = null;
        List<Character> characters = new ArrayList<Character>();
        try {
            inputStreamReader = new InputStreamReader(bais, Charset.forName("UTF-8"));
            int c;
            while ((c = inputStreamReader.read()) > -1) {
                characters.add(new Character((char) c));
            }
        } finally {
            inputStreamReader.close();
        }
        char[] passwordAsCharArray = new char[characters.size()];
        for (int i = 0; i < characters.size(); i++) {
            passwordAsCharArray[i] = characters.get(i);
        }
        return passwordAsCharArray;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.apporiented.hermesftp.utils.IOUtils.java

/**
 * Reads an arbitrary text resource from the class path.
 * //from w w w . j  a  va  2 s  .  co  m
 * @param name The name of the resource.
 * @param encoding The text encoding.
 * @return The text.
 * @throws IOException Error on accessing the resource.
 */
public static String loadTextResource(String name, String encoding) throws IOException {
    String result = null;
    StringWriter sw = null;
    InputStreamReader isr = null;
    if (encoding == null) {
        encoding = "UTF-8";
    }
    try {
        InputStream is = IOUtils.class.getResourceAsStream(name);
        isr = new InputStreamReader(is, encoding);
        sw = new StringWriter();
        int c;
        while ((c = isr.read()) != -1) {
            sw.write(c);
        }
        result = sw.getBuffer().toString();
    } finally {
        closeGracefully(isr);
        closeGracefully(sw);
    }
    return result;
}

From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java

public static void copyFile(File sourceFile, String destinationPath) throws IOException {
    File destinationFile = new File(destinationPath);

    InputStreamReader in = new InputStreamReader(new FileInputStream(sourceFile), Charset.defaultCharset());

    BufferedWriter out = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(destinationFile), Charset.defaultCharset()));
    int c;//from   w  ww.j  a v a 2 s  . co m
    try {
        while ((c = in.read()) != -1) {
            out.write(c);
        }
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            //ignore
        }
        try {
            out.close();
        } catch (IOException e) {
            //ignore
        }
    }
}

From source file:org.kie.guvnor.m2repo.backend.server.GuvnorM2Repository.java

public static String loadPOMFromJar(String jarPath) {
    try {/*from  ww  w .  j  a va  2s. c o  m*/
        ZipFile zip = new ZipFile(new File(jarPath));

        for (Enumeration e = zip.entries(); e.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();

            if (entry.getName().startsWith("META-INF/maven") && entry.getName().endsWith("pom.xml")) {
                InputStream is = zip.getInputStream(entry);

                InputStreamReader isr = new InputStreamReader(is, "UTF-8");
                StringBuilder sb = new StringBuilder();
                for (int c = isr.read(); c != -1; c = isr.read()) {
                    sb.append((char) c);
                }
                return sb.toString();
            }
        }
    } catch (ZipException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}