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:it.polimi.brusamentoceruti.moviebookrest.boundary.JsonRequest.java

public static JSONObject doQuery(String Url) throws JSONException, IOException {
    String responseBody = null;/*from  ww w  .  j  a va2  s .  c  o  m*/
    HttpGet httpget;
    HttpClient httpClient = new DefaultHttpClient();
    try {
        httpget = new HttpGet(Url);
    } catch (IllegalArgumentException iae) {
        return null;
    }

    HttpResponse response = httpClient.execute(httpget);
    InputStream contentStream = null;
    try {
        StatusLine statusLine = response.getStatusLine();
        if (statusLine == null) {
            throw new IOException(String.format("Unable to get a response from 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();
    }
    return new JSONObject(responseBody);
}

From source file:org.apache.roller.planet.config.PlanetRuntimeConfig.java

/**
 * Get the runtime configuration definitions XML file as a string.
 *
 * This is basically a convenience method for accessing this file.
 * The file itself contains meta-data about what configuration
 * properties we change at runtime via the UI and how to setup
 * the display for editing those properties.
 *///from ww w .  ja v  a  2s .  c om
public static String getRuntimeConfigDefsAsString() {

    log.debug("Trying to load runtime config defs file");

    try {
        InputStreamReader reader = new InputStreamReader(
                PlanetConfig.class.getResourceAsStream(runtime_config));
        StringWriter configString = new StringWriter();

        char[] buf = new char[8196];
        int length = 0;
        while ((length = reader.read(buf)) > 0)
            configString.write(buf, 0, length);

        reader.close();

        return configString.toString();
    } catch (Exception e) {
        log.error("Error loading runtime config defs file", e);
    }

    return "";
}

From source file:com.sun.socialsite.config.RuntimeConfig.java

/**
 * Get the runtime configuration definitions XML file as a string.
 *
 * This is basically a convenience method for accessing this file.
 * The file itself contains meta-data about what configuration
 * properties we change at runtime via the UI and how to setup
 * the display for editing those properties.
 *///from w  w w . j av  a 2 s .c  om
public static String getRuntimeConfigDefsAsString() {

    log.debug("Trying to load runtime config defs file");

    try {

        InputStream in = Config.class.getResourceAsStream(DEFINITIONS_FILE);
        InputStreamReader reader = new InputStreamReader(in);
        StringWriter configString = new StringWriter();

        char[] buf = new char[8196];
        int length = 0;
        while ((length = reader.read(buf)) > 0) {
            configString.write(buf, 0, length);
        }

        reader.close();

        return configString.toString();

    } catch (Exception e) {
        log.error("Error loading runtime config defs file", e);
    }

    return "";
}

From source file:Main.java

/**
 * Using a Reader and a Writer, returns a String from an InputStream.
 *
 * Method based on Hypersonic Code/*from   w  ww  . j a  v  a  2 s .c  om*/
 *
 * @param x InputStream to read from
 * @throws IOException
 * @return a Java string
 */
public static String inputStreamToString(InputStream x, String encoding) throws IOException {

    InputStreamReader in = new InputStreamReader(x, encoding);
    StringWriter writer = new StringWriter();
    int blocksize = 8 * 1024;
    char[] buffer = new char[blocksize];

    for (;;) {
        int read = in.read(buffer);

        if (read == -1) {
            break;
        }

        writer.write(buffer, 0, read);
    }

    writer.close();

    return writer.toString();
}

From source file:com.voa.weixin.utils.HttpUtils.java

private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {/*  w w w  . jav a  2s  .co  m*/
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
        StringWriter writer = new StringWriter();

        char[] chars = new char[1024];
        int count = 0;
        while ((count = reader.read(chars)) > 0) {
            writer.write(chars, 0, count);
        }

        return writer.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}

From source file:Main.java

/**
 * Convert InputStream to String//from w w w  . java  2  s .co  m
 *
 * @param stream inputStream
 * @return string
 */
private static String streamToString(InputStream stream) {
    StringWriter writer = null;
    if (stream != null) {
        try {
            InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
            writer = new StringWriter();
            int n;
            char[] buffer = new char[1024 * 4];
            while (-1 != (n = reader.read(buffer))) {
                writer.write(buffer, 0, n);
            }
        } catch (IOException e) {
            // @todo better logging
            e.printStackTrace();
        }
    }
    return writer != null ? writer.toString() : "";
}

From source file:UploadUtils.PomfUpload.java

/**
 * Get a response from Uguu./*from  w w w  .  j av a  2 s.c  o  m*/
 * @param conn the connection to use to listen to response.
 * @throws IOException during reading GZip response
 */
private static String getResponse(HttpURLConnection conn) throws IOException {
    String charset = "UTF-8";
    InputStream gzippedResponse = conn.getInputStream();
    InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse);
    Reader reader = new InputStreamReader(ungzippedResponse, charset);
    StringWriter writer = new StringWriter();
    char[] buffer = new char[10240];
    for (int length = 0; (length = reader.read(buffer)) > 0;) {
        writer.write(buffer, 0, length);
    }
    String response = writer.toString();
    writer.close();
    reader.close();
    reader.close();
    for (ImagelinkListener ll : listeners) {
        ll.onImageLink(response);
    }
    return response;
}

From source file:com.xerox.amazonws.ec2.EC2Utils.java

/**
 * This method makes a best effort to fetch a piece of instance metadata.
 *
 * @param key the name of the metadata to fetch
 * @return value of the metadata item//from w ww  . j a v a 2  s .co  m
 */
public static String getInstanceUserdata() throws IOException {
    int retries = 0;
    while (true) {
        try {
            URL url = new URL("http://169.254.169.254/latest/user-data/");
            InputStreamReader rdr = new InputStreamReader(url.openStream());
            StringWriter wtr = new StringWriter();
            char[] buf = new char[1024];
            int bytes;
            while ((bytes = rdr.read(buf)) > -1) {
                if (bytes > 0) {
                    wtr.write(buf, 0, bytes);
                }
            }
            rdr.close();
            return wtr.toString();
        } catch (IOException ex) {
            if (retries == 5) {
                logger.debug("Problem getting user data, retries exhausted...");
                return null;
            } else {
                logger.debug("Problem getting user data, retrying...");
                try {
                    Thread.sleep((int) Math.pow(2.0, retries) * 1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }
}

From source file:org.apache.roller.weblogger.config.WebloggerRuntimeConfig.java

/**
 * Get the runtime configuration definitions XML file as a string.
 *
 * This is basically a convenience method for accessing this file.
 * The file itself contains meta-data about what configuration
 * properties we change at runtime via the UI and how to setup
 * the display for editing those properties.
 *//*w w  w. j  ava 2s .  c o  m*/
public static String getRuntimeConfigDefsAsString() {

    log.debug("Trying to load runtime config defs file");

    try {
        InputStreamReader reader = new InputStreamReader(
                WebloggerConfig.class.getResourceAsStream(runtime_config));
        StringWriter configString = new StringWriter();

        char[] buf = new char[8196];
        int length = 0;
        while ((length = reader.read(buf)) > 0)
            configString.write(buf, 0, length);

        reader.close();

        return configString.toString();
    } catch (Exception e) {
        log.error("Error loading runtime config defs file", e);
    }

    return "";
}

From source file:com.dmsl.anyplace.utils.NetworkUtils.java

private static String readInputStream(InputStream stream) throws IOException {
    int n = 0;/*w  ww . ja  va2s .co  m*/
    char[] buffer = new char[1024 * 4];
    InputStreamReader reader = new InputStreamReader(stream, "UTF8");
    StringWriter writer = new StringWriter();
    while (-1 != (n = reader.read(buffer)))
        writer.write(buffer, 0, n);
    return writer.toString();
}