Example usage for java.io DataOutputStream writeBytes

List of usage examples for java.io DataOutputStream writeBytes

Introduction

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

Prototype

public final void writeBytes(String s) throws IOException 

Source Link

Document

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

Usage

From source file:br.org.indt.ndg.servlets.DownloadClient.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    log.info("Started processing stream from " + request.getRemoteAddr());
    log.info("User: " + request.getParameter("user"));

    InputStreamReader dis = new InputStreamReader(request.getInputStream(), "UTF-8");
    DataOutputStream dos = new DataOutputStream(response.getOutputStream());

    if (dis != null) {
        BufferedReader reader = new BufferedReader(dis);
        StringBuffer tempBuffer = new StringBuffer();
        String line = null;/*w w  w  .  j a v  a  2s .c  o m*/

        if (validateUser(request.getParameter("user"), request.getParameter("psw"))) {
            while ((line = reader.readLine()) != null) {
                tempBuffer.append(line + '\n');
                log.info(line);
            }

            try {
                msmBD.postSurvey(request.getParameter("user"), tempBuffer, createTransactionLogVO(request),
                        false);
                dos.writeBytes(SUCCESS);
            } catch (MSMApplicationException e) {
                dos.writeBytes(FAILURE);
                e.printStackTrace();
            } catch (MSMSystemException e) {
                dos.writeBytes(FAILURE);
                e.printStackTrace();
            }
        } else {
            dos.writeBytes(USERINVALID);
            log.info("User invalid, returning " + USERINVALID + " to " + request.getRemoteAddr());
        }

        reader.close();
        dos.close();
    } else {
        dos.writeBytes(FAILURE);
        log.info("Failed processing stream from " + request.getRemoteAddr());
    }
}

From source file:org.openestate.is24.restapi.DefaultClient.java

@Override
protected Response sendVideoUploadRequest(URL url, RequestMethod method, String auth, InputStream input,
        String fileName, long fileSize) throws IOException, OAuthException {
    if (method == null)
        method = RequestMethod.POST;/*from  w  w  w.ja va 2 s.  c o  m*/
    if (!RequestMethod.POST.equals(method) && !RequestMethod.PUT.equals(method))
        throw new IllegalArgumentException("Invalid request method!");
    auth = StringUtils.trimToNull(auth);

    final String twoHyphens = "--";
    final String lineEnd = "\r\n";
    final String boundary = "*****" + RandomStringUtils.random(25, true, true) + "*****";

    HttpURLConnection connection = null;
    DataOutputStream output = null;
    try {
        // create connection
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method.name());
        connection.setRequestProperty("MIME-Version", "1.0");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        connection.setRequestProperty("Content-Language", "en-US");
        connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
        connection.setRequestProperty("Connection", "close");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        //getAuthConsumer().sign( connection );

        output = (auth != null || input != null) ? new DataOutputStream(connection.getOutputStream()) : null;

        // send auth part
        if (auth != null && output != null) {
            output.writeBytes(twoHyphens + boundary + lineEnd);
            output.writeBytes("Content-Type: text/plain; charset=" + getEncoding() + lineEnd);
            output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
            output.writeBytes("Content-Disposition: form-data; name=\"auth\"" + lineEnd);
            output.writeBytes(lineEnd);
            output.writeBytes(auth);
            output.writeBytes(lineEnd);
            output.flush();
        }

        // send file part
        if (input != null && output != null) {
            fileName = StringUtils.trimToNull(fileName);
            if (fileName == null)
                fileName = "upload.bin";

            output.writeBytes(twoHyphens + boundary + lineEnd);
            output.writeBytes("Content-Type: application/octet-stream; name=\"" + fileName + "\"" + lineEnd);
            output.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
            output.writeBytes("Content-Length: " + String.valueOf(fileSize) + lineEnd);
            output.writeBytes("Content-Disposition: form-data; name=\"videofile\"; filename=\"" + fileName
                    + "\"" + lineEnd);
            output.writeBytes(lineEnd);

            byte[] buffer = new byte[4096];
            int i = input.read(buffer, 0, buffer.length);
            while (i > 0) {
                output.write(buffer, 0, i);
                i = input.read(buffer, 0, buffer.length);
            }
            output.writeBytes(lineEnd);
        }

        if (output != null) {
            output.writeBytes(twoHyphens + boundary + lineEnd);
        }

        if (output != null)
            output.flush();
        connection.connect();

        // read response into string
        return createResponse(connection);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.close(connection);
    }
}

From source file:com.intel.xdk.device.Device.java

public void getRemoteData(String requestUrl, String requestMethod, String requestBody,
        CallbackContext callbackContext) {
    try {/*from   w  w  w  .j a  v a  2s  .c o m*/
        URL url = new URL(requestUrl);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod(requestMethod);

        //Write requestBody
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(requestBody);
        outputStream.flush();
        outputStream.close();

        //Get response code and response message
        int responseCode = connection.getResponseCode();
        String responseMessage = connection.getResponseMessage();

        //Get response Message
        DataInputStream inputStream = new DataInputStream(connection.getInputStream());
        if (responseCode == 200) {
            String temp;
            String responseBody = "";
            while ((temp = inputStream.readLine()) != null) {
                responseBody += temp;
            }
            callbackContext.success(responseBody);
        } else {
            callbackContext.error("Fail to get the response, response code: " + responseCode
                    + ", response message: " + responseMessage);
        }

        inputStream.close();
    } catch (IOException e) {
        Log.d("request", e.getMessage());
    }
}

From source file:com.intel.xdk.device.Device.java

public void getRemoteDataWithID(String requestUrl, String requestMethod, String requestBody, int uuid,
        String successCallback, String errorCallback) {
    try {//from   w  ww.  j  a  v  a2 s  .  c  om
        URL url = new URL(requestUrl);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod(requestMethod);

        //Write requestBody
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(requestBody);
        outputStream.writeBytes("&uuid=" + uuid);
        outputStream.flush();
        outputStream.close();

        //Get response code and response message
        int responseCode = connection.getResponseCode();
        String responseMessage = connection.getResponseMessage();

        //Get response Message
        DataInputStream inputStream = new DataInputStream(connection.getInputStream());
        if (responseCode == 200) {
            String temp;
            String responseBody = "";
            while ((temp = inputStream.readLine()) != null) {
                responseBody += temp;
            }
            //callbackContext.success(responseBody);
            String js = "javascript:" + successCallback + "(" + uuid + ", '" + responseBody + "');";
            injectJS(js);
        } else {
            //callbackContext.error("Fail to get the response, response code: " + responseCode + ", response message: " + responseMessage);
            String js = "javascript:" + errorCallback + "(" + uuid + ", '" + "Fail to get the response" + "');";
            injectJS(js);
        }

        inputStream.close();
    } catch (IOException e) {
        Log.d("request", e.getMessage());
    }
}

From source file:com.intel.xdk.device.Device.java

public void getRemoteData(String requestUrl, String requestMethod, String requestBody, String successCallback,
        String errorCallback) {//from  w w  w . j  ava2  s .  c o m
    Log.d("getRemoteData", "url: " + requestUrl + ", method: " + requestMethod + ", body: " + requestBody);

    try {
        URL url = new URL(requestUrl);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod(requestMethod);

        //Write requestBody
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(requestBody);
        outputStream.flush();
        outputStream.close();

        //Get response code and response message
        int responseCode = connection.getResponseCode();
        String responseMessage = connection.getResponseMessage();

        //Get response Message
        DataInputStream inputStream = new DataInputStream(connection.getInputStream());
        if (responseCode == 200) {
            String temp;
            String responseBody = "";
            while ((temp = inputStream.readLine()) != null) {
                responseBody += temp;
            }
            //callbackContext.success(responseBody);
            String js = "javascript:" + successCallback + "('" + responseBody + "');";
            injectJS(js);
        } else {
            //callbackContext.error("Fail to get the response, response code: " + responseCode + ", response message: " + responseMessage);
            String js = "javascript:" + errorCallback + "(" + "'response code :" + responseCode + "');";
            injectJS(js);
        }

        inputStream.close();
    } catch (IOException e) {
        Log.d("request", e.getMessage());
    }
}

From source file:com.intel.xdk.device.Device.java

public void getRemoteDataWithID(String requestUrl, String requestMethod, String requestBody, int uuid,
        CallbackContext callbackContext) {
    try {/*from  w  w w .j av  a  2s .  c om*/
        URL url = new URL(requestUrl);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod(requestMethod);

        //Write requestBody
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(requestBody);
        outputStream.writeBytes("&uuid=" + uuid);
        outputStream.flush();
        outputStream.close();

        //Get response code and response message
        int responseCode = connection.getResponseCode();
        String responseMessage = connection.getResponseMessage();

        //Get response Message
        DataInputStream inputStream = new DataInputStream(connection.getInputStream());
        if (responseCode == 200) {
            String temp;
            String responseBody = "";
            while ((temp = inputStream.readLine()) != null) {
                responseBody += temp;
            }
            callbackContext.success(uuid + ", " + responseBody);
            //String js = "javascript:" + successCallback + "(" + uuid + ", '" + responseBody + "');";
            //injectJS(js);
        } else {
            callbackContext.error(uuid + ", Fail to get the response, response code: " + responseCode
                    + ", response message: " + responseMessage);
            //String js = "javascript:" + errorCallback + "(" + uuid + ", '" + "Fail to get the response" + "');";
            //injectJS(js);
        }

        inputStream.close();
    } catch (IOException e) {
        Log.d("request", e.getMessage());
    }
}

From source file:org.gatein.sso.plugin.RestCallbackCaller.java

private HttpResponseContext sendHttpRequest(String url, String urlParameters) throws IOException {
    Reader reader = null;/*  ww  w  .  j av  a 2  s  .  c  o  m*/
    DataOutputStream wr = null;
    StringBuilder result = new StringBuilder();

    try {
        HttpURLConnection connection;

        if (isPostHttpMethod) {
            URL tempURL = new URL(url);
            connection = (HttpURLConnection) tempURL.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
        } else {
            if (urlParameters != null) {
                url = url + "?" + urlParameters;
            }

            URL tempURL = new URL(url);
            connection = (HttpURLConnection) tempURL.openConnection();
        }

        connection.setUseCaches(false);
        connection.setDoInput(true);

        if (isPostHttpMethod) {
            connection.setDoOutput(true);

            //Send request
            wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
        }

        int statusCode = connection.getResponseCode();

        try {
            reader = new InputStreamReader(connection.getInputStream());
        } catch (IOException ioe) {
            reader = new InputStreamReader(connection.getErrorStream());
        }

        char[] buffer = new char[50];
        int nrOfChars;
        while ((nrOfChars = reader.read(buffer)) != -1) {
            result.append(buffer, 0, nrOfChars);
        }

        String response = result.toString();
        return new HttpResponseContext(statusCode, response);
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (wr != null) {
            wr.close();
        }
    }
}

From source file:org.apache.hadoop.dfs.Balancer.java

private OutputStream checkAndMarkRunningBalancer() throws IOException {
    try {/*from  ww w .ja v  a2s.c o  m*/
        DataOutputStream out = fs.create(BALANCER_ID_PATH);
        out.writeBytes(InetAddress.getLocalHost().getHostName());
        out.flush();
        return out;
    } catch (RemoteException e) {
        if (AlreadyBeingCreatedException.class.getName().equals(e.getClassName())) {
            return null;
        } else {
            throw e;
        }
    }
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static void appendToFile(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    if (!isNull(ArgList) && !isUndefined(ArgList)) {
        try {//from ww w.  j a  v  a  2  s .c  o  m
            FileOutputStream file = new FileOutputStream((String) ArgList[0], true);
            DataOutputStream out = new DataOutputStream(file);
            out.writeBytes((String) ArgList[1]);
            out.flush();
            out.close();
        } catch (Exception er) {
            throw new RuntimeException(er.toString());
        }
    } else {
        throw new RuntimeException("The function call appendToFile requires arguments.");
    }
}