WriteVInt() public method

Writes an int in a variable-length format. Writes between one and five bytes. Smaller values take fewer bytes. Negative numbers are supported, but should be avoided.

VByte is a variable-length format for positive integers is defined where the high-order bit of each byte indicates whether more bytes remain to be read. The low-order seven bits are appended as increasingly more significant bits in the resulting integer value. Thus values from zero to 127 may be stored in a single byte, values from 128 to 16,383 may be stored in two bytes, and so on.

VByte Encoding Example

Value Byte 1 Byte 2 Byte 3
0 00000000
1 00000001
2 00000010
...
127 01111111
128 10000000 00000001
129 10000001 00000001
130 10000010 00000001
...
16,383 11111111 01111111
16,384 10000000 10000000 00000001
16,385 10000001 10000000 00000001
...

this provides compression while still being efficient to decode.

If there is an I/O error writing to the underlying medium.
public WriteVInt ( int i ) : void
i int Smaller values take fewer bytes. Negative numbers are /// supported, but should be avoided.
return void
Example #1
0
        /// <summary>
        /// Create a new <seealso cref="ForUtil"/> instance and save state into <code>out</code>.
        /// </summary>
        public ForUtil(float acceptableOverheadRatio, DataOutput @out)
        {
            @out.WriteVInt(PackedInts.VERSION_CURRENT);
            EncodedSizes = new int[33];
            Encoders = new PackedInts.Encoder[33];
            Decoders = new PackedInts.Decoder[33];
            Iterations = new int[33];

            for (int bpv = 1; bpv <= 32; ++bpv)
            {
                PackedInts.FormatAndBits formatAndBits = PackedInts.FastestFormatAndBits(Lucene41PostingsFormat.BLOCK_SIZE, bpv, acceptableOverheadRatio);
                Debug.Assert(formatAndBits.format.IsSupported(formatAndBits.bitsPerValue));
                Debug.Assert(formatAndBits.bitsPerValue <= 32);
                EncodedSizes[bpv] = EncodedSize(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Encoders[bpv] = PackedInts.GetEncoder(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Decoders[bpv] = PackedInts.GetDecoder(formatAndBits.format, PackedInts.VERSION_CURRENT, formatAndBits.bitsPerValue);
                Iterations[bpv] = ComputeIterations(Decoders[bpv]);

                @out.WriteVInt(formatAndBits.format.id << 5 | (formatAndBits.bitsPerValue - 1));
            }
        }
 private static void SaveInts(int[] values, int length, DataOutput @out)
 {
     Debug.Assert(length > 0);
     if (length == 1)
     {
         @out.WriteVInt(values[0]);
     }
     else
     {
         bool allEqual = true;
         for (int i = 1; i < length; ++i)
         {
             if (values[i] != values[0])
             {
                 allEqual = false;
                 break;
             }
         }
         if (allEqual)
         {
             @out.WriteVInt(0);
             @out.WriteVInt(values[0]);
         }
         else
         {
             long max = 0;
             for (int i = 0; i < length; ++i)
             {
                 max |= (uint)values[i];
             }
             int bitsRequired = PackedInts.BitsRequired(max);
             @out.WriteVInt(bitsRequired);
             PackedInts.Writer w = PackedInts.GetWriterNoHeader(@out, PackedInts.Format.PACKED, length, bitsRequired, 1);
             for (int i = 0; i < length; ++i)
             {
                 w.Add(values[i]);
             }
             w.Finish();
         }
     }
 }
 public override void Write(DataOutput indexOut, bool absolute)
 {
     if (absolute)
     {
         indexOut.WriteVInt(upto);
         indexOut.WriteVLong(fp);
     }
     else if (fp == lastFP)
     {
         // same block
         Debug.Assert(upto >= lastUpto);
         int uptoDelta = upto - lastUpto;
         indexOut.WriteVInt(uptoDelta << 1 | 1);
     }
     else
     {
         // new block
         indexOut.WriteVInt(upto << 1);
         indexOut.WriteVLong(fp - lastFP);
     }
     lastUpto = upto;
     lastFP = fp;
 }
 public override void EncodeTerm(long[] empty, DataOutput output, FieldInfo fieldInfo, BlockTermState _state,
     bool absolute)
 {
     PulsingTermState state = (PulsingTermState) _state;
     Debug.Debug.Assert((empty.Length == 0);
     this.absolute = this.absolute || absolute;
     if (state.bytes == null)
     {
         _wrappedPostingsWriter.EncodeTerm(longs, buffer, fieldInfo, state.wrappedState, this.absolute);
         for (int i = 0; i < longsSize; i++)
         {
             output.WriteVLong(longs[i]);
         }
         buffer.WriteTo(output);
         buffer.Reset();
         this.absolute = false;
     }
     else
     {
         output.WriteVInt(state.bytes.Length);
         output.WriteBytes(state.bytes, 0, state.bytes.Length);
         this.absolute = this.absolute || absolute;
     }
 }