Here you can find the source of encodeVarUInt32(int unsignedValue, OutputStream outputStream)
Parameter | Description |
---|---|
unsignedValue | the value to encode, interpreted as unsigned |
outputStream | the output stream |
static int encodeVarUInt32(int unsignedValue, OutputStream outputStream) throws IOException
//package com.java2s; // Licensed under the MIT license. See LICENSE file in the project root for full license information. import java.io.IOException; import java.io.OutputStream; public class Main { /**//from ww w. ja va 2 s. c o m * Encodes an unsigned uint32 value (represented as a signed int32 value) into a stream. * The sign bit of the value is re-interpreted as the high order bit for encoding purposes. * * @param unsignedValue the value to encode, interpreted as unsigned * @param outputStream the output stream * @return the number of bytes written to the stream (1-5) */ static int encodeVarUInt32(int unsignedValue, OutputStream outputStream) throws IOException { int length = 1; // byte 0 (needs a special case to test for negative) if (unsignedValue >= 0x80 || unsignedValue < 0) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 2; // byte 1 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 3; // byte 2 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 4; // byte 3 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 5; } } } } // last byte outputStream.write((byte) unsignedValue); return length; } }