Java Encode encodeVarUInt64(long unsignedValue, OutputStream outputStream)

Here you can find the source of encodeVarUInt64(long unsignedValue, OutputStream outputStream)

Description

Encodes an unsigned uint64 value (represented as signed int64 value) into a stream.

License

Open Source License

Parameter

Parameter Description
unsignedValue the value to encode, interpreted as unsigned
outputStream the output stream

Return

the number of bytes written to the stream (1-10)

Declaration

static int encodeVarUInt64(long unsignedValue, OutputStream outputStream) throws IOException 

Method Source Code


//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;
    }
}

Related

  1. encodeUInt32(int value, OutputStream out)
  2. encodeUnknownString(String in, OutputStream os)
  3. encodeUTCTime(long time, OutputStream os)
  4. encodeUtf8AsBase64(String data)
  5. encodeVarUInt32(int unsignedValue, OutputStream outputStream)
  6. encodeWikiTag(String s)
  7. encodeXML(String input)
  8. encodeXMLString(String aString, PrintWriter aWriter)