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) 

Source Link

Document

Write a string.

Usage

From source file:org.commoncrawl.service.crawler.CrawlHostImpl.java

public void feedQueue() {

    if (getQueue() != null) {

        if (!_inFeedQueue) {

            if (getHead() != null && (getHead().getDisposition() == CrawlList.Disposition.WaitingOnCompletion
                    || getHead().getDisposition() == CrawlList.Disposition.WaitingOnTime)) {
                LOG.warn("FeedQueue called on Host with Active List:" + getHead().toString());
                return;
            }/*from   w  w w. j av  a2 s. c  o m*/

            _inFeedQueue = true;

            boolean exitLoop = false;

            while (!exitLoop && getHead() != null) {

                CrawlList currentList = getHead();

                setIdle(false);

                CrawlTarget target = null;

                if (currentList.getDisposition() != CrawlList.Disposition.QueueEmpty) {
                    // get the next target for the current domain ...
                    target = currentList.getNextTarget();
                }

                if (target != null) {
                    // log it for now ...
                    if (Environment.detailLogEnabled())
                        LOG.info("Host: " + getIPAddressAsString() + " FeedQueue returned Target:"
                                + target.getOriginalURL());
                    // and queue it up with the fetcher 
                    getQueue().fetchItem(target);

                    // break out here ... 
                    exitLoop = true;
                } else {

                    // figure out what to do next ... 
                    if (currentList.getDisposition() == CrawlList.Disposition.WaitingOnTime) {
                        if (Environment.detailLogEnabled())
                            LOG.info("Feed Queue for List:" + currentList.getListName()
                                    + " Returned WaitingOnTime");
                        setTimer(currentList.calculateNextWaitTime());
                        exitLoop = true;
                    } else if (currentList.getDisposition() == CrawlList.Disposition.QueueEmpty) {

                        if (currentList.getListId() != CrawlerServer.getServer().getHighPriorityListId()) {
                            if (Environment.detailLogEnabled())
                                LOG.info("Feed Detected List:" + currentList.getListName()
                                        + " is in QueueEmpty state - removing...");
                            // remove the domain from the list ... 
                            _crawlLists.removeElement(currentList);
                        } else {
                            // remove the list to the tail end of the queue ... 
                            _crawlLists.removeElement(currentList);
                            _crawlLists.addTail(currentList);
                            if (_crawlLists.getHead() == currentList) {
                                if (Environment.detailLogEnabled())
                                    LOG.info("FeedQueue Detected HighPriority List:" + currentList.getListName()
                                            + " is IDLE. Exiting.");
                                // and exit loop 
                                exitLoop = true;
                            }
                        }

                        // if there is a next domain ... 
                        if (!exitLoop && getHead() != null) {
                            // setup a transition timer ... 
                            setTimer(getDomainTransitionWaitTime());
                            // and exit loop 
                            exitLoop = true;
                        }

                    } else {

                        StringWriter writer = new StringWriter();

                        writer.write("Invalid Domain State Encountered for List:" + currentList.getListName()
                                + " Disposition:" + currentList.getDisposition() + "\n");
                        try {
                            dumpDetails(writer);
                        } catch (IOException e) {
                            LOG.error(CCStringUtils.stringifyException(e));
                        }
                        LOG.fatal(writer.toString());
                        throw new RuntimeException(writer.toString());
                    }
                }
            }

            if (getHead() == null) {
                setIdle(true);
            }
            _inFeedQueue = false;
        }
    }
}

From source file:com.hichinaschool.flashcards.libanki.sync.BasicHttpSyncer.java

public HttpResponse req(String method, InputStream fobj, int comp, boolean hkey, JSONObject registerData,
        Connection.CancelCallback cancelCallback) {
    File tmpFileBuffer = null;// www.  j  a  v a  2  s.com
    try {
        String bdry = "--" + BOUNDARY;
        StringWriter buf = new StringWriter();
        HashMap<String, Object> vars = new HashMap<String, Object>();
        // compression flag and session key as post vars
        vars.put("c", comp != 0 ? 1 : 0);
        if (hkey) {
            vars.put("k", mHKey);
            vars.put("s", mSKey);
        }
        for (String key : vars.keySet()) {
            buf.write(bdry + "\r\n");
            buf.write(String.format(Locale.US, "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", key,
                    vars.get(key)));
        }
        tmpFileBuffer = File.createTempFile("syncer", ".tmp",
                new File(AnkiDroidApp.getCacheStorageDirectory()));
        FileOutputStream fos = new FileOutputStream(tmpFileBuffer);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        GZIPOutputStream tgt;
        // payload as raw data or json
        if (fobj != null) {
            // header
            buf.write(bdry + "\r\n");
            buf.write(
                    "Content-Disposition: form-data; name=\"data\"; filename=\"data\"\r\nContent-Type: application/octet-stream\r\n\r\n");
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
            // write file into buffer, optionally compressing
            int len;
            BufferedInputStream bfobj = new BufferedInputStream(fobj);
            byte[] chunk = new byte[65536];
            if (comp != 0) {
                tgt = new GZIPOutputStream(bos);
                while ((len = bfobj.read(chunk)) >= 0) {
                    tgt.write(chunk, 0, len);
                }
                tgt.close();
                bos = new BufferedOutputStream(new FileOutputStream(tmpFileBuffer, true));
            } else {
                while ((len = bfobj.read(chunk)) >= 0) {
                    bos.write(chunk, 0, len);
                }
            }
            bos.write(("\r\n" + bdry + "--\r\n").getBytes("UTF-8"));
        } else {
            buf.close();
            bos.write(buf.toString().getBytes("UTF-8"));
        }
        bos.flush();
        bos.close();
        // connection headers
        String url = Collection.SYNC_URL;
        if (method.equals("register")) {
            url = url + "account/signup" + "?username=" + registerData.getString("u") + "&password="
                    + registerData.getString("p");
        } else if (method.startsWith("upgrade")) {
            url = url + method;
        } else {
            url = url + "sync/" + method;
        }
        HttpPost httpPost = new HttpPost(url);
        HttpEntity entity = new ProgressByteEntity(tmpFileBuffer);

        // body
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-type", "multipart/form-data; boundary=" + BOUNDARY);

        // HttpParams
        HttpParams params = new BasicHttpParams();
        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
        params.setParameter(CoreProtocolPNames.USER_AGENT, "AnkiDroid-" + AnkiDroidApp.getPkgVersionName());
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSoTimeout(params, Connection.CONN_TIMEOUT);

        // Registry
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", new EasySSLSocketFactory(), 443));
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, registry);
        if (cancelCallback != null) {
            cancelCallback.setConnectionManager(cm);
        }

        try {
            HttpClient httpClient = new DefaultHttpClient(cm, params);
            return httpClient.execute(httpPost);
        } catch (SSLException e) {
            Log.e(AnkiDroidApp.TAG, "SSLException while building HttpClient", e);
            return null;
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "BasicHttpSyncer.sync: IOException", e);
        return null;
    } catch (JSONException e) {
        throw new RuntimeException(e);
    } finally {
        if (tmpFileBuffer != null && tmpFileBuffer.exists()) {
            tmpFileBuffer.delete();
        }
    }
}

From source file:org.wymiwyg.wrhapi.test.BaseTests.java

private void testSimpleBody(final boolean writeBody) throws Exception {
    final String body = "This is the content of the body";
    WebServer webServer = createServer().startNewWebServer(new Handler() {

        public void handle(Request request, Response response) throws HandlerException {
            log.info("handling testSimpleBody");

            if (writeBody) {
                response.setBody(new MessageBody2Write() {

                    public void writeTo(WritableByteChannel out) throws IOException {
                        out.write(ByteBuffer.wrap(body.getBytes()));
                    }/*  w  w w . j  a v a2  s.  c  o m*/
                });
            } else {
                response.setBody(new MessageBody2Read() {

                    public ReadableByteChannel read() throws IOException {
                        return Channels.newChannel(new ByteArrayInputStream(body.getBytes()));
                    }
                });
            }
        }
    }, serverBinding);

    try {
        URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":"
                + serverBinding.getPort() + "/");
        Reader reader = new InputStreamReader(serverURL.openStream());
        StringWriter stringWriter = new StringWriter();

        for (int ch = reader.read(); ch != -1; ch = reader.read()) {
            stringWriter.write(ch);
        }

        assertEquals(body, stringWriter.toString());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        webServer.stop();
    }
}

From source file:org.wymiwyg.wrhapi.test.BaseTests.java

@Test
public void testEmptyBodyAnHeader() throws Exception {
    final String body = "";
    WebServer webServer = createServer().startNewWebServer(new Handler() {

        public void handle(Request request, final Response response) throws HandlerException {
            log.info("handling testEmptyBody");
            response.setResponseStatus(ResponseStatus.CREATED);
            response.setBody(new MessageBody2Write() {

                public void writeTo(WritableByteChannel out) throws IOException {
                    try {
                        response.setHeader(HeaderName.CONTENT_TYPE, "text/plain");
                    } catch (HandlerException ex) {
                        throw new RuntimeException(ex);
                    }//  www  . j a  v  a 2  s.  c  o m
                    out.write(ByteBuffer.wrap(body.getBytes()));
                    out.close();
                }
            });
        }
    }, serverBinding);

    try {
        URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":"
                + serverBinding.getPort() + "/");
        HttpURLConnection connection = (HttpURLConnection) serverURL.openConnection();
        assertEquals("text/plain", connection.getHeaderField("Content-Type"));
        assertEquals(ResponseStatus.CREATED.getCode(), connection.getResponseCode());
        Reader reader = new InputStreamReader(connection.getInputStream());
        StringWriter stringWriter = new StringWriter();

        for (int ch = reader.read(); ch != -1; ch = reader.read()) {
            stringWriter.write(ch);
        }

        assertEquals(body, stringWriter.toString());
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        webServer.stop();
    }
}

From source file:edu.cornell.mannlib.semservices.service.impl.AgrovocService.java

protected String getWsdl() {
    String result = new String();
    try {// w w w  .j  a  v  a 2  s.com

        StringWriter sw = new StringWriter();
        URL rss = new URL(this.AgrovocWS_address + "?wsdl");

        BufferedReader in = new BufferedReader(new InputStreamReader(rss.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            sw.write(inputLine);
        }
        in.close();

        result = sw.toString();

    } catch (Exception ex) {
        logger.error("error occurred in servlet", ex);
    }
    return result;
}

From source file:cuanto.api.CuantoConnector.java

/**
 * This is here to substitute for HttpMethod.getResponseBodyAsString(), which logs an annoying error message each time
 * it's called./*from  w  w w.  j a va2s  .  c  om*/
 *
 * @param method The method for which to get the response.
 * @return The full response body as a String.
 * @throws IOException If something bad happened.
 */
private String getResponseBodyAsString(HttpMethod method) throws IOException {
    InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream());
    StringWriter writer = new StringWriter();
    int in;
    while ((in = reader.read()) != -1) {
        writer.write(in);
    }
    reader.close();
    return writer.toString();
}

From source file:edu.pitt.csb.stability.StabilitySearch.java

public String toString() {
    StringWriter sw = new StringWriter();
    sw.write("Num Observations: " + numSamps + "\n");
    sw.write("Num Variables: " + numVars + "\n");
    sw.write("Num subsamples: " + numSubs + "\n");
    sw.write("Subsample size: " + subSize + "\n");
    sw.write("Running cpss?: " + cpss + "\n");
    sw.write("Class of graph search: " + gs.getClass().getName() + "\n");
    sw.write("Graph search parameters: " + Arrays.toString(gs.searchParams) + "\n");
    return sw.toString();
}

From source file:org.wymiwyg.wrhapi.test.BaseTests.java

/**
 * test is the returned status code matches the one of the HandlerException
 * thrown, with a HandlerException thrown after a body is set
 * /*from w  w w.j a  v a  2  s .  co  m*/
 * @throws Exception
 *             on failure
 */
/*public void testExceptionStatusCodeAfterBody() throws Exception {
final int statusCode = 302;
WebServer webServer = createServer().startNewWebServer(new Handler() {
public void handle(Request request, Response response)
throws HandlerException {
log.info("handling testStatusCode");
response.setHeader(HeaderName.SERVER, "Ad-Hoc testing server");
response.setBody(new MessageBody2Write() {
public void writeTo(WritableByteChannel out)
throws IOException {
out.write(ByteBuffer.wrap("my body\n\ncontent\n"
.getBytes()));
}
});
        
throw new HandlerException(ResponseStatus.getInstanceByCode(statusCode));
}
}, serverBinding);
        
try {
URL serverURL = new URL("http://"
+ serverBinding.getInetAddress().getHostAddress() + ":"
+ serverBinding.getPort() + "/");
HttpClient client = new HttpClient();
HttpMethod method = new HeadMethod(serverURL.toString());
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
client.executeMethod(method);
// for the handler to be invoked, something of the response has to
// be asked
assertEquals(statusCode, method.getStatusCode());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
webServer.stop();
}
}*/
@Test
public void testRepeated() throws Exception {
    final String body = "This is the content of the body";
    final boolean writeBody = false;
    WebServer webServer = createServer().startNewWebServer(new Handler() {

        public void handle(Request request, Response response) throws HandlerException {
            log.info("handling testSimpleBody");

            if (writeBody) {
                response.setBody(new MessageBody2Write() {

                    public void writeTo(WritableByteChannel out) throws IOException {
                        out.write(ByteBuffer.wrap(body.getBytes()));
                    }
                });
            } else {
                response.setBody(new MessageBody2Read() {

                    public ReadableByteChannel read() throws IOException {
                        return Channels.newChannel(new ByteArrayInputStream(body.getBytes()));
                    }
                });
            }
        }
    }, serverBinding);

    TimeLogger tl = new TimeLogger();
    for (int r = 0; r < 10000; r++) {
        tl.startSection("iteration");
        URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":"
                + serverBinding.getPort() + "/");
        Reader reader = new InputStreamReader(serverURL.openStream());
        StringWriter stringWriter = new StringWriter();

        for (int ch = reader.read(); ch != -1; ch = reader.read()) {
            stringWriter.write(ch);
        }

        assertEquals(body, stringWriter.toString());
        tl.endSection();
    }

    webServer.stop();

    tl.writeReport(new PrintWriter(System.out));
    System.out.println("Using SummaryReportWriter:");
    tl.setReportWriter(new SummaryReportWriter());
    tl.writeReport(new PrintWriter(System.out));
}

From source file:org.jzkit.a2j.codec.runtime.BERInputStream.java

public byte[] any_codec(Object instance, boolean is_constructed) throws java.io.IOException {
    byte[] data = null;

    if ((next_length > 0) && (next_is_constructed)) {
        // debug("definite length encoding of octetstring ("+next_length+")");
        data = new byte[next_length];
        int bytes_left_to_read = next_length;
        int offset = 0;

        // We may need to call read repeatedly until we have all the data.
        while (bytes_left_to_read > 0) {
            int bytes_read = read(data, offset, bytes_left_to_read);
            bytes_left_to_read -= bytes_read;
            offset += bytes_read;/*w  w w .  j  a va  2  s.  c  o m*/
            // debug("Read "+bytes_read+" of "+next_length+" leaving "+bytes_left_to_read+" Next bytes will be at "+offset);
        }
    } else if (next_length == 0) {
        // Indefinite length encoding
        // debug("Indefinite length encoding of any data....");
        StringWriter w = new StringWriter();

        byte current_octet = (byte) read();
        byte next_octet = (byte) read();

        while ((current_octet != 0) && (next_octet != 0)) {
            w.write(current_octet);

            current_octet = next_octet;
            next_octet = (byte) read();
        }

        data = w.toString().getBytes();
    } else
        throw new java.io.IOException("Problem decoding any");

    // debug("any returns ... length="+next_length);

    return data;
}