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:cn.isif.util_plus.cache.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;
    try {//  ww  w .j a  va2s.c o  m
        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 {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }
}

From source file:com.plusub.lib.other.LruDiskCache.java

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;
    try {/*  w w  w  .  j a  v a 2 s  .  com*/
        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 {
            reader.close();
            writer.close();
        } catch (Throwable e) {
        }
    }
}

From source file:org.apache.vxquery.xtest.TestCaseResult.java

private String slurpFile(File f) {
    StringWriter out = new StringWriter();
    try {// w ww.  j a  v  a 2 s  .  c  o  m
        FileReader in = new FileReader(f);
        try {
            char[] buffer = new char[8192];
            int c;
            while ((c = in.read(buffer)) >= 0) {
                out.write(buffer, 0, c);
            }
        } finally {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    } catch (IOException e) {
        return null;
    }
    return out.toString();
}

From source file:org.i3xx.step.uno.impl.ScriptCacheImpl.java

/**
 * Reads a script from the cache/* w w w .  ja  v a2 s .c  om*/
 * 
 * @param index The index of the cached element
 * @return The String read
 * @throws IOException
 */
public String read(int index) throws IOException {
    ByteArrayInputStream bin = new ByteArrayInputStream(buffer[index]);
    InputStream in = compress ? new GZIPInputStream(bin) : bin;
    InputStreamReader r = new InputStreamReader(in);
    StringWriter w = new StringWriter();

    char[] cbuf = new char[1024];
    int c = 0;

    while ((c = r.read(cbuf)) > -1)
        w.write(cbuf, 0, c);

    r.close();
    w.close();

    return w.toString();
}

From source file:org.wapama.web.server.UUIDBasedRepositoryServlet.java

/**
 * This method saves the model contents based on the json sent as the
 * body of the request./*from ww w .  ja va  2 s .  co m*/
 * 
 * The json should look like:
 * 
 * { "data" : ....,
 *   "svg" : <svg>...</svg>,
 *   "uuid" : "1234",
 *   "profile" : "default"
 * }
 * 
 * The data is the json representation of the model.
 * The svg represents the graphical model as a SVG format.
 */
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    BufferedReader reader = req.getReader();
    StringWriter reqWriter = new StringWriter();
    char[] buffer = new char[4096];
    int read;
    while ((read = reader.read(buffer)) != -1) {
        reqWriter.write(buffer, 0, read);
    }

    String data = reqWriter.toString();
    try {
        JSONObject jsonObject = new JSONObject(data);

        String json = (String) jsonObject.get("data");
        String svg = (String) jsonObject.get("svg");
        String uuid = (String) jsonObject.get("uuid");
        String profileName = (String) jsonObject.get("profile");
        boolean autosave = jsonObject.getBoolean("savetype");

        if (_logger.isDebugEnabled()) {
            _logger.debug("Calling UUIDBasedRepositoryServlet doPost()...");
            _logger.debug("autosave: " + autosave);
        }

        IDiagramProfile profile = getProfile(req, profileName);
        if (_logger.isDebugEnabled()) {
            _logger.debug("Begin saving the diagram");
        }
        _repository.save(req, uuid, json, svg, profile, autosave);
        if (_logger.isDebugEnabled()) {
            _logger.debug("Finish saving the diagram");
        }
    } catch (JSONException e1) {
        throw new ServletException(e1);
    } catch (DiagramValidationException e) {
        // set the error JSON to response
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write(e.getErrorJsonStr());
    }
}

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

private static String readFully(Reader reader) throws IOException {
    StringWriter writer = null;
    try {//from   www .j a v  a2  s.com
        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:org.bitpipeline.lib.owm.OwmClient.java

private JSONObject doQuery(String subUrl) throws JSONException, IOException {
    String responseBody = null;/*  ww  w  .j  a  v a2  s  .  c  o  m*/
    HttpGet httpget = new HttpGet(this.baseOwmUrl + subUrl);
    if (this.owmAPPID != null) {
        httpget.addHeader(OwmClient.APPID_HEADER, this.owmAPPID);
    }

    HttpResponse response = this.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 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();
    }
    return new JSONObject(responseBody);
}

From source file:android.net.http.AbstractProxyTest.java

private String contentToString(HttpResponse response) throws IOException {
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader = new InputStreamReader(response.getEntity().getContent());
    int length;//w ww . j  av  a2s.c  o  m
    while ((length = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, length);
    }
    reader.close();
    return writer.toString();
}

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

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

    try {/*from  w w w  .j a v  a2  s . c om*/
        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:com.webcohesion.enunciate.EnunciateConfiguration.java

public String readGeneratedCodeLicenseFile() {
    License license = getGeneratedCodeLicense();
    String filePath = license == null ? null : license.getFile();
    if (filePath == null) {
        return null;
    }/*  ww  w .  j av a 2s.  co  m*/

    File file = resolveFile(filePath);
    try {
        FileReader reader = new FileReader(file);
        StringWriter writer = new StringWriter();
        char[] chars = new char[100];
        int read = reader.read(chars);
        while (read >= 0) {
            writer.write(chars, 0, read);
        }
        reader.close();
        writer.close();
        return writer.toString();
    } catch (IOException e) {
        throw new EnunciateException(e);
    }
}