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 abstract int read(char cbuf[], int off, int len) throws IOException;

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:Main.java

public static void main(String[] args) {

    String s = "tutorial from java2s.com";

    Reader reader = new StringReader(s);

    // create a char array to read chars into
    char cbuf[] = new char[5];

    try {//  w  w w  .  j  a  va2s . c  om
        // read characters into a portion of an array.
        System.out.println(reader.read(cbuf, 0, 5));

        System.out.println(cbuf);

        reader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:com.foxykeep.parcelablecodegenerator.Main.java

public static void main(String[] args) {

    File fileInputDir = new File("input");
    if (!fileInputDir.exists() || !fileInputDir.isDirectory()) {
        return;//from w  w w.  j a va2s .  c o  m
    }

    ArrayList<FileInfo> fileInfoList = new ArrayList<FileInfo>();
    findJsonFiles(fileInputDir, null, fileInfoList);

    StringBuilder sb = new StringBuilder();

    // For each file in the input folder
    for (FileInfo fileInfo : fileInfoList) {
        String fileName = fileInfo.file.getName();
        System.out.println("Generating code for " + fileName);

        char[] buffer = new char[2048];
        sb.setLength(0);
        Reader in;
        try {
            in = new InputStreamReader(new FileInputStream(fileInfo.file), "UTF-8");
            int read;
            do {
                read = in.read(buffer, 0, buffer.length);
                if (read != -1) {
                    sb.append(buffer, 0, read);
                }
            } while (read >= 0);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        String content = sb.toString();
        if (content.length() == 0) {
            System.out.println("file is empty.");
            return;
        }

        try {
            JSONObject root = new JSONObject(content);

            // Classes generation
            String classPackage, className, superClassPackage, superClassName;
            boolean isSuperClassParcelable, hasSubClasses, isAbstract;

            classPackage = root.getString("package");
            className = root.getString("name");
            superClassPackage = JsonUtils.getStringFixFalseNull(root, "superClassPackage");
            superClassName = JsonUtils.getStringFixFalseNull(root, "superClassName");
            isSuperClassParcelable = root.optBoolean("isSuperClassParcelable");
            hasSubClasses = root.optBoolean("hasSubClasses");
            if (hasSubClasses) {
                isAbstract = root.optBoolean("isAbstract");
            } else {
                isAbstract = false;
            }

            JSONArray fieldJsonArray = root.optJSONArray("fields");
            ArrayList<FieldData> fieldDataList;
            if (fieldJsonArray != null) {
                fieldDataList = FieldData.getFieldsData(fieldJsonArray);
            } else {
                fieldDataList = new ArrayList<FieldData>();
            }

            // Parcelable generation
            ParcelableGenerator.generate(fileInfo.dirPath, classPackage, className, superClassPackage,
                    superClassName, isSuperClassParcelable, hasSubClasses, isAbstract, fieldDataList);
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
    }

}

From source file:Main.java

/**
 * Converts an input stream to a string, using UTF-8 encoding.
 *//*  w w  w.  j  a va 2  s.  co m*/
public static String getStringFromStream(InputStream inputStream) throws IOException {
    final int bufferSize = 1024;
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(inputStream, "UTF-8");
    for (;;) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0)
            break;
        out.append(buffer, 0, rsz);
    }
    return out.toString();
}

From source file:Main.java

public static final int readerTryRead(Reader reader, char[] cc, int len) throws IOException {
    int r;/*www. j av a2s.  com*/
    int t = 0;
    while ((r = reader.read(cc, t, len - t)) >= 0) {
        t += r;
        if (t == len) {
            break;
        }
    }
    return t;
}

From source file:Main.java

private static String toString(InputStream is) throws Exception {
    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(is, "UTF-8");
    int read;//from ww w  .  j  a v  a 2  s  . c  o  m
    do {
        read = in.read(buffer, 0, buffer.length);
        if (read > 0) {
            out.append(buffer, 0, read);
        }
    } while (read >= 0);
    String result = out.toString();

    is.close();

    return result;
}

From source file:Main.java

public static String slurp(final InputStream is, final int bufferSize) {
    final char[] buffer = new char[bufferSize];
    final StringBuilder out = new StringBuilder();
    try {//from   www  .j a v  a 2s  .  c  o m
        final Reader in = new InputStreamReader(is, "UTF-8");
        try {
            for (;;) {
                int rsz = in.read(buffer, 0, buffer.length);
                if (rsz < 0)
                    break;
                out.append(buffer, 0, rsz);
            }
        } finally {
            in.close();
        }
    } catch (Exception ex) {
    }
    return out.toString();
}

From source file:Main.java

private static String stringFromInputStream(InputStream is) throws IOException {
    char[] buf = new char[1024];
    StringBuilder out = new StringBuilder();

    Reader in = new InputStreamReader(is, "UTF-8");

    int bin;/*from   ww  w. j  a  v  a  2s . co m*/
    while ((bin = in.read(buf, 0, buf.length)) >= 0) {
        out.append(buf, 0, bin);
    }

    return out.toString();
}

From source file:Main.java

public static String toString(InputStream is) {
    StringBuilder out = new StringBuilder();
    char[] buffer = new char[512];
    if (is != null) {
        try {//from w ww.  j  av  a2  s.c o m
            Reader in = new InputStreamReader(is, "UTF-8");
            try {
                int n;
                while ((n = in.read(buffer, 0, buffer.length)) >= 0) {
                    out.append(buffer, 0, n);
                }
            } finally {
                in.close();
            }
        } catch (IOException ex) {
            out.append("error");
        }
    }
    return out.toString();
}

From source file:Main.java

public static final String inputStreamToString(final InputStream is) throws IOException {
    final char[] buffer = new char[0x10000];
    final StringBuilder sb = new StringBuilder();
    final Reader in = new InputStreamReader(is, "utf-8");
    try {/*from   w ww. j ava2s.c om*/
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                sb.append(buffer, 0, read);
            }
        } while (read >= 0);
    } finally {
        closeSilently(in);
    }
    return sb.toString();
}

From source file:edu.mit.media.funf.IOUtils.java

public static String inputStreamToString(InputStream is, String encoding) throws IOException {
    final char[] buffer = new char[0x10000];
    StringBuilder out = new StringBuilder();
    Reader in = new InputStreamReader(is, encoding);
    int read;//from  w w  w . j av a  2 s.c  o m
    do {
        read = in.read(buffer, 0, buffer.length);
        if (read > 0) {
            out.append(buffer, 0, read);
        }
    } while (read >= 0);
    return out.toString();
}