Example usage for java.io DataOutputStream write

List of usage examples for java.io DataOutputStream write

Introduction

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

Prototype

public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Usage

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

public void setMessage(User user, String deactivate) {

    try {//from   w  ww  . j a  va2  s .c o m
        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.paymaya.sdk.android.common.network.AndroidClient.java

private void write(OutputStream os, byte[] body) throws IOException {
    DataOutputStream out = new DataOutputStream(os);
    out.write(body);
    out.flush();//ww w  .j  a  v a 2s .c o m
    out.close();
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.bincas.BinaryCasWriter.java

private void writeHeader(OutputStream aOS) throws IOException {
    byte[] header = new byte[] { 'D', 'K', 'P', 'r', 'o', '1' };
    DataOutputStream dataOS = new DataOutputStream(aOS);
    dataOS.write(header);
    dataOS.flush();/* www. j  a v a 2 s  . co  m*/
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.FileDownload.java

@Override
public ActionForward execute(final ActionMapping mapping, final ActionForm actionForm,
        final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String oid = request.getParameter("oid");
    final File file = FenixFramework.getDomainObject(oid);
    if (file == null) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();//from ww w.  j  a  v  a2 s  .  com
    } else {
        final Person person = AccessControl.getPerson();
        if (!file.isPrivate() || file.isPersonAllowedToAccess(person)) {
            response.setContentType(file.getContentType());
            response.addHeader("Content-Disposition", "attachment; filename=" + file.getFilename());
            response.setContentLength(file.getSize().intValue());
            final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
            dos.write(file.getContents());
            dos.close();
        } else if (file.isPrivate() && person == null) {
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_UNAUTHORIZED));
            response.getWriter().close();
        } else {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_FORBIDDEN));
            response.getWriter().close();
        }
    }
    return null;
}

From source file:org.mule.transport.comm.protocols.LengthProtocol.java

protected void writeByteArray(OutputStream os, byte[] data) throws IOException {
    // Write the length and then the data.
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeInt(data.length);/*from ww w .  ja  v  a  2 s.co m*/
    dos.write(data);
    dos.flush();
}

From source file:com.bigdata.dastor.db.ReadResponse.java

public void serialize(ReadResponse rm, DataOutputStream dos) throws IOException {
    dos.writeInt(rm.digest().length);/*from   ww w .j a v a 2  s.c  om*/
    dos.write(rm.digest());
    dos.writeBoolean(rm.isDigestQuery());

    if (!rm.isDigestQuery() && rm.row() != null) {
        Row.serializer().serialize(rm.row(), dos);
    }
}

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

/**
 * Creates a GET web request for the current URL.
 * @throws IOException //from w w  w .  j a v  a  2 s  .  c  o  m
 */
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:org.apache.hadoop.streaming.TestStreaming.java

protected void createInput() throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(INPUT_FILE.getAbsoluteFile()));
    out.write(input.getBytes("UTF-8"));
    out.close();//from  www . jav a  2s . c  o  m
}

From source file:com.microsoft.azure.management.TestContainerService.java

private String getSshKey() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(2048);//from w ww . j a v a  2  s.  co  m
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(byteOs);
    dos.writeInt("ssh-rsa".getBytes().length);
    dos.write("ssh-rsa".getBytes());
    dos.writeInt(publicKey.getPublicExponent().toByteArray().length);
    dos.write(publicKey.getPublicExponent().toByteArray());
    dos.writeInt(publicKey.getModulus().toByteArray().length);
    dos.write(publicKey.getModulus().toByteArray());
    String publicKeyEncoded = new String(Base64.encodeBase64(byteOs.toByteArray()));
    return "ssh-rsa " + publicKeyEncoded + " ";
}

From source file:org.apache.hawq.pxf.service.FragmentsResponse.java

/**
 * Serializes a fragments list in JSON, To be used as the result string for
 * HAWQ. An example result is as follows:
 * <code>{"PXFFragments":[{"replicas":
 * ["sdw1.corp.emc.com","sdw3.corp.emc.com","sdw8.corp.emc.com"],
 * "sourceName":"text2.csv", "index":"0","metadata":"&lt;base64 metadata for fragment&gt;",
 * "userData":"&lt;data_specific_to_third_party_fragmenter&gt;"
 * },{"replicas":["sdw2.corp.emc.com","sdw4.corp.emc.com","sdw5.corp.emc.com"
 * ],"sourceName":"text_data.csv","index":"0","metadata":
 * "&lt;base64 metadata for fragment&gt;"
 * ,"userData":"&lt;data_specific_to_third_party_fragmenter&gt;"
 * }]}</code>/*ww w.  j av a 2s .c o  m*/
 */
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
    DataOutputStream dos = new DataOutputStream(output);
    ObjectMapper mapper = new ObjectMapper();

    dos.write("{\"PXFFragments\":[".getBytes());

    String prefix = "";
    for (Fragment fragment : fragments) {
        StringBuilder result = new StringBuilder();
        /* metaData and userData are automatically converted to Base64 */
        result.append(prefix).append(mapper.writeValueAsString(fragment));
        prefix = ",";
        dos.write(result.toString().getBytes());
    }

    dos.write("]}".getBytes());
}