Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

In this page you can find the example usage for java.io StringWriter write.

Prototype

public void write(String str, int off, int len) 

Source Link

Document

Write a portion of a string.

Usage

From source file:de.innovationgate.wgpublisher.webtml.Include.java

private void performURLInclude() throws TMLException {

    Status status = (Status) getStatus();

    try {//  w  w  w.  ja  va2 s.  c o m

        HttpClient client = WGFactory.getHttpClientFactory().createHttpClient();
        HttpMethodParams methodParams = new HttpMethodParams();
        methodParams.setSoTimeout(Integer.parseInt(getTimeout()));
        methodParams.setVersion(HttpVersion.HTTP_1_1);

        GetMethod getMethod = new GetMethod();
        getMethod.setURI(new URI(status.ref, false));
        getMethod.setParams(methodParams);
        getMethod.setFollowRedirects(true);

        int httpStatus = client.executeMethod(getMethod);
        if (httpStatus != HttpServletResponse.SC_OK) {
            throw new TMLException("Response status " + httpStatus + " (" + getMethod.getStatusText()
                    + ") for included URL " + ref, true);
        }

        String encoding = getEncoding();
        if (encoding == null) {
            encoding = getMethod.getResponseCharSet();
            if (encoding == null) {
                getTMLContext().addwarning("No encoding returned from URL '" + status.ref
                        + "'. Assuming default encoding " + encoding);
                encoding = getCore().getCharacterEncoding();
            }
        }

        Reader reader = new InputStreamReader(getMethod.getResponseBodyAsStream(), encoding);
        StringWriter writer = new StringWriter();
        char[] buf = new char[2048];
        long count = 0;

        String limitStr = getLimit();
        long charLimit = 0;
        try {
            charLimit = Math.round(1024 * 1024 * Double.parseDouble(limitStr));
        } catch (NumberFormatException e) {
            throw new TMLException("Cannot parse limit attribute as number: " + limitStr);
        }

        int len;
        while ((len = reader.read(buf)) != -1) {
            writer.write(buf, 0, len);
            count += len;
            if (charLimit != 0 && count > charLimit) {
                throw new TMLException("Include of URL '" + status.ref + "' reaches content limit of "
                        + limitStr + " million characters. Include is cancelled.");
            }
        }
        this.setResult(writer.toString());

    } catch (java.io.IOException exc) {
        log.error("Exception including url", exc);
        this.addWarning("Exception while including url: " + exc.getMessage());
    }

}

From source file:org.kaaproject.kaa.server.control.cli.ControlServerCliIT.java

/**
 * Gets the test file content./*from   w ww.j a va  2  s. co m*/
 *
 * @param file the file
 * @return the test file content
 */
private String getTestFileContent(String file) {
    String targetPath = System.getProperty("targetPath");
    File targetDir = new File(targetPath);
    File testFile = new File(targetDir.getAbsolutePath() + File.separator + "test-classes" + File.separator
            + "data" + File.separator + file);
    BufferedReader reader = null;
    String result = null;
    try {
        StringWriter stringWriter = new StringWriter();
        reader = new BufferedReader(new FileReader(testFile));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            stringWriter.write(buf, 0, numRead);
        }
        result = stringWriter.toString();

    } catch (IOException e) {
        logger.error("Unable to read from specified file '" + testFile + "'! Error: " + e.getMessage(), e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ioe) {
                logger.error("Unable to close file '" + testFile + "'! Error: " + ioe.getMessage(), ioe);
            }
        }
    }
    return result;
}