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(char cbuf[]) throws IOException 

Source Link

Document

Reads characters into an array.

Usage

From source file:org.eclipse.swt.snippets.Snippet133.java

void menuOpen() {
    final String textString;
    FileDialog dialog = new FileDialog(shell, SWT.OPEN);
    dialog.setFilterExtensions(new String[] { "*.java", "*.*" });
    String name = dialog.open();/*  w w w .  j av a 2 s .c  o  m*/
    if ((name == null) || (name.length() == 0))
        return;

    File file = new File(name);
    try (FileInputStream stream = new FileInputStream(file.getPath())) {
        Reader in = new BufferedReader(new InputStreamReader(stream));
        char[] readBuffer = new char[2048];
        StringBuilder buffer = new StringBuilder((int) file.length());
        int n;
        while ((n = in.read(readBuffer)) > 0) {
            buffer.append(readBuffer, 0, n);
        }
        textString = buffer.toString();
        stream.close();
    } catch (FileNotFoundException e) {
        MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
        box.setMessage("File not found:\n" + name);
        box.open();
        return;
    } catch (IOException e) {
        MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
        box.setMessage("Error reading file:\n" + name);
        box.open();
        return;
    }

    text.setText(textString);
}

From source file:io.ucoin.ucoinj.core.client.service.HttpServiceImpl.java

protected String getContentAsString(InputStream content) throws IOException {
    Reader reader = new InputStreamReader(content, StandardCharsets.UTF_8);
    StringBuilder result = new StringBuilder();
    char[] buf = new char[64];
    int len = 0;/*w w w.ja  v a2 s  .  com*/
    while ((len = reader.read(buf)) != -1) {
        result.append(buf, 0, len);
    }
    return result.toString();
}

From source file:com.twentyn.patentScorer.PatentScorer.java

@Override
public void processPatentText(File patentFile, Reader patentTextReader, int patentTextLength)
        throws IOException, ParserConfigurationException, SAXException, TransformerConfigurationException,
        TransformerException, XPathExpressionException {

    System.out.println("Patent text length: " + patentTextLength);
    CharBuffer buff = CharBuffer.allocate(patentTextLength);
    int read = patentTextReader.read(buff);
    System.out.println("Read bytes: " + read);
    patentTextReader.reset();/*from   w ww.j  a v a 2  s .  com*/
    String fullContent = new String(buff.array());

    PatentDocument patentDocument = PatentDocument
            .patentDocumentFromXMLStream(new ReaderInputStream(patentTextReader, Charset.forName("utf-8")));
    if (patentDocument == null) {
        LOGGER.info("Found non-patent type document, skipping.");
        return;
    }

    double pr = this.patentModel.ProbabilityOf(fullContent);
    this.outputWriter
            .write(objectMapper.writeValueAsString(new ClassificationResult(patentDocument.getFileId(), pr)));
    this.outputWriter.write(LINE_SEPARATOR);
    System.out.println("Doc " + patentDocument.getFileId() + " has score " + pr);
}

From source file:com.example.ronald.tracle.MainActivity.java

public String convertInputStreamToString(InputStream stream, int length)
        throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[length];
    reader.read(buffer);
    return new String(buffer);
}

From source file:xdroid.ui.core.cache.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;//  w  w w .  jav  a  2 s .  c o  m
    try {
        writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }

            if (writer != null) {
                writer.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

From source file:de.mpg.escidoc.pubman.multipleimport.processor.ZfNProcessor.java

private void initialize() {
    this.init = true;

    try {// ww  w . j a v a2  s  .c o m
        InputStream in = new FileInputStream(getSourceFile());
        ArrayList<String> itemList = new ArrayList<String>();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        String item = null;
        final int bufLength = 1024;
        char[] buffer = new char[bufLength];
        int readReturn;
        int count = 0;

        ZipEntry zipentry;
        ZipInputStream zipinputstream = new ZipInputStream(in);

        while ((zipentry = zipinputstream.getNextEntry()) != null) {
            count++;
            StringWriter sw = new StringWriter();
            Reader reader = new BufferedReader(new InputStreamReader(zipinputstream, "UTF-8"));

            while ((readReturn = reader.read(buffer)) != -1) {
                sw.write(buffer, 0, readReturn);
            }

            item = new String(sw.toString());
            itemList.add(item);
            this.fileNames.add(zipentry.getName());

            reader.close();
            zipinputstream.closeEntry();

        }

        this.logger.debug("Zip file contains " + count + "elements");
        zipinputstream.close();
        this.counter = 0;

        this.originalData = byteArrayOutputStream.toByteArray();
        this.items = itemList.toArray(new String[] {});
        this.length = this.items.length;
    } catch (Exception e) {
        this.logger.error("Could not read zip File: " + e.getMessage());
        throw new RuntimeException("Error reading input stream", e);
    }
}

From source file:communication.WeatherService.java

private JSONObject doQuery(String subUrl) throws JSONException, IOException {
    String responseBody = null;/*  w w  w.  j  av a  2 s .  c o m*/
    HttpGet httpget = new HttpGet(this.baseOwmUrl + subUrl);
    if (this.owmAPPID != null) {
        httpget.addHeader(WeatherService.APPID_HEADER, this.owmAPPID);
    }
    System.out.println("getting...");
    HttpResponse response = this.httpClient.execute(httpget);
    System.out.println("...got");
    InputStream contentStream = null;
    try {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine == null) {
            throw new IOException(String.format("Unable to get a response from OWM server"));
        }
        int statusCode = statusLine.getStatusCode();
        if (statusCode < 200 && statusCode >= 300) {
            throw new IOException(
                    String.format("OWM server responded with status code %d: %s", statusCode, statusLine));
        }
        /* Read the response content */
        HttpEntity responseEntity = response.getEntity();
        contentStream = responseEntity.getContent();
        Reader isReader = new InputStreamReader(contentStream);
        int contentSize = (int) responseEntity.getContentLength();
        if (contentSize < 0)
            contentSize = 8 * 1024;
        StringWriter strWriter = new StringWriter(contentSize);
        char[] buffer = new char[8 * 1024];
        int n = 0;
        while ((n = isReader.read(buffer)) != -1) {
            strWriter.write(buffer, 0, n);
        }
        responseBody = strWriter.toString();
        contentStream.close();
    } catch (IOException e) {
        throw e;
    } catch (RuntimeException re) {
        httpget.abort();
        throw re;
    } finally {
        if (contentStream != null)
            contentStream.close();
    }
    System.out.println(responseBody); //Testing by Brandon
    return new JSONObject(responseBody);
}

From source file:se.kb.oai.pmh.OaiPmhServer.java

private Document executeQuery(String query) throws OAIException, IOException {
    LOGGER.info(" --->  Sending query to OAI-PMH-server: ---> \n {}", query);
    if (this.client == null) {
        // if there is no special client initialized use the default one
        try {// w  ww . j  a v a 2s .c om
            return this.reader.read(query);
        } catch (DocumentException e) {
            LOGGER.error("Problem parsing OAI-PMH result from query {}", query);
            throw new OAIException("", e);
        }
    } else {
        HttpGet httpget = new HttpGet(query);
        HttpResponse response = this.client.execute(httpget);
        String message = "";
        try {
            InputStream en = response.getEntity().getContent();

            if (en != null) {
                Writer writer = new StringWriter();
                char[] buffer = new char[1024];
                try {
                    Reader reader = new BufferedReader(new InputStreamReader(en, Charsets.UTF_8));
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                        message = writer.toString();
                    }
                } finally {
                    en.close();
                }
            }

            StringReader sr = new StringReader(message);
            LOGGER.info("<--- Received result from OAI-PMH-server: <---\n {}", message);
            return this.reader.read(sr);
        } catch (DocumentException de) {
            LOGGER.error("Problem parsing OAI-PMH result from query {} with result {}", query, message);
            throw new OAIException("", de);
        }
    }
}

From source file:com.silverpeas.jcrutil.model.impl.AbstractJcrTestCase.java

protected String readFile(String path) throws IOException {
    CharArrayWriter writer = null;
    InputStream in = null;/*from   w w w .j a  va 2 s  .co m*/
    Reader reader = null;
    try {
        in = new FileInputStream(path);
        writer = new CharArrayWriter();
        reader = new InputStreamReader(in);
        char[] buffer = new char[8];
        int c = 0;
        while ((c = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, c);
        }
        return new String(writer.toCharArray());
    } catch (IOException ioex) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (in != null) {
            in.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:org.kotemaru.android.sample.webview.XMLHttpRequestXS.java

private String getEntityString(HttpEntity entity, String charset) throws IllegalStateException, IOException {
    InputStream in = entity.getContent();
    try {/*from  www .  j a  v a  2s  .  c  om*/
        Reader reader = new InputStreamReader(in, charset);
        StringBuilder sbuf = new StringBuilder();
        char[] buff = new char[1500];
        int n = 0;
        while ((n = reader.read(buff)) >= 0) {
            sbuf.append(buff, 0, n);
        }
        return sbuf.toString();
    } finally {
        in.close();
    }
}