Java Encode encodeUint(OutputStream out, final long value)

Here you can find the source of encodeUint(OutputStream out, final long value)

Description

Unsigned integers are the basis for all other primitive values.

License

Open Source License

Declaration

public static boolean encodeUint(OutputStream out, final long value) throws IOException 

Method Source Code


//package com.java2s;
// license that can be found in the LICENSE file.

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**/*from www .  j a v  a  2 s .c o  m*/
     * Unsigned integers are the basis for all other primitive values. This is a two-state encoding.
     * If the number is less than 128 (0 through 0x7f), its value is written directly. Otherwise the
     * value is written in big-endian byte order preceded by the negated byte length.
     * Returns true iff the value is non-zero.
     */
    public static boolean encodeUint(OutputStream out, final long value) throws IOException {
        if ((value & 0x7f) == value) {
            out.write((byte) value);
            return value != 0;
        }
        int len = 0;
        while (((value >>> (len * 8)) | 0xff) != 0xff) {
            len++;
        }
        len++;
        out.write(-len);
        while (len > 0) {
            len--;
            out.write((byte) (value >>> (len * 8)));
        }
        return true;
    }

    public static boolean encodeUint(OutputStream out, final int value) throws IOException {
        return encodeUint(out, value & 0xffffffffL);
    }

    public static boolean encodeUint(OutputStream out, final short value) throws IOException {
        return encodeUint(out, value & 0xffffL);
    }
}

Related

  1. encodeResult(final Object result)
  2. encodeRGBAsGrayScale(final byte[] raw, final int width, final int height, final int bitsPerPixel, final OutputStream out)
  3. encodeStringBase64(String str)
  4. encodeTagChar(char c, Appendable buffer)
  5. encodeToTargetEncoding(String text, String sourceEncoding, String targetEncoding)
  6. encodeUInt32(int value, OutputStream out)
  7. encodeUnknownString(String in, OutputStream os)
  8. encodeUTCTime(long time, OutputStream os)
  9. encodeUtf8AsBase64(String data)