Here you can find the source of encodeVarUInt64(long unsignedValue, OutputStream outputStream)
Parameter | Description |
---|---|
unsignedValue | the value to encode, interpreted as unsigned |
outputStream | the output stream |
static int encodeVarUInt64(long 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 www.j ava 2 s. c o m*/ * Encodes an unsigned uint64 value (represented as signed int64 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-10) */ static int encodeVarUInt64(long 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; // byte 4 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 6; // byte 5 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 7; // byte 6 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 8; // byte 7 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 9; // byte 8 if (unsignedValue >= 0x80) { outputStream.write((byte) (unsignedValue | 0x80)); unsignedValue >>>= 7; length = 10; } } } } } } } } } // last byte outputStream.write((byte) unsignedValue); return length; } }