Java OutputStream Write Int writeInt(final OutputStream outputStream, final int integer)

Here you can find the source of writeInt(final OutputStream outputStream, final int integer)

Description

Writes an integer out to an OutputStream.

License

Open Source License

Parameter

Parameter Description
outputStream The OutputStream the integer will be written to
integer The integer to write

Exception

Parameter Description
IOException Thrown if there is a problem writing to the OutputStream

Declaration

public static void writeInt(final OutputStream outputStream, final int integer) throws IOException 

Method Source Code

//package com.java2s;
/**/*from w w  w  .jav a 2  s  .com*/
 * Copyright (c) 2008-2012 Ardor Labs, Inc.
 *
 * This file is part of Ardor3D.
 *
 * Ardor3D is free software: you can redistribute it and/or modify it 
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
 */

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**
     * Writes an integer out to an OutputStream.
     * 
     * @param outputStream
     *            The OutputStream the integer will be written to
     * @param integer
     *            The integer to write
     * @throws IOException
     *             Thrown if there is a problem writing to the OutputStream
     */
    public static void writeInt(final OutputStream outputStream, final int integer) throws IOException {
        final byte[] byteArray = convertToBytes(integer);

        outputStream.write(byteArray);

        return;
    }

    public static byte[] convertToBytes(final short value) {
        final byte[] byteArray = new byte[2];

        byteArray[0] = (byte) (value >> 8);
        byteArray[1] = (byte) value;
        return byteArray;
    }

    public static byte[] convertToBytes(final int integer) {
        final byte[] byteArray = new byte[4];

        byteArray[0] = (byte) (integer >> 24);
        byteArray[1] = (byte) (integer >> 16);
        byteArray[2] = (byte) (integer >> 8);
        byteArray[3] = (byte) integer;
        return byteArray;
    }

    public static byte[] convertToBytes(long n) {
        final byte[] bytes = new byte[8];

        bytes[7] = (byte) (n);
        n >>>= 8;
        bytes[6] = (byte) (n);
        n >>>= 8;
        bytes[5] = (byte) (n);
        n >>>= 8;
        bytes[4] = (byte) (n);
        n >>>= 8;
        bytes[3] = (byte) (n);
        n >>>= 8;
        bytes[2] = (byte) (n);
        n >>>= 8;
        bytes[1] = (byte) (n);
        n >>>= 8;
        bytes[0] = (byte) (n);

        return bytes;
    }

    public static byte[] convertToBytes(final double n) {
        final long bits = Double.doubleToLongBits(n);
        return convertToBytes(bits);
    }

    public static byte[] convertToBytes(final float f) {
        final int temp = Float.floatToIntBits(f);
        return convertToBytes(temp);
    }

    public static byte[] convertToBytes(final boolean b) {
        final byte[] rVal = new byte[1];
        rVal[0] = b ? (byte) 1 : (byte) 0;
        return rVal;
    }
}

Related

  1. writeInt(final int v, final OutputStream out)
  2. writeInt(final int v, final OutputStream out)
  3. writeInt(final int value, final OutputStream os)
  4. writeInt(final OutputStream out, final int v)
  5. writeInt(final OutputStream out, final int value)
  6. writeInt(int data, int length, OutputStream out)
  7. writeInt(int v, OutputStream stream)
  8. writeInt(int v, OutputStream stream)
  9. writeInt(OutputStream buffer, int val)