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.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Copy chars from a large (over 2GB) <code>Reader</code> to a
 * <code>Writer</code>.// ww  w.  j a v a2s  .c o  m
 * <p>
 * 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(final Reader input, final Writer output) throws IOException {
    final 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:cz.tomas.StockAnalyze.utils.DownloadService.java

public String readStream(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {/* ww w  .j  a va 2  s.c  om*/
            Reader reader = new BufferedReader(new InputStreamReader(is));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}

From source file:architecture.common.license.io.LicenseReader.java

private String decodeToXml(Reader in) throws IOException {
    StringBuffer text = new StringBuffer();
    char buf[] = new char[1024];
    int len;//from  w ww.  jav  a2s .  co m
    while ((len = in.read(buf)) >= 0) {
        int j = 0;
        while (j < len) {
            char ch = buf[j];
            if (Character.isLetter(ch) || Character.isDigit(ch) || ch == '+' || ch == '/' || ch == '=')
                text.append(ch);
            j++;
        }
    }
    in.close();
    String xml = new String(
            Base64.decodeBase64(text.toString().getBytes(ApplicationConstants.DEFAULT_CHAR_ENCODING)));
    if (!assertionsDisabled && !text.toString().matches("^[^\\s]*$")) {
        throw new AssertionError();
    } else {
        log.debug(xml);
        return xml;
    }
}

From source file:it.tidalwave.northernwind.frontend.ui.component.DefaultStaticHtmlFragmentViewController.java

protected void populate(final @Nonnull String htmlResourceName, final @Nonnull Map<String, String> attributes)
        throws IOException {
    final Resource htmlResource = new ClassPathResource(htmlResourceName, getClass());
    final @Cleanup Reader r = new InputStreamReader(htmlResource.getInputStream());
    final CharBuffer charBuffer = CharBuffer.allocate((int) htmlResource.contentLength());
    final int length = r.read(charBuffer);
    r.close();/*from ww w. j  ava 2  s  .  com*/
    final String html = new String(charBuffer.array(), 0, length);

    ST template = new ST(html, '$', '$');

    for (final Entry<String, String> entry : attributes.entrySet()) {
        template = template.add(entry.getKey(), entry.getValue());
    }

    view.setContent(template.render());
}

From source file:com.fatwire.dta.sscrawler.UrlRenderingCallable.java

/**
 * @param builder//w ww.ja  v a2  s .com
 * @param reader
 * @throws IOException
 */
private String copy(final Reader reader) throws IOException {
    final StringBuilder builder = new StringBuilder();
    final char[] c = new char[1024];
    int s;
    while ((s = reader.read(c)) != -1) {
        builder.append(c, 0, s);

    }
    return builder.toString();
}

From source file:org.geosdi.geoplatform.gui.server.command.publish.cas.CasPublishLayerPreviewCommand.java

@Override
public PublishLayerPreviewResponse execute(CasPublishLayerPreviewRequest request,
        HttpServletRequest httpServletRequest) {
    try {//w w w  .  j  a  va 2 s  . co m
        sessionUtility.getLoggedAccount(httpServletRequest);
    } catch (GPSessionTimeout timeout) {
        throw new GeoPlatformException(timeout);
    }
    String result = null;
    try {
        List<String> layerList = request.getLayerList();
        casPublisherService.publishAll(httpServletRequest.getSession().getId(), "previews", "dataTest",
                layerList);
        boolean reloadCluster = request.isReloadCluster();
        if (reloadCluster) {
            HttpResponse response = httpclient.execute(targetHost, httpget, localContext);
            //                HttpResponse response = httpclient.execute(get, localContext);
            InputStream is = response.getEntity().getContent();
            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();
            }
            result = writer.toString();
        }
    } catch (ResourceNotFoundFault ex) {
        logger.error("Error on publish shape: " + ex);
        throw new GeoPlatformException("Error on publish shape.");
    } catch (FileNotFoundException ex) {
        logger.error("Error on publish shape: " + ex);
        throw new GeoPlatformException("Error on publish shape.");
    } catch (MalformedURLException e) {
        logger.error("Error on cluster url: " + e);
        throw new GeoPlatformException(new GPReloadURLException("Error on cluster url."));
    } catch (IOException e) {
        logger.error("Error on reloading cluster: " + e);
        throw new GeoPlatformException(new GPReloadURLException("Error on reloading cluster."));
    }
    return new PublishLayerPreviewResponse(result);

}

From source file:es.eucm.eadventure.tracking.prv.service.TrackingPoster.java

public String openSession() {
    if (baseURL != null)
        return baseURL;

    try {//from  w ww.  j a va 2s  .c  o  m
        HttpClient httpclient = new DefaultHttpClient();
        URI uri = URIUtils.createURI("http", serviceURL, -1, "", null, null);
        HttpPost httppost = new HttpPost(uri);
        HttpResponse response;

        response = httpclient.execute(httppost);
        if (Integer.toString(response.getStatusLine().getStatusCode()).startsWith("2")) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();

                StringWriter strWriter = new StringWriter();

                char[] buffer = new char[1024];

                try {
                    Reader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        strWriter.write(buffer, 0, n);
                    }
                } finally {
                    instream.close();
                }
                baseURL = strWriter.toString();

            }
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
    return baseURL;
}

From source file:de.betterform.agent.web.servlet.UploadServlet.java

private String getFieldValue(FileItem item) throws IOException {
    InputStream is = item.getInputStream();
    if (is != null) {
        Writer writer = new StringWriter();
        try {//from w  w w.  ja  va2  s .c om
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            char[] buffer = new char[1024];
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return null;
    }
}

From source file:org.geosdi.geoplatform.connector.server.request.GPPostConnectorRequest.java

@Override
public String getResponseAsString() throws ServerInternalFault, IOException, IllegalParameterFault {
    Writer writer = new StringWriter();
    try {/*  w w  w .  j a v a2 s .  c o  m*/
        HttpResponse httpResponse = super.securityConnector.secure(this, this.getPostMethod());
        HttpEntity responseEntity = httpResponse.getEntity();

        if (responseEntity != null) {
            InputStream is = responseEntity.getContent();

            char[] buffer = new char[1024];

            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            EntityUtils.consume(responseEntity);
        } else {
            throw new ServerInternalFault("Connector Server Error: Connection " + "problem");
        }

    } catch (JAXBException ex) {
        logger.error("\n@@@@@@@@@@@@@@@@@@ JAXBException *** {} ***", ex.getMessage());
        throw new ServerInternalFault("*** JAXBException ***");

    } catch (ClientProtocolException ex) {
        logger.error("\n@@@@@@@@@@@@@@@@@@ ClientProtocolException *** {} ***", ex.getMessage());
        throw new ServerInternalFault("*** ClientProtocolException ***");

    }

    return writer.toString();
}