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:com.dclab.preparation.ReadTest.java

public String scan(Reader r) {
    try {/*from  ww w . j av a 2s .c o m*/
        CharBuffer cb = CharBuffer.allocate(MAX_CAPACITY);
        int i = r.read(cb);
        Logger.getAnonymousLogger().log(Level.INFO, "read {0} bytes", i);

        String content = cb.toString();
        return extract(content);
    } catch (IOException ex) {
        Logger.getLogger(ReadTest.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:lcn.module.oltp.web.common.base.handler.AltibaseClobStringTypeHandler.java

protected Object getResultInternal(ResultSet rs, int index, LobHandler lobHandler) throws SQLException {

    StringBuffer read_data = new StringBuffer("");
    int read_length;

    char[] buf = new char[1024];

    Reader rd = lobHandler.getClobAsCharacterStream(rs, index);
    try {/*from   ww w .ja  va 2s.  c  o  m*/
        while ((read_length = rd.read(buf)) != -1) {
            read_data.append(buf, 0, read_length);
        }
    } catch (IOException ie) {
        SQLException sqle = new SQLException(ie.getMessage());
        throw sqle;
        // 2011.10.10 ? ?
    } finally {
        if (rd != null) {
            try {
                rd.close();
            } catch (Exception ignore) {
                LOG.debug("IGNORE: " + ignore.getMessage());
            }
        }
    }

    return read_data.toString();

    //return lobHandler.getClobAsString(rs, index);
}

From source file:org.apache.manifoldcf.scriptengine.ScriptParser.java

public static String convertToString(HttpResponse httpResponse) throws IOException {
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        InputStream is = entity.getContent();
        try {/*w  w w  .j  ava2s. c o m*/
            Charset charSet;
            try {
                ContentType ct = ContentType.get(entity);
                if (ct == null)
                    charSet = StandardCharsets.UTF_8;
                else
                    charSet = ct.getCharset();
            } catch (ParseException e) {
                charSet = StandardCharsets.UTF_8;
            }
            char[] buffer = new char[65536];
            Reader r = new InputStreamReader(is, charSet);
            Writer w = new StringWriter();
            try {
                while (true) {
                    int amt = r.read(buffer);
                    if (amt == -1)
                        break;
                    w.write(buffer, 0, amt);
                }
            } finally {
                w.flush();
            }
            return w.toString();
        } finally {
            is.close();
        }
    }
    return "";
}

From source file:com.aperigeek.dropvault.dav.DropDAVClient.java

private void login() throws InvalidPasswordException, DAVException {
    String url = MessageFormat.format(LOGIN_URL, URLEncoder.encode(this.username), hash.hash(this.password));
    HttpGet get = new HttpGet(url);
    try {/*  www  .j ava  2  s  .  c o m*/
        HttpResponse response = client.execute(get);

        if (response.getStatusLine().getStatusCode() == 401) {
            throw new InvalidPasswordException();
        }

        if (response.getStatusLine().getStatusCode() != 200) {
            throw new DAVException("Server error");
        }

        InputStream in = response.getEntity().getContent();
        StringWriter writer = new StringWriter();
        Reader reader = new InputStreamReader(in);
        char[] buffer = new char[128];
        int readed;
        while ((readed = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, readed);
        }
        in.close();
        baseUri = writer.toString().trim();
    } catch (IOException ex) {
        throw new DAVException("Login URL unavailable", ex);
    }
}

From source file:org.zilverline.extractors.TestHTMLExtractor.java

public void testGetContentAsFile() {
    HTMLExtractor hex = new HTMLExtractor();
    File file = new File("test\\data\\test.html");
    // File file = new File("d:\\books\\problems\\manual_fr.htm2");
    Reader r = hex.getContent(file);
    assertNotNull(r);/*from   w w  w .j  a  va  2s. c  o  m*/

    char[] text = new char[50];
    try {
        assertTrue(r.read(text) > 0);
        log.debug("Content with length of 50: " + new String(text));
    } catch (IOException e) {
        fail(e.getMessage());
    }
    assertTrue(text.length > 0);
}

From source file:es.upm.dit.gsi.noticiastvi.gtv.item.SetRemoveFavoriteThread.java

public String convertStreamToString(InputStream is) throws IOException {
    /*//from w  w  w .j  a va 2 s . c  o  m
     * To convert the InputStream to String we use the
     * Reader.read(char[] buffer) method. We iterate until the
     * Reader return -1 which means there's no more data to
     * read. We use the StringWriter class to produce the string.
     */
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:guru.nidi.raml.doc.servlet.MirrorServlet.java

private void copy(Reader in, Writer out) throws IOException {
    final char[] buf = new char[10000];
    int read;/*from w w w  . jav a2s. com*/
    while ((read = in.read(buf)) > 0) {
        out.write(buf, 0, read);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.freemarker.DelimitingTemplateLoader.java

private StringBuilder readTemplateSource(String encoding, Object ts) throws IOException {
    StringBuilder sb = new StringBuilder();
    Reader reader = innerLoader.getReader(ts, encoding);
    char[] buffer = new char[8192];
    int howmany;//  ww  w. j av a 2  s.c  om
    while (-1 != (howmany = reader.read(buffer))) {
        sb.append(buffer, 0, howmany);
    }
    return sb;
}

From source file:IOUtils.java

/**
 * Copy chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
 * <p>/*from  ww  w  . ja va 2s  .  co  m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 *
 * @param input  the <code>Reader</code> to read from
 * @param output  the <code>Writer</code> to write to
 * @return the number of characters copied
 * @throws NullPointerException if the input or output is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.3
 */
public static long copyLarge(Reader input, Writer output) throws IOException {
    char[] buffer = new char[DEFAULT_BUFFER_SIZE];
    long count = 0;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

From source file:it.tidalwave.northernwind.frontend.ui.component.gallery.spi.GalleryAdapterSupport.java

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
@Nonnull//from  w w  w .j  a  v  a  2 s .co  m
private String loadDefaultTemplate(final @Nonnull String templateName) throws IOException {
    final String packagePath = getClass().getPackage().getName().replace('.', '/');
    final Resource resource = new ClassPathResource("/" + packagePath + "/" + templateName);
    final @Cleanup Reader r = new InputStreamReader(resource.getInputStream());
    final char[] buffer = new char[(int) resource.contentLength()];
    r.read(buffer);
    return new String(buffer);
}