Writes a byte to the OutputStream - Java java.io

Java examples for java.io:OutputStream

Description

Writes a byte to the OutputStream

Demo Code


//package com.java2s;

import java.io.IOException;

import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Main {
    /**/* w ww . j a  v a 2  s . c  o m*/
     * Writes a byte to the stream
     * @param out The output stream
     * @param value The byte to write.  Will be written as a .NET byte.
     * @throws IOException If an IO error occurs
     */
    public static void writeByte(final OutputStream out, byte value)
            throws IOException {
        byte[] buffer = new byte[1];
        ByteBuffer bb = ByteBuffer.wrap(buffer);

        //Switch the byte ordering to little endian, which is what .NET uses
        bb.order(ByteOrder.LITTLE_ENDIAN);
        bb.position(0);

        bb.put(value); //Write the byte to the buffer

        out.write(buffer); //Write the buffer to the stream
    }
}

Related Tutorials