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:Main.java

private static HttpURLConnection defineHttpsURLConnection(HttpURLConnection connection, JSONObject jsonObject) {
    try {//from www  .  j  a va  2  s  . com
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        /* Add headers to the request */
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(jsonObject.toString().getBytes(Charset.forName("UTF-8")));
        wr.flush();
        wr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return connection;
}

From source file:Main.java

public static void writeNullableString(DataOutputStream out, String value) throws IOException {
    if (value != null) {
        out.write(1);
        out.writeUTF(value);/*  w ww.j  a v a 2 s.c o m*/
    } else {
        out.write(0);
    }
}

From source file:Main.java

public static byte[] getBodyBytes(String ip, String port, byte[] key, byte[] ips)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);
    if (!isNum(port))
        throw new NumberFormatException("port is not number...");
    byte[] portByte = short2bytes(Integer.parseInt(port));
    dos.write(portByte);/*from   www  . j  a va  2  s.co m*/
    //        dos.writeByte(key.getBytes().length);
    dos.write(key);
    if (ips != null && ips.length > 0 && dos != null) {
        dos.write(ips);
    }
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}

From source file:Main.java

public static String stringFromHttpPost(String urlStr, String body) {
    HttpURLConnection conn;//from   w  ww  .j  a v  a 2  s.  c o m
    try {
        URL e = new URL(urlStr);
        conn = (HttpURLConnection) e.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setInstanceFollowRedirects(true);
        conn.setRequestMethod("POST");
        OutputStream os1 = conn.getOutputStream();
        DataOutputStream out1 = new DataOutputStream(os1);
        out1.write(body.getBytes("UTF-8"));
        out1.flush();
        conn.connect();
        String line;
        BufferedReader reader;
        StringBuffer sb = new StringBuffer();
        if ("gzip".equals(conn.getHeaderField("Content-Encoding"))) {
            reader = new BufferedReader(
                    new InputStreamReader(new GZIPInputStream(conn.getInputStream()), "UTF-8"));
        } else {
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        }
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    } catch (Exception e) {
        e.printStackTrace();
        logError(e.getMessage());
    }
    return null;
}

From source file:Main.java

public static void writeString(final DataOutputStream output, final String value) throws IOException {
    for (int i = 0; i < value.length(); i++) {
        output.write(value.charAt(i));
    }/*from   ww  w  .  java  2 s.c o  m*/
}

From source file:Main.java

public static void writeUTF(DataOutputStream os, String str) throws IOException {
    byte[] data = str.getBytes(CHARSET);
    writeShort(os, (short) data.length);
    os.write(data);
}

From source file:com.baran.file.FileOperator.java

public static String storeBinaryContent(byte[] crypted, String inputFile) {
    String completePath = uniqueNameGenerator(inputFile);
    try {/*  w w w  .j  a va2s .c  om*/
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(completePath));
        dos.write(crypted);
        dos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return completePath;
}

From source file:me.sonarbeserk.lockup.utils.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, String body) throws Exception {
    DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
    writer.write(body.getBytes());
    writer.flush();/*w w w  .j av a2s.c  o m*/
    writer.close();
}

From source file:org.apache.accumulo.server.security.SecurityConstants.java

private static AuthenticationToken makeSystemPassword() {
    int wireVersion = Constants.WIRE_VERSION;
    byte[] inst = HdfsZooInstance.getInstance().getInstanceID().getBytes(Constants.UTF8);
    try {/*from w ww  .j  ava 2 s  .  c o m*/
        confChecksum = getSystemConfigChecksum();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Failed to compute configuration checksum", e);
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream(
            3 * (Integer.SIZE / Byte.SIZE) + inst.length + confChecksum.length);
    DataOutputStream out = new DataOutputStream(bytes);
    try {
        out.write(wireVersion * -1);
        out.write(inst.length);
        out.write(inst);
        out.write(confChecksum.length);
        out.write(confChecksum);
    } catch (IOException e) {
        throw new RuntimeException(e); // this is impossible with
        // ByteArrayOutputStream; crash hard
        // if this happens
    }
    return new PasswordToken(Base64.encodeBase64(bytes.toByteArray()));
}

From source file:Main.java

/**
 * Add wave header at front pcm data.//from   www . ja v  a  2s .  c  om
 * 
 * @param data byte array of pcm data
 * @param channel pcm channel, e.g. 1 or 2
 * @param sampleRate pcm sample rate, e.g. 22050, 44100 etc.
 * @param bits pcm bits, e.g. 8 or 16
 * @return
 */
public static byte[] PCM2Wave(byte[] data, int channel, int sampleRate, int bits) {
    int riffSize = data.length + HEADER_LENGTH;
    int chunk = channel * bits / 8;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
        // Write RIFF header
        dos.write(RIFF_array);
        dos.write(intToByteArray(riffSize));
        dos.write(WAVE_array);

        // Write format header
        dos.write(fmt_array);
        dos.write(intToByteArray(16));
        dos.write(shortToByteArray(1));
        dos.write(shortToByteArray(channel));
        dos.write(intToByteArray(sampleRate));
        dos.write(intToByteArray(sampleRate * chunk));
        dos.write(shortToByteArray(2));
        dos.write(shortToByteArray(bits));

        // Write data section
        dos.write(data_array);
        dos.write(intToByteArray(data.length));
        dos.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return baos.toByteArray();
}