Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

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

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:Main.java

public static void writeSettings(String file, Object... objs) throws IOException {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1024));
    try {// www  .ja va2 s . c o m
        out.writeInt(objs.length);
        for (Object obj : objs) {
            char cl;
            if (obj instanceof Byte) {
                cl = 'Y';
            } else {
                cl = obj.getClass().getSimpleName().charAt(0);
            }

            out.writeChar(cl);
            if (obj instanceof String) {
                out.writeUTF((String) obj);
            } else if (obj instanceof Float) {
                out.writeFloat((Float) obj);
            } else if (obj instanceof Double) {
                out.writeDouble((Double) obj);
            } else if (obj instanceof Integer) {
                out.writeInt((Integer) obj);
            } else if (obj instanceof Long) {
                out.writeLong((Long) obj);
            } else if (obj instanceof Boolean) {
                out.writeBoolean((Boolean) obj);
            } else {
                throw new IllegalStateException("Unsupported type");
            }
        }
    } finally {
        out.close();
    }
}

From source file:WriteBinaryFile.java

private static DataOutputStream openOutputStream(String name) throws Exception {
    DataOutputStream out = null;//w  w  w .ja  v a 2s.  co m
    File file = new File(name);
    out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
    return out;
}

From source file:Main.java

public static String executePost(String url, String parameters) throws IOException {

    URL request = new URL(url);
    HttpURLConnection connection = (HttpURLConnection) request.openConnection();
    connection.setDoOutput(true);/*from w ww .j a v  a 2s .co  m*/
    connection.setDoInput(true);
    connection.setInstanceFollowRedirects(false);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("User-Agent", "StripeConnectAndroid");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("charset", "utf-8");
    connection.setRequestProperty("Content-Length", "" + Integer.toString(parameters.getBytes().length));
    connection.setUseCaches(false);

    DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
    wr.writeBytes(parameters);
    wr.flush();
    wr.close();

    String response = streamToString(connection.getInputStream());
    connection.disconnect();
    return response;

}

From source file:Main.java

/**
 * Add wave header at front pcm data./*from  ww  w . ja va  2  s .c  o  m*/
 * 
 * @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();
}

From source file:Main.java

private static byte[] stringToBytes(String string) {
    if (string == null) {
        return null;
    }//from ww  w.  j  av  a 2s  .c o  m
    byte[] buffer = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
        dos.writeUTF(string);
        buffer = baos.toByteArray();
    } catch (IOException ex) {
    } finally {
        try {
            baos.close();
            dos.close();
        } catch (IOException ex1) {
        }
        baos = null;
        dos = null;
    }
    return buffer;
}

From source file:Main.java

public static Process runSuCommandAsync(Context context, String command) throws IOException {
    DataOutputStream fout = new DataOutputStream(context.openFileOutput(SCRIPT_NAME, 0));
    fout.writeBytes(command);//  w w  w.  j  a  va 2s.c o  m
    fout.close();

    String[] args = new String[] { "su", "-c",
            ". " + context.getFilesDir().getAbsolutePath() + "/" + SCRIPT_NAME };
    Process proc = Runtime.getRuntime().exec(args);
    return proc;
}

From source file:bakuposter.gcm.server.POST2GCMessage.java

public static void post(String apiKey, Content content) {

    try {/*from  ww w .  j  a v  a 2 s  . co  m*/
        URL url = new URL("https://android.googleapis.com/gcm/send");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);
        conn.setDoOutput(true);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(content.getRegistration_ids().get(0));
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        mapper.writeValue(wr, content);
        wr.flush();
        wr.close();
        int responseCode = conn.getResponseCode();
        System.out.println("responseCode = " + responseCode);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * The URL looks like a request for a resource, such as a JavaScript or CSS file. Write
 * the given resource to the response output in binary format (needed for images).
 *//* w  w w . j a va 2 s. c  o m*/
public static void readWriteBinaryUtil(String sxURL, String resource, OutputStream outStream)
        throws IOException {

    DataOutputStream outData = null;
    DataInputStream inData = null;
    int byteCnt = 0;
    byte[] buffer = new byte[4096];

    // get full qualified path of class
    System.out.println("RW Base Directory - " + sxURL);

    // remove class and add resource relative path
    sxURL = getResourceURL(sxURL, resource);

    System.out.println("RW Loading - " + sxURL);
    try {
        outData = new DataOutputStream(outStream);
        inData = new DataInputStream(new URL(sxURL).openConnection().getInputStream());

        while ((byteCnt = inData.read(buffer)) != -1) {
            if (outData != null && byteCnt > 0) {
                outData.write(buffer, 0, byteCnt);
            }
        }
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (inData != null) {
                inData.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:MyTask.java

License:asdf

public MyTask(OutputStream dest) {
    out = new DataOutputStream(dest);
}

From source file:Main.java

public static void killProcesses(Context context, int pid, String processName) {

    String cmd = "kill -9 " + pid;
    String Command = "am force-stop " + processName + "\n";
    Process sh = null;//from   ww w  .  j a  v  a 2s  . c o  m
    DataOutputStream os = null;
    try {
        sh = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(sh.getOutputStream());
        os.writeBytes(Command + "\n");
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        sh.waitFor();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // AbLogUtil.d(AbAppUtil.class, "#kill -9 "+pid);
    // L.i(processName);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    String packageName = null;
    try {
        if (processName.indexOf(":") == -1) {
            packageName = processName;
        } else {
            packageName = processName.split(":")[0];
        }

        activityManager.killBackgroundProcesses(packageName);

        //
        Method forceStopPackage = activityManager.getClass().getDeclaredMethod("forceStopPackage",
                String.class);
        forceStopPackage.setAccessible(true);
        forceStopPackage.invoke(activityManager, packageName);
    } catch (Exception e) {
        e.printStackTrace();
    }

}