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(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w w. j a  va  2 s  . c o  m*/
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile, encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    try {//from   w  ww  .ja v a 2 s.  c o  m
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile,

                encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:Messenger.TorLib.java

public static void main(String[] args) {
    String req = "-r";
    String targetHostname = "tor.eff.org";
    String targetDir = "index.html";
    int targetPort = 80;

    if (args.length > 0 && args[0].equals("-h")) {
        System.out.println("Tinfoil/TorLib - interface for using Tor from Java\n"
                + "By Joe Foley<foley@mit.edu>\n" + "Usage: java Tinfoil.TorLib <cmd> <args>\n"
                + "<cmd> can be: -h for help\n" + "              -r for resolve\n"
                + "              -w for wget\n" + "For -r, the arg is:\n"
                + "  <hostname> Hostname to DNS resolve\n" + "For -w, the args are:\n"
                + "   <host> <path> <optional port>\n"
                + " for example, http://tor.eff.org:80/index.html would be\n" + "   tor.eff.org index.html 80\n"
                + " Since this is a demo, the default is the tor website.\n");
        System.exit(2);//from w  w w  .  j a  v a2s  . com
    }

    if (args.length >= 4)
        targetPort = new Integer(args[2]).intValue();
    if (args.length >= 3)
        targetDir = args[2];
    if (args.length >= 2)
        targetHostname = args[1];
    if (args.length >= 1)
        req = args[0];

    if (req.equals("-r")) {
        System.out.println(TorResolve(targetHostname));
    } else if (req.equals("-w")) {
        try {
            Socket s = TorSocket(targetHostname, targetPort);
            DataInputStream is = new DataInputStream(s.getInputStream());
            PrintStream out = new java.io.PrintStream(s.getOutputStream());

            //Construct an HTTP request
            out.print("GET  /" + targetDir + " HTTP/1.0\r\n");
            out.print("Host: " + targetHostname + ":" + targetPort + "\r\n");
            out.print("Accept: */*\r\n");
            out.print("Connection: Keep-Aliv\r\n");
            out.print("Pragma: no-cache\r\n");
            out.print("\r\n");
            out.flush();

            // this is from Java Examples In a Nutshell
            final InputStreamReader from_server = new InputStreamReader(is);
            char[] buffer = new char[1024];
            int chars_read;

            // read until stream closes
            while ((chars_read = from_server.read(buffer)) != -1) {
                // loop through array of chars
                // change \n to local platform terminator
                // this is a nieve implementation
                for (int j = 0; j < chars_read; j++) {
                    if (buffer[j] == '\n')
                        System.out.println();
                    else
                        System.out.print(buffer[j]);
                }
                System.out.flush();
            }
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static String loadFile(File f) throws IOException {
    FileInputStream stream = new FileInputStream(f);
    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
    char[] cData = new char[(int) f.length()];
    reader.read(cData);
    return new String(cData).trim();
}

From source file:Main.java

public static String inputStream2String(InputStream in, String encoding) throws Exception {
    StringBuffer out = new StringBuffer();
    InputStreamReader inread = new InputStreamReader(in, encoding);

    char[] b = new char[4096];
    for (int n; (n = inread.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }/*from  w ww . j av a2  s . c om*/

    return out.toString();
}

From source file:Main.java

private static String readFirstLine(InputStreamReader rdr) throws IOException {
    char[] buf = new char[1];
    StringBuffer bldr = new StringBuffer();
    rdr.read(buf);
    while (buf[0] != '>') {
        bldr.append(buf[0]);//from  w  ww.  j  a va  2s  .  c  om
        rdr.read(buf);
    }
    return bldr.toString();
}

From source file:Main.java

/**
 * @author Cheb/*from w  w w  .  j  a v  a2  s  .c  o m*/
 * @param URL - URL to server
 * @param context - context
 * @param mPreferences SharedPreferences
 * downloading JSON from URL
 */
public static String downloadJSON(String URL, Context context, SharedPreferences mPreferences) {
    StringBuilder sb = new StringBuilder();
    DefaultHttpClient mHttpClient = new DefaultHttpClient();
    HttpGet dhttpget = new HttpGet(URL);
    HttpResponse dresponse = null;
    try {
        dresponse = mHttpClient.execute(dhttpget);
    } catch (IOException e) {
        e.printStackTrace();
    }
    int status = dresponse.getStatusLine().getStatusCode();
    if (status == 200) {
        char[] buffer = new char[1];
        try {
            InputStream content = dresponse.getEntity().getContent();
            InputStreamReader isr = new InputStreamReader(content);
            while (isr.read(buffer) != -1) {
                sb.append(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //saving JSON 
        mPreferences = context.getSharedPreferences(TAG_FEED, 0);
        mPreferences.edit().putString(TAG_FEED, sb.toString()).commit();
    } else {
        Log.i("Error", "Connection error : " + Integer.toString(status));
    }
    return sb.toString();
}

From source file:Main.java

public static boolean symlink(File inFile, File outFile) {
    int exitCode = -1;
    try {//from w w  w.  j a  v  a 2s  .  c o m
        Process sh = Runtime.getRuntime().exec("sh");
        OutputStream out = sh.getOutputStream();
        String command = "/system/bin/ln -s " + inFile + " " + outFile + "\nexit\n";
        out.write(command.getBytes("ASCII"));

        final char buf[] = new char[40];
        InputStreamReader reader = new InputStreamReader(sh.getInputStream());
        while (reader.read(buf) != -1)
            throw new IOException("stdout: " + new String(buf));
        reader = new InputStreamReader(sh.getErrorStream());
        while (reader.read(buf) != -1)
            throw new IOException("stderr: " + new String(buf));

        exitCode = sh.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (InterruptedException e) {
        e.printStackTrace();
        return false;
    }
    return exitCode == 0;
}

From source file:Main.java

/**
 * Decodes the contents of the provided {@link InputStream} into a
 * {@link String} using the charset denoted by the charsetName parameter.
 * /* w ww  .  j a v  a  2s.c  o m*/
 * @param byteStream The {@link InputStream} to decode.
 * @param charsetName The charset used to decode the stream.
 * @return A {@link String} representation of the stream's contents.
 * @throws IOException
 */
public static String toString(InputStream byteStream, String charsetName) throws IOException {
    char[] buffer = new char[1024];

    StringBuilder builder = new StringBuilder();

    InputStreamReader reader = new InputStreamReader(byteStream, charsetName);

    for (int length = 0; (length = reader.read(buffer)) >= 0;) {
        builder.append(buffer, 0, length);
    }

    reader.close();

    return builder.toString();
}

From source file:Main.java

public static String inputStreamToString(InputStream inputStream, String charsetName) throws IOException {
    StringBuilder builder = new StringBuilder();
    InputStreamReader reader = new InputStreamReader(inputStream, charsetName);
    char[] buffer = new char[BUFFER_SIZE];
    int length;// w ww. j a  va  2  s  .  co m
    while ((length = reader.read(buffer)) != -1) {
        builder.append(buffer, 0, length);
    }
    return builder.toString();
}