Example usage for java.io DataOutputStream close

List of usage examples for java.io DataOutputStream close

Introduction

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

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:ai.eve.volley.request.JsonRequest.java

@Override
public void getBody(HttpURLConnection connection) {
    try {/*from   w w w  .j a v a2 s .c o m*/
        if (mRequestBody != null) {
            connection.setDoOutput(true);
            connection.addRequestProperty(HTTP.CONTENT_TYPE, getBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(mRequestBody.getBytes(PROTOCOL_CHARSET));
            out.close();
        }
    } catch (UnsupportedEncodingException uee) {
        NetroidLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody,
                PROTOCOL_CHARSET);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.portalblockz.portalbot.urlshorteners.GooGl.java

@Override
public String shorten(String url) {
    StringBuilder response = new StringBuilder();
    try {// w w w  .ja v  a2  s.  c  o m
        URL req = new URL("https://www.googleapis.com/urlshortener/v1/url");

        HttpsURLConnection con = (HttpsURLConnection) req.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes("{\"longUrl\": \"" + url + "\"}");
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject res = new JSONObject(response.toString());
        if (res.optString("id") != null)
            return res.getString("id");

    } catch (Exception ignored) {
        ignored.printStackTrace();
        System.out.print(response.toString());
    }
    return null;
}

From source file:com.microsoft.tfs.client.eclipse.resourcedata.ResourceData.java

/**
 * @return this object serialized to a byte array, or <code>null</code> if
 *         there was a problem serializing
 *//*from  w  ww. j  a va  2s  .  c o m*/
public byte[] toByteArray() {
    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final DataOutputStream dos = new DataOutputStream(os);

        dos.writeUTF(serverItem);
        dos.writeInt(changesetId);

        dos.close();
        return os.toByteArray();
    } catch (final IOException e) {
        log.error("Error serializing", e); //$NON-NLS-1$
        return null;
    }
}

From source file:net.terryyiu.emailservice.providers.AbstractEmailServiceProvider.java

@Override
public boolean send(Email email) throws IOException {
    HttpURLConnection connection = createConnection();

    // Create a Map from an email object, which can be translated to JSON which
    // the specific email service provider understands.
    Map<String, Object> map = getRequestPostData(email);

    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(map);

    DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
    dos.writeBytes(json);/*from w  ww  .jav a  2  s  .  c  o  m*/
    dos.flush();
    dos.close();

    int responseCode = connection.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + getServiceUrl());
    System.out.println("JSON: " + json);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    connection.disconnect();
    return false;
}

From source file:MainFrame.CheckConnection.java

private boolean isOnline() throws MalformedURLException, IOException, Exception {
    String url = "http://www.itstepdeskview.hol.es";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    //add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

    String urlParameters = "apideskviewer.checkStatus={}";

    // Send post request
    con.setDoOutput(true);//from   www .jav a  2 s.com
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    //        System.out.println("\nSending 'POST' request to URL : " + url);
    //        System.out.println("Post parameters : " + urlParameters);
    //        System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

    JSONParser parser = new JSONParser();
    Object parsedResponse = parser.parse(in);

    JSONObject jsonParsedResponse = (JSONObject) parsedResponse;

    String s = (String) jsonParsedResponse.get("response");
    if (s.equals("online")) {
        return true;
    } else {
        return false;
    }
}

From source file:com.uber.hoodie.common.BloomFilter.java

/**
 * Serialize the bloom filter as a string.
 *//*from   ww  w .j  a  va  2 s  . c  o  m*/
public String serializeToString() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
        filter.write(dos);
        byte[] bytes = baos.toByteArray();
        dos.close();
        return DatatypeConverter.printBase64Binary(bytes);
    } catch (IOException e) {
        throw new HoodieIndexException("Could not serialize BloomFilter instance", e);
    }
}

From source file:net.indialend.web.component.GCMComponent.java

public void setMessage(User user, String deactivate) {

    try {/*  w  w w .j a v a2  s  .  com*/
        URL obj = new URL(serviceUrl);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("Authorization", "key=" + API_KEY);

        String urlParameters = "{" + "   \"data\": {" + "     \"deactivate\": \"" + deactivate + "\"," + "   },"
                + "   \"to\": \"" + user.getGcmToken() + "\"" + " }";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(urlParameters.getBytes("UTF-8"));
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + serviceUrl);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.bikfalvi.java.web.WebState.java

/**
 * Creates a GET web request for the current URL.
 * @throws IOException //from   w ww . j  a  v  a  2s .c om
 */
public void execute() throws IOException {
    // If the data is not null.
    if (null != this.data) {
        // Write the data.
        this.connection.setDoOutput(true);
        DataOutputStream stream = new DataOutputStream(this.connection.getOutputStream());
        stream.write(this.data);
        stream.flush();
        stream.close();
    }

    // Execute the request and set the response code.
    int code = this.connection.getResponseCode();

    // Set the response data.
    this.setResponse(code, this.connection.getInputStream(), this.connection.getContentEncoding());

    // Complete the request.
    this.complete();
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.commons.student.ViewStudentApplicationDA.java

public ActionForward downloadDocument(final ActionMapping mapping, final ActionForm form,
        final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final IndividualCandidacyDocumentFile document = getDomainObject(request, "documentOID");
    if (document != null && isAuthorized(document)) {
        response.setContentType(document.getContentType());
        response.addHeader("Content-Disposition", "attachment; filename=\"" + document.getFilename() + "\"");
        response.setContentLength(document.getSize().intValue());
        final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
        dos.write(document.getContent());
        dos.close();
    } else {/*from ww w.ja  va 2  s  . com*/
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();
    }
    return null;
}

From source file:com.androidex.volley.toolbox.HurlStack.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        ProgressListener progressListener = null;
        if (request instanceof ProgressListener) {
            progressListener = (ProgressListener) request;
        }/* ww w.jav  a  2  s . c  om*/
        connection.setDoOutput(true);
        connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());

        if (progressListener != null) {
            int transferredBytes = 0;
            int totalSize = body.length;
            int offset = 0;
            int chunkSize = Math.min(2048, Math.max(totalSize - offset, 0));
            while (chunkSize > 0 && offset + chunkSize <= totalSize) {
                out.write(body, offset, chunkSize);
                transferredBytes += chunkSize;
                progressListener.onProgress(transferredBytes, totalSize);
                offset += chunkSize;
                chunkSize = Math.min(chunkSize, Math.max(totalSize - offset, 0));
            }
        } else {
            out.write(body);
        }

        out.close();
    }
}