Exemplo n.º 1
0
        /// <exception cref="System.IO.IOException"/>
        public static void Write(java.nio.channels.WritableByteChannel outputChannel, Capnproto.MessageBuilder message)
        {
            java.nio.ByteBuffer[] segments = message.GetSegmentsForOutput();
            int tableSize = (segments.Length + 2) & (~1);

            java.nio.ByteBuffer table = java.nio.ByteBuffer.allocate(4 * tableSize);
            table.order(java.nio.ByteOrder.LITTLE_ENDIAN);
            table.putInt(0, segments.Length - 1);
            for (int i = 0; i < segments.Length; ++i)
            {
                table.putInt(4 * (i + 1), segments[i].limit() / 8);
            }
            //Any padding is already zeroed.
            while (table.hasRemaining())
            {
                outputChannel.Write(table);
            }
            foreach (java.nio.ByteBuffer buffer in segments)
            {
                while (buffer.hasRemaining())
                {
                    outputChannel.Write(buffer);
                }
            }
        }
Exemplo n.º 2
0
 /// <exception cref="System.IO.IOException"/>
 public virtual void writeBuffer(java.nio.channels.SeekableByteChannel channel)
 {
     buffer.mark();
     while (buffer.hasRemaining())
     {
         channel.write(buffer);
     }
     buffer.reset();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Copies a string to ByteBuffer, if byteBuffer overflows then not all of string
 /// is copied
 /// </summary>
 /// <param name="string"/>
 /// <param name="byteBuffer">destination buffer is duplicated first so that position is not list
 ///     </param>
 /// <returns>new byteBuffer</returns>
 public static java.nio.ByteBuffer copy(string @string, java.nio.ByteBuffer byteBuffer
                                        )
 {
     java.nio.ByteBuffer dupe = byteBuffer.duplicate();
     for (int i = 0; i < @string.Length && dupe.hasRemaining(); i++)
     {
         dupe.put(unchecked ((byte)@string[i]));
     }
     return(dupe);
 }
Exemplo n.º 4
0
 /// <exception cref="System.IO.IOException"/>
 public static void fillBuffer(java.nio.ByteBuffer buffer, java.nio.channels.ReadableByteChannel bc)
 {
     while (buffer.hasRemaining())
     {
         int r = bc.Read(buffer);
         if (r < 0)
         {
             throw new System.IO.IOException("premature EOF");
         }
     }
 }
Exemplo n.º 5
0
        /// <exception cref="System.IO.IOException"/>
        public int Write(java.nio.ByteBuffer src)
        {
            int available = this.buf.remaining();
            int size      = src.remaining();

            if (size <= available)
            {
                this.buf.put(src);
            }
            else if (size <= this.buf.capacity())
            {
                //# Too much for this buffer, but not a full buffer's worth,
                //# so we'll go ahead and copy.
                java.nio.ByteBuffer slice = src.slice();
                slice.limit(available);
                this.buf.put(slice);
                this.buf.rewind();
                while (this.buf.hasRemaining())
                {
                    this.inner.Write(this.buf);
                }
                this.buf.rewind();
                src.position(src.position() + available);
                this.buf.put(src);
            }
            else
            {
                //# Writing so much data that we might as well write
                //# directly to avoid a copy.
                int pos = this.buf.position();
                this.buf.rewind();
                java.nio.ByteBuffer slice = this.buf.slice();
                slice.limit(pos);
                while (slice.hasRemaining())
                {
                    this.inner.Write(slice);
                }
                while (src.hasRemaining())
                {
                    this.inner.Write(src);
                }
            }
            return(size);
        }
Exemplo n.º 6
0
        /// <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);
        }
Exemplo n.º 7
0
 public override int read(char[] buffer, int offset, int length)
 {
     lock (@lock)
     {
         if (!isOpen())
         {
             throw new System.IO.IOException("InputStreamReader is closed");
         }
         java.util.Arrays.checkOffsetAndCount(buffer.Length, offset, length);
         if (length == 0)
         {
             return(0);
         }
         java.nio.CharBuffer          @out   = java.nio.CharBuffer.wrap(buffer, offset, length);
         java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW;
         // bytes.remaining() indicates number of bytes in buffer
         // when 1-st time entered, it'll be equal to zero
         bool needInput = !bytes.hasRemaining();
         while (@out.hasRemaining())
         {
             // fill the buffer if needed
             if (needInput)
             {
                 try
                 {
                     if (@in.available() == 0 && @out.position() > offset)
                     {
                         // we could return the result without blocking read
                         break;
                     }
                 }
                 catch (System.IO.IOException)
                 {
                 }
                 // available didn't work so just try the read
                 int desiredByteCount = bytes.capacity() - bytes.limit();
                 int off             = bytes.arrayOffset() + bytes.limit();
                 int actualByteCount = @in.read(((byte[])bytes.array()), off, desiredByteCount);
                 if (actualByteCount == -1)
                 {
                     endOfInput = true;
                     break;
                 }
                 else
                 {
                     if (actualByteCount == 0)
                     {
                         break;
                     }
                 }
                 bytes.limit(bytes.limit() + actualByteCount);
                 needInput = false;
             }
             // decode bytes
             result = decoder.decode(bytes, @out, false);
             if (result.isUnderflow())
             {
                 // compact the buffer if no space left
                 if (bytes.limit() == bytes.capacity())
                 {
                     bytes.compact();
                     bytes.limit(bytes.position());
                     bytes.position(0);
                 }
                 needInput = true;
             }
             else
             {
                 break;
             }
         }
         if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput)
         {
             result = decoder.decode(bytes, @out, true);
             decoder.flush(@out);
             decoder.reset();
         }
         if (result.isMalformed() || result.isUnmappable())
         {
             result.throwException();
         }
         return(@out.position() - offset == 0 ? -1 : @out.position() - offset);
     }
 }