Writes the integer to the OutputStream as a 7 bit encoded integer - Java java.io

Java examples for java.io:OutputStream

Description

Writes the integer to the OutputStream as a 7 bit encoded integer

Demo Code


//package com.java2s;

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**/*w  w w . j  a v  a  2s. c  o m*/
     * Writes the integer to the stream as a 7 bit encoded integer
     * @param out The output stream
     * @param value The integer to write
     * @throws IOException if an IO error occurs
     */
    public static void write7BitEncodedInt(final OutputStream out, int value)
            throws IOException {
        //Based on the .NET implementation.  See http://referencesource.microsoft.com/#mscorlib/system/io/binarywriter.cs,407

        //Write out an int 7 bits at a time.  The high bit of the byte, when on, tells reader to continue reading more bytes.
        int v = value;

        while (v >= 0x80) {
            out.write(new byte[] { (byte) (v | 0x80) });
            v >>= 7;
        }

        out.write(new byte[] { (byte) v });
    }
}

Related Tutorials