Writes an Int64 to the OutputStream - Java java.io

Java examples for java.io:OutputStream

Description

Writes an Int64 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 {
    /**//from   w w w . j a  v  a  2s .c o m
     * Writes an Int64 to the stream
     * @param out The output stream
     * @param value The long to write.  Will be written as a .NET Int64.
     * @throws IOException If an IO error occurs
     */
    public static void writeInt64(final OutputStream out, long value)
            throws IOException {
        byte[] buffer = new byte[8];
        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.putLong(value); //Write the long to the buffer

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

Related Tutorials