Java OutputStream Write Byte Array writeBytes(int[] data, OutputStream out)

Here you can find the source of writeBytes(int[] data, OutputStream out)

Description

Writes data from the integer array to disk as raw bytes.

License

Open Source License

Parameter

Parameter Description
data The integer array to write to disk.
out The output stream where the data should be written.

Exception

Parameter Description
IOException an exception

Declaration

public static void writeBytes(int[] data, OutputStream out) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**//from  www  .ja  v  a2 s  .  c  o  m
     * Writes data from the integer array to disk as raw bytes, overwriting the old file if present.
     * 
     * @param data The integer array to write to disk.
     * @param filename The filename where the data should be written.
     * @throws IOException
     * @return the FileOutputStream on which the bytes were written
     */
    public static FileOutputStream writeBytes(int[] data, String filename) throws IOException {
        FileOutputStream out = new FileOutputStream(filename, false);
        writeBytes(data, out);
        return out;
    }

    /**
     * Writes data from the integer array to disk as raw bytes.
     * 
     * @param data The integer array to write to disk.
     * @param out The output stream where the data should be written.
     * @throws IOException
     */
    public static void writeBytes(int[] data, OutputStream out) throws IOException {

        byte[] b = new byte[4];

        for (int word : data) {
            b[0] = (byte) ((word >>> 24) & 0xFF);
            b[1] = (byte) ((word >>> 16) & 0xFF);
            b[2] = (byte) ((word >>> 8) & 0xFF);
            b[3] = (byte) ((word >>> 0) & 0xFF);

            out.write(b);
        }
    }
}

Related

  1. writeBytes(byte[] data, OutputStream out)
  2. writeBytes(byte[] data, OutputStream out)
  3. writeBytes(byte[] data, OutputStream stream)
  4. writeBytes(OutputStream os, byte[] b)
  5. writeBytes(OutputStream os, byte[] b)
  6. writeBytes(OutputStream out, byte[] data)
  7. writeBytes(OutputStream output, Object value)