Example usage for java.io DataOutputStream writeChars

List of usage examples for java.io DataOutputStream writeChars

Introduction

In this page you can find the example usage for java.io DataOutputStream writeChars.

Prototype

public final void writeChars(String s) throws IOException 

Source Link

Document

Writes a string to the underlying output stream as a sequence of characters.

Usage

From source file:org.opentox.toxotis.client.http.PostHttpClient.java

/**
 * According to the the configuration of the PostHttpClient, performs a remote POST
 * request to the server identified by the URI provided in the constructor. First,
 * the protected method {@link PostHttpClient#initializeConnection(java.net.URI)
 * initializeConnection(URI)} is invoked and then a DataOutputStream opens to
 * transfer the data to the server.//ww  w  .  j  a  v a  2 s.co m
 *
 * @throws ServiceInvocationException
 *      Encapsulates an IOException which might be thrown due to I/O errors
 *      during the data transaction.
 */
@Override
public void post() throws ServiceInvocationException {
    String query = getParametersAsQuery();
    if ((fileContentToPost != null || inputStream != null) && query != null && !query.isEmpty()) {
        postMultiPart();
    } else {
        connect(getUri().toURI());
        DataOutputStream wr;
        try {
            getPostLock().lock(); // LOCK
            wr = new DataOutputStream(getConnection().getOutputStream());

            if (query != null && !query.isEmpty()) {
                wr.writeBytes(getParametersAsQuery());// POST the parameters
            } else if (model != null) {
                model.write(wr);
            } else if (staxComponent != null) {
                staxComponent.writeRdf(wr);
            } else if (stringToPost != null) {
                wr.writeChars(stringToPost);
            } else if (bytesToPost != null) {
                wr.writeBytes(bytesToPost);
            } else if (fileContentToPost != null || inputStream != null) {
                // Post from file (binary data) or from other input stream
                InputStream is = null;
                InputStreamReader isr = null;
                BufferedReader br = null;
                boolean doCloseInputStream = false;
                // choose input stream (used-defined or from file)
                if (fileContentToPost != null) {
                    is = new FileInputStream(fileContentToPost);
                    doCloseInputStream = true;
                } else {
                    is = inputStream;
                }
                try {
                    isr = new InputStreamReader(is);
                    br = new BufferedReader(isr);
                    String line;
                    while ((line = br.readLine()) != null) {
                        wr.writeBytes(line);
                        wr.writeChars("\n");
                    }
                } catch (IOException ex) {
                    throw new ConnectionException("Unable to post data to the remote service at '" + getUri()
                            + "' - The connection dropped " + "unexpectidly while POSTing.", ex);
                } finally {
                    Throwable thr = null;
                    if (br != null) {
                        try {
                            br.close();
                        } catch (final IOException ex) {
                            thr = ex;
                        }
                    }
                    if (isr != null) {
                        try {
                            isr.close();
                        } catch (final IOException ex) {
                            thr = ex;
                        }
                    }
                    if (doCloseInputStream) {
                        try {
                            is.close();
                        } catch (final IOException ex) {
                            thr = ex;
                        }
                    }
                    if (thr != null) {
                        ConnectionException connExc = new ConnectionException("Stream could not close", thr);
                        connExc.setActor(getUri() != null ? getUri().toString() : "N/A");
                        //TODO Why is the exception not thrown here?
                    }
                }
            }
            wr.flush();
            wr.close();
        } catch (final IOException ex) {
            ConnectionException postException = new ConnectionException(
                    "Exception caught while posting the parameters to the " + "remote web service located at '"
                            + getUri() + "'",
                    ex);
            postException.setActor(getUri() != null ? getUri().toString() : "N/A");
            throw postException;
        } finally {
            getPostLock().unlock(); // UNLOCK
        }
    }
}

From source file:org.opentox.toxotis.client.https.PostHttpsClient.java

@Override
public void post() throws ServiceInvocationException {
    String query = getParametersAsQuery();
    if (fileContentToPost != null && query != null && !query.isEmpty()) {
        postMultiPart();//  w w w .j a  va  2  s  . co  m
    } else {

        connect(getUri().toURI());
        DataOutputStream wr;
        try {
            getPostLock().lock(); // LOCK
            wr = new DataOutputStream(getConnection().getOutputStream());

            if (query != null && !query.isEmpty()) {
                wr.writeBytes(getParametersAsQuery());// POST the parameters
            } else if (model != null) {
                model.write(wr);
            } else if (staxComponent != null) {
                staxComponent.writeRdf(wr);
            } else if (postableString != null) {
                wr.writeChars(postableString);
            } else if (postableBytes != null) {
                wr.writeBytes(postableBytes);
            } else if (fileContentToPost != null) {
                FileReader fr = null;
                BufferedReader br = null;
                try {
                    fr = new FileReader(fileContentToPost);
                    br = new BufferedReader(fr);
                    String line;
                    while ((line = br.readLine()) != null) {
                        wr.writeBytes(line);
                        wr.writeChars("\n");
                    }
                } catch (IOException ex) {
                    throw new ConnectionException("Unable to post data to the remote service at '" + getUri()
                            + "' - The connection dropped " + "unexpectidly while POSTing.", ex);
                } finally {
                    Throwable thr = null;
                    if (br != null) {
                        try {
                            br.close();
                        } catch (final IOException ex) {
                            thr = ex;
                        }
                    }
                    if (fr != null) {
                        try {
                            fr.close();
                        } catch (final IOException ex) {
                            thr = ex;
                        }
                    }
                    if (thr != null) {
                        ConnectionException connExc = new ConnectionException("Stream could not close", thr);
                        connExc.setActor(getUri() != null ? getUri().toString() : "N/A");
                    }
                }
            }
            wr.flush();
            wr.close();
        } catch (final IOException ex) {
            ConnectionException postException = new ConnectionException(
                    "Exception caught while posting the parameters to the " + "remote web service located at '"
                            + getUri() + "'",
                    ex);
            postException.setActor(getUri() != null ? getUri().toString() : "N/A");
            throw postException;
        } finally {
            getPostLock().unlock(); // UNLOCK
        }
    }
}

From source file:tachyon.io.Utils.java

public static void writeString(String str, DataOutputStream os) throws IOException {
    if (str == null) {
        os.writeInt(-1);/*from   w w w.j a  va2s.  c o  m*/
    } else {
        os.writeInt(str.length());
        os.writeChars(str);
    }
}