private static void relGet(ByteBuffer b, int start)
 {
     int n = b.Remaining;
     byte v;
     for (int i = start; i < n; i++)
         ck(b, (long)b.Get(), (long)((byte)Ic(i)));
     b.Rewind();
 }
 private static void absGet(ByteBuffer b)
 {
     int n = b.Capacity;
     byte v;
     for (int i = 0; i < n; i++)
         ck(b, (long)b.Get(), (long)((byte)Ic(i)));
     b.Rewind();
 }
        protected override void ParseBody(ByteBuffer buf)
        {
            // 判断搜索是否已经结束
            if (!buf.HasRemaining() || buf.Get() == 0x2D && buf.Get() == 0x31)
            {
                Finished = true;
                return;
            }
            buf.Rewind();
            // 只要还有数据就继续读取下一个friend结构
            Users = new List<UserInfo>();
            while (buf.HasRemaining())
            {
                UserInfo ui = new UserInfo();
                ui.Read(buf);

                // 添加到list
                Users.Add(ui);
            }
        }
        /**
         * Reads GIF image from byte array
         *
         * @param data containing GIF file.
         * @return read status code (0 = no errors)
         */
        public int read(byte[] data)
        {
            init();
            if (data != null)
            {
                //Initiliaze the raw data buffer
                rawData = ByteBuffer.Wrap(data);
                rawData.Rewind();
                rawData.Order(ByteOrder.LittleEndian);
                
                readHeader();
                if (!err())
                {
                    readContents();
                    if (frameCount < 0)
                    {
                        status = STATUS_FORMAT_ERROR;
                    }
                }
            }
            else {
                status = STATUS_OPEN_ERROR;
            }

            return status;
        }
        public static void test(int level, ByteBuffer b, bool direct)
        {
            Show(level, b);

            if (direct != b.IsDirect)
                fail("Wrong direction", b);

            // Gets and puts

            relPut(b);
            relGet(b);
            absGet(b);
            bulkGet(b);

            absPut(b);
            relGet(b);
            absGet(b);
            bulkGet(b);

            bulkPutArray(b);
            relGet(b);

            bulkPutBuffer(b);
            relGet(b);


            // Compact

            relPut(b);
            b.Position = (13);
            b.Compact();
            b.Flip();
            relGet(b, 13);

            // Exceptions

            relPut(b);
            b.Limit = (b.Capacity / 2);
            b.Position = (b.Limit);

            tryCatch(b, typeof(BufferUnderflowException), () =>
            {
                b.Get();
            });

            tryCatch(b, typeof(BufferOverflowException), () =>
            {
                b.Put((byte)42);
            });

            // The index must be non-negative and lesss than the buffer's limit.
            tryCatch(b, typeof(IndexOutOfRangeException), () =>
            {
                b.Get(b.Limit);
            });
            tryCatch(b, typeof(IndexOutOfRangeException), () =>
            {
                b.Get(-1);
            });

            tryCatch(b, typeof(IndexOutOfRangeException), () =>
            {
                b.Put(b.Limit, (byte)42);
            });

            tryCatch(b, typeof(InvalidMarkException), () =>
            {
                b.Position = (0);
                b.Mark();
                b.Compact();
                b.Reset();
            });

            // Values

            b.Clear();
            b.Put((byte)0);
            b.Put(unchecked((byte)-1));
            b.Put((byte)1);
            b.Put(unchecked((byte)sbyte.MaxValue));
            b.Put(unchecked((byte)sbyte.MinValue));

            byte v;
            b.Flip();
            ck(b, b.Get(), 0);
            ck(b, b.Get(), unchecked((byte)-1));
            ck(b, b.Get(), 1);
            ck(b, b.Get(), unchecked((byte)sbyte.MaxValue));
            ck(b, b.Get(), unchecked((byte)sbyte.MinValue));


            // Comparison
            b.Rewind();
            ByteBuffer b2 = Lucene.Net.Support.ByteBuffer.Allocate(b.Capacity);
            b2.Put(b);
            b2.Flip();
            b.Position = (2);
            b2.Position = (2);
            if (!b.Equals(b2))
            {
                for (int i = 2; i < b.Limit; i++)
                {
                    byte x = b.Get(i);
                    byte y = b2.Get(i);
                    if (x != y)
                        output.WriteLine("[" + i + "] " + x + " != " + y);
                }
                fail("Identical buffers not equal", b, b2);
            }
            if (b.CompareTo(b2) != 0)
                fail("Comparison to identical buffer != 0", b, b2);

            b.Limit = (b.Limit + 1);
            b.Position = (b.Limit - 1);
            b.Put((byte)99);
            b.Rewind();
            b2.Rewind();
            if (b.Equals(b2))
                fail("Non-identical buffers equal", b, b2);
            if (b.CompareTo(b2) <= 0)
                fail("Comparison to shorter buffer <= 0", b, b2);
            b.Limit = (b.Limit - 1);

            b.Put(2, (byte)42);
            if (b.equals(b2))
                fail("Non-identical buffers equal", b, b2);
            if (b.CompareTo(b2) <= 0)
                fail("Comparison to lesser buffer <= 0", b, b2);

            // Check equals and compareTo with interesting values
            foreach (byte x in VALUES)
            {
                ByteBuffer xb = Lucene.Net.Support.ByteBuffer.Wrap(new byte[] { x });
                if (xb.CompareTo(xb) != 0)
                {
                    fail("compareTo not reflexive", xb, xb, x, x);
                }
                if (!xb.Equals(xb))
                {
                    fail("equals not reflexive", xb, xb, x, x);
                }
                foreach (byte y in VALUES)
                {
                    ByteBuffer yb = Lucene.Net.Support.ByteBuffer.Wrap(new byte[] { y });
                    if (xb.CompareTo(yb) != -yb.CompareTo(xb))
                    {
                        fail("compareTo not anti-symmetric",
                             xb, yb, x, y);
                    }
                    if ((xb.CompareTo(yb) == 0) != xb.Equals(yb))
                    {
                        fail("compareTo inconsistent with equals",
                             xb, yb, x, y);
                    }
                    // from Byte.compare(x, y)
                    //return x - y;
                    if (xb.CompareTo(yb) != (x - y) /* Byte.Compare(x, y)*/)
                    {
                        fail("Incorrect results for ByteBuffer.compareTo",
                             xb, yb, x, y);
                    }
                    if (xb.equals(yb) != ((x == y) || ((x != x) && (y != y))))
                    {
                        fail("Incorrect results for ByteBuffer.equals",
                             xb, yb, x, y);
                    }
                }
            }

            // Sub, dup

            relPut(b);
            relGet(b.Duplicate());
            b.Position = (13);
            relGet(b.Duplicate(), 13);
            relGet(b.Duplicate().Slice(), 13);
            relGet(b.Slice(), 13);
            relGet(b.Slice().Duplicate(), 13);

            // Slice

            b.Position = (5);
            ByteBuffer sb = b.Slice();
            checkSlice(b, sb);
            b.Position = (0);
            ByteBuffer sb2 = sb.Slice();
            checkSlice(sb, sb2);

            if (!sb.Equals(sb2))
                fail("Sliced slices do not match", sb, sb2);
            if ((sb.HasArray) && (sb.ArrayOffset != sb2.ArrayOffset))
                fail("Array offsets do not match: "
                     + sb.ArrayOffset + " != " + sb2.ArrayOffset, sb, sb2);

            // Views

            b.Clear();
            b.Order = (ByteOrder.BIG_ENDIAN);
            testViews(level + 1, b, direct);

            for (int i = 1; i <= 9; i++)
            {
                b.Position = (i);
                Show(level + 1, b);
                testViews(level + 2, b, direct);
            }

            b.Position=(0);
            b.Order=(ByteOrder.LITTLE_ENDIAN);
            testViews(level + 1, b, direct);

            // Heterogeneous accessors

            b.Order = (ByteOrder.BIG_ENDIAN);
            for (int i = 0; i <= 9; i++)
            {
                b.Position = (i);
                testHet(level + 1, b);
            }
            b.Order = (ByteOrder.LITTLE_ENDIAN);
            b.Position = (3);
            testHet(level + 1, b);

            // Read-only views

            b.Rewind();
            ByteBuffer rb = b.AsReadOnlyBuffer();
            if (!b.equals(rb))
                fail("Buffer not equal to read-only view", b, rb);
            Show(level + 1, rb);

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                relPut(rb);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                absPut(rb);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                bulkPutArray(rb);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                bulkPutBuffer(rb);
            });

            // put(ByteBuffer) should not change source position
            ByteBuffer src = ByteBuffer.Allocate(1);
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.Put(src);
            });
            ck(src, src.Position, 0);

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.Compact();
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutChar((char)1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutChar(0, (char)1);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutShort((short)1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutShort(0, (short)1);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutInt(1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutInt(0, 1);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutLong((long)1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutLong(0, (long)1);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutFloat((float)1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutFloat(0, (float)1);
            });

            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutDouble((double)1);
            });
            tryCatch(b, typeof(ReadOnlyBufferException), () =>
            {
                rb.PutDouble(0, (double)1);
            });


            if (rb.GetType().Name.StartsWith("java.nio.Heap"))
            {

                tryCatch(b, typeof(ReadOnlyBufferException), () =>
                {
                    var x = rb.Array;
                });

                tryCatch(b, typeof(ReadOnlyBufferException), () =>
                {
                    var x = rb.ArrayOffset;
                });

                if (rb.HasArray)
                    fail("Read-only heap buffer's backing array is accessible",
                         rb);

            }

            // Bulk puts from read-only buffers

            b.Clear();
            rb.Rewind();
            b.Put(rb);

            // LUCENENET: AllocateDirect not implemented

            // For byte buffers, test both the direct and non-direct cases
            //ByteBuffer ob
            //    = (b.IsDirect
            //       ? ByteBuffer.Allocate(rb.Capacity)
            //       : ByteBuffer.AllocateDirect(rb.Capacity));
            ByteBuffer ob = ByteBuffer.Allocate(rb.Capacity);
            rb.Rewind();
            ob.Put(rb);


            relPut(b);                       // Required by testViews
        }