public override long get()
 {
     if (_position == _limit)
     {
         throw new java.nio.BufferUnderflowException();
     }
     return(byteBuffer.getLong(_position++ *libcore.io.SizeOf.LONG));
 }
Beispiel #2
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual Chunk readChunk()
        {
            java.nio.ByteBuffer chunkHeadBuffer = this.readBytes(ArangoDBConstants
                                                                 .CHUNK_MIN_HEADER_SIZE);
            int  length    = chunkHeadBuffer.getInt();
            int  chunkX    = chunkHeadBuffer.getInt();
            long messageId = chunkHeadBuffer.getLong();
            long messageLength;
            int  contentLength;

            if ((1 == (chunkX & unchecked ((int)0x1))) && (chunkX >> 1 > 1))
            {
                messageLength = this.readBytes(ArangoDBConstants.LONG_BYTES).getLong
                                    ();
                contentLength = length - ArangoDBConstants.CHUNK_MAX_HEADER_SIZE;
            }
            else
            {
                messageLength = -1L;
                contentLength = length - ArangoDBConstants.CHUNK_MIN_HEADER_SIZE;
            }
            Chunk chunk = new Chunk
                              (messageId, chunkX, messageLength, 0, contentLength);

            if (LOGGER.isDebugEnabled())
            {
                LOGGER.debug(string.format("Received chunk %s:%s from message %s", chunk.getChunk
                                               (), chunk.isFirstChunk() ? 1 : 0, chunk.getMessageId()));
            }
            return(chunk);
        }
        /// <exception cref="System.IO.IOException"/>
        public int Write(java.nio.ByteBuffer inBuf)
        {
            int length = inBuf.remaining();

            java.nio.ByteBuffer @out       = this.inner.GetWriteBuffer();
            java.nio.ByteBuffer slowBuffer = java.nio.ByteBuffer.allocate(20);
            int inPtr = inBuf.position();
            int inEnd = inPtr + length;

            while (inPtr < inEnd)
            {
                if (@out.remaining() < 10)
                {
                    //# Oops, we're out of space. We need at least 10
                    //# bytes for the fast path, since we don't
                    //# bounds-check on every byte.
                    if (@out == slowBuffer)
                    {
                        int oldLimit = @out.limit();
                        @out.limit(@out.position());
                        @out.rewind();
                        this.inner.Write(@out);
                        @out.limit(oldLimit);
                    }
                    @out = slowBuffer;
                    @out.rewind();
                }
                int tagPos = @out.position();
                @out.position(tagPos + 1);
                byte curByte;
                curByte = inBuf.get(inPtr);
                byte bit0 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit0 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit1 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit1 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit2 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit2 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit3 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit3 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit4 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit4 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit5 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit5 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit6 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit6 - 1);
                inPtr  += 1;
                curByte = inBuf.get(inPtr);
                byte bit7 = (curByte != 0)? unchecked ((byte)1) : unchecked ((byte)0);
                @out.put(curByte);
                @out.position(@out.position() + bit7 - 1);
                inPtr += 1;
                byte tag = unchecked ((byte)((bit0 << 0) | (bit1 << 1) | (bit2 << 2) | (bit3 << 3)
                                             | (bit4 << 4) | (bit5 << 5) | (bit6 << 6) | (bit7 << 7)));
                @out.put(tagPos, tag);
                if (tag == 0)
                {
                    //# An all-zero word is followed by a count of
                    //# consecutive zero words (not including the first
                    //# one).
                    int runStart = inPtr;
                    int limit    = inEnd;
                    if (limit - inPtr > 255 * 8)
                    {
                        limit = inPtr + 255 * 8;
                    }
                    while (inPtr < limit && inBuf.getLong(inPtr) == 0)
                    {
                        inPtr += 8;
                    }
                    @out.put(unchecked ((byte)((inPtr - runStart) / 8)));
                }
                else if (tag == unchecked ((byte)unchecked ((int)(0xff))))
                {
                    //# An all-nonzero word is followed by a count of
                    //# consecutive uncompressed words, followed by the
                    //# uncompressed words themselves.
                    //# Count the number of consecutive words in the input
                    //# which have no more than a single zero-byte. We look
                    //# for at least two zeros because that's the point
                    //# where our compression scheme becomes a net win.
                    int runStart = inPtr;
                    int limit    = inEnd;
                    if (limit - inPtr > 255 * 8)
                    {
                        limit = inPtr + 255 * 8;
                    }
                    while (inPtr < limit)
                    {
                        byte c = 0;
                        for (int ii = 0; ii < 8; ++ii)
                        {
                            c     += (inBuf.get(inPtr) == 0 ? (byte)1 : (byte)0);
                            inPtr += 1;
                        }
                        if (c >= 2)
                        {
                            //# Un-read the word with multiple zeros, since
                            //# we'll want to compress that one.
                            inPtr -= 8;
                            break;
                        }
                    }
                    int count = inPtr - runStart;
                    @out.put(unchecked ((byte)(count / 8)));
                    if (count <= @out.remaining())
                    {
                        //# There's enough space to memcpy.
                        inBuf.position(runStart);
                        java.nio.ByteBuffer slice = inBuf.slice();
                        slice.limit(count);
                        @out.put(slice);
                    }
                    else
                    {
                        //# Input overruns the output buffer. We'll give it
                        //# to the output stream in one chunk and let it
                        //# decide what to do.
                        if (@out == slowBuffer)
                        {
                            int oldLimit = @out.limit();
                            @out.limit(@out.position());
                            @out.rewind();
                            this.inner.Write(@out);
                            @out.limit(oldLimit);
                        }
                        inBuf.position(runStart);
                        java.nio.ByteBuffer slice = inBuf.slice();
                        slice.limit(count);
                        while (slice.hasRemaining())
                        {
                            this.inner.Write(slice);
                        }
                        @out = this.inner.GetWriteBuffer();
                    }
                }
            }
            if (@out == slowBuffer)
            {
                @out.limit(@out.position());
                @out.rewind();
                this.inner.Write(@out);
            }
            inBuf.position(inPtr);
            return(length);
        }
 public long get(int index)
 {
     return(buffer.getLong(index * Capnproto.Constants.BYTES_PER_WORD));
 }