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(char cbuf[], int offset, int length) 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) throws IOException {
    char[] cbuf = new char[5];

    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // reads into the char buffer
    int i = isr.read(cbuf, 2, 3);

    // prints the number of characters
    System.out.println("Number of characters read: " + i);

    // for each character in the character buffer
    for (char c : cbuf) {
        // for empty character
        if (((int) c) == 0)
            c = '-';

        System.out.println(c);//from   www. j  a  v a  2 s  .  co m
    }
    isr.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zip = new ZipFile(new File("sample.zip"));

    for (Enumeration e = zip.entries(); e.hasMoreElements();) {
        ZipEntry entry = (ZipEntry) e.nextElement();
        System.out.println("File name: " + entry.getName() + "; size: " + entry.getSize()
                + "; compressed size: " + entry.getCompressedSize());
        InputStream is = zip.getInputStream(entry);
        InputStreamReader isr = new InputStreamReader(is);

        char[] buffer = new char[1024];
        while (isr.read(buffer, 0, buffer.length) != -1) {
            String s = new String(buffer);
            System.out.println(s.trim());
        }/*from w w w  . j ava 2 s .c o m*/
    }
}

From source file:Main.java

public static String readText(InputStream in) {
    try {/*w  w  w . ja  v a  2 s . co  m*/
        int available = in.available();
        if (available > 0) {
            char[] text = new char[available];
            InputStreamReader reader = new InputStreamReader(in);
            int readed = reader.read(text, 0, available);
            return new String(text);
        }
    } catch (IOException e) {
    }
    return null;
}

From source file:Main.java

public static String readStringFromInputStreamAndCloseStream(final InputStream inputStream,
        final int bufferSize) throws IOException {
    if (bufferSize <= 0) {
        // Safe close: it's more important to alert the programmer of
        // their error than to let them catch and continue on their way.
        throw new IllegalArgumentException("Expected buffer size larger than 0. Got: " + bufferSize);
    }//from   w  ww  .  ja v a 2  s  .co  m

    final StringBuilder stringBuilder = new StringBuilder(bufferSize);
    final InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
    try {
        int charsRead;
        final char[] buffer = new char[bufferSize];
        while ((charsRead = reader.read(buffer, 0, bufferSize)) != -1) {
            stringBuilder.append(buffer, 0, charsRead);
        }
    } finally {
        reader.close();
    }
    return stringBuilder.toString();
}

From source file:com.telekom.api.common.webrequest.JsonSerializer.java

public static <T> T deserialize(InputStream stream, Class<T> valueType) throws IOException {

    // Convert stream to string first , because the string deserialization
    // routine/*from  www  . j a va2 s. co  m*/
    // contains a workaround
    InputStreamReader sr = new InputStreamReader(stream);
    StringBuilder sbBuilder = new StringBuilder();
    char[] buff = new char[1024];
    while (true) {
        int charsRead = sr.read(buff, 0, buff.length);

        if (charsRead == -1)
            break;

        sbBuilder.append(buff, 0, charsRead);
    }
    String content = sbBuilder.toString();

    return deserialize(content, valueType);
    // return mapper.readValue(stream, valueType);
}

From source file:fr.ms.tomcat.manager.TomcatManagerUrl.java

private static String toString(final InputStream in, final String charset) throws IOException {
    final InputStreamReader reader = new InputStreamReader(in, charset);

    final StringBuffer buffer = new StringBuffer();
    final char[] chars = new char[1024];
    int n;//w ww  .j  a va 2s.c om
    while ((n = reader.read(chars, 0, chars.length)) != -1) {
        buffer.append(chars, 0, n);
    }

    return buffer.toString();
}

From source file:com.commonsware.android.webserver.template.WebServerService.java

public static String slurp(final InputStream is) throws IOException {
    final char[] buffer = new char[1024];
    final StringBuilder out = new StringBuilder();
    final InputStreamReader in = new InputStreamReader(is, "UTF-8");

    while (true) {
        int rsz = in.read(buffer, 0, buffer.length);
        if (rsz < 0)
            break;
        out.append(buffer, 0, rsz);//from  w  w w.j a v  a2  s  .c  om
    }

    return (out.toString());
}

From source file:sf.net.experimaestro.manager.js.JavaScriptChecker.java

static String getFileContent(FileObject file) throws IOException {
    InputStreamReader reader = new InputStreamReader(file.getContent().getInputStream());
    char[] cbuf = new char[8192];
    int read;/*from   w  w w  .  j  a v a 2  s .co  m*/
    StringBuilder builder = new StringBuilder();
    while ((read = reader.read(cbuf, 0, cbuf.length)) > 0)
        builder.append(cbuf, 0, read);
    return builder.toString();
}

From source file:org.planetcrypto.bitcoin.PlanetCryptoBitcoinMinerAPICommands.java

private static String process(String cmd, InetSocketAddress ip, int port) throws Exception {
    Socket socket = new Socket();
    StringBuffer sb = new StringBuffer();
    char buf[] = new char[MAXRECEIVESIZE];
    int len = 0;//from  ww  w. j  av  a2s  . c  o  m
    //System.out.println("Attempting to send: " + cmd + " to: "+ ip.getHostAddress()+":"+port);
    try {
        //socket = new Socket(ip, port);
        socket.connect(ip, 2000);
        //System.out.println("Start Sleep");
        //Thread.sleep(1000);
        //System.out.println("Stop Sleep");
        socket.setSoTimeout(2000);
        //socket.(ip, port);
        //System.out.println(socket.getSoTimeout());
        PrintStream ps = new PrintStream(socket.getOutputStream());
        ps.print(cmd.toLowerCase().toCharArray());
        InputStreamReader isr = new InputStreamReader(socket.getInputStream());
        while (0x80085 > 0) {
            len = isr.read(buf, 0, MAXRECEIVESIZE);
            if (len < 1) {
                break;
            }
            sb.append(buf, 0, len);
            if (buf[len - 1] == '\0') {
                break;
            }
        }
        //closeAll();
        socket.close();
        socket = null;
    } catch (IOException ioe) {
        System.err.println(ioe.toString() + ", " + ip.getHostName());
        //closeAll();
        socket.close();
        socket = null;
        return "Failed to get information";
    }
    String result = sb.toString();
    //System.out.println(result);
    return result.replace("\0", "");
    //jsonFactory(result);
    //System.out.println(result);
}

From source file:io.hops.hopsworks.common.jobs.yarn.LogReader.java

/**
 * Writes all logs for a single container to the provided writer.
 *
 * @param valueStream//from  w ww.j  a va2 s .c om
 * @param writer
 * @throws IOException
 */
public static void readAcontainerLogs(DataInputStream valueStream, Writer writer) throws IOException {
    int bufferSize = 65536;
    char[] cbuf = new char[bufferSize];
    String fileType;
    String fileLengthStr;
    long fileLength;

    while (true) {
        try {
            fileType = valueStream.readUTF();
        } catch (EOFException e) {
            // EndOfFile
            return;
        }
        fileLengthStr = valueStream.readUTF();
        fileLength = Long.parseLong(fileLengthStr);
        writer.write("\n\nLogType:");
        writer.write(fileType);
        writer.write("\nLogLength:");
        writer.write(fileLengthStr);
        writer.write("\nLog Contents:\n");
        // ByteLevel
        BoundedInputStream bis = new BoundedInputStream(valueStream, fileLength);
        InputStreamReader reader = new InputStreamReader(bis);
        int currentRead = 0;
        int totalRead = 0;
        while ((currentRead = reader.read(cbuf, 0, bufferSize)) != -1) {
            writer.write(cbuf, 0, currentRead);
            totalRead += currentRead;
        }
    }
}