Пример #1
0
        ///Upon return, `bb.position()` will be at the end of the message.
        /// <exception cref="System.IO.IOException"/>
        public static Capnproto.MessageReader Read(java.nio.ByteBuffer bb, Capnproto.ReaderOptions options)
        {
            bb.order(java.nio.ByteOrder.LITTLE_ENDIAN);
            int segmentCount = 1 + bb.getInt();

            if (segmentCount > 512)
            {
                throw new System.IO.IOException("too many segments");
            }
            java.nio.ByteBuffer[] segmentSlices = new java.nio.ByteBuffer[segmentCount];
            int segmentSizesBase = bb.position();
            int segmentSizesSize = segmentCount * 4;
            int align            = Capnproto.Constants.BYTES_PER_WORD - 1;
            int segmentBase      = (segmentSizesBase + segmentSizesSize + align) & ~align;
            int totalWords       = 0;

            for (int ii = 0; ii < segmentCount; ++ii)
            {
                int segmentSize = bb.getInt(segmentSizesBase + ii * 4);
                bb.position(segmentBase + totalWords * Capnproto.Constants.BYTES_PER_WORD);
                segmentSlices[ii] = bb.slice();
                segmentSlices[ii].limit(segmentSize * Capnproto.Constants.BYTES_PER_WORD);
                segmentSlices[ii].order(java.nio.ByteOrder.LITTLE_ENDIAN);
                totalWords += segmentSize;
            }
            bb.position(segmentBase + totalWords * Capnproto.Constants.BYTES_PER_WORD);
            if (totalWords > options.traversalLimitInWords)
            {
                throw new Capnproto.DecodeException("Message size exceeds traversal limit.");
            }
            return(new Capnproto.MessageReader(segmentSlices, options));
        }
Пример #2
0
        /// <exception cref="System.IO.IOException"/>
        public static Capnproto.MessageReader Read(java.nio.channels.ReadableByteChannel bc, Capnproto.ReaderOptions options)
        {
            java.nio.ByteBuffer firstWord = makeByteBuffer(Capnproto.Constants.BYTES_PER_WORD);
            fillBuffer(firstWord, bc);
            int segmentCount = 1 + firstWord.getInt(0);
            int segment0Size = 0;

            if (segmentCount > 0)
            {
                segment0Size = firstWord.getInt(4);
            }
            int totalWords = segment0Size;

            if (segmentCount > 512)
            {
                throw new System.IO.IOException("too many segments");
            }

            //In words.
            System.Collections.Generic.List <int> moreSizes = new System.Collections.Generic.List <int>();
            if (segmentCount > 1)
            {
                java.nio.ByteBuffer moreSizesRaw = makeByteBuffer(4 * (segmentCount & ~1));
                fillBuffer(moreSizesRaw, bc);
                for (int ii = 0; ii < segmentCount - 1; ++ii)
                {
                    int size = moreSizesRaw.getInt(ii * 4);
                    moreSizes.Add(size);
                    totalWords += size;
                }
            }
            if (totalWords > options.traversalLimitInWords)
            {
                throw new Capnproto.DecodeException("Message size exceeds traversal limit.");
            }
            java.nio.ByteBuffer allSegments = makeByteBuffer(totalWords * Capnproto.Constants.BYTES_PER_WORD);
            fillBuffer(allSegments, bc);
            java.nio.ByteBuffer[] segmentSlices = new java.nio.ByteBuffer[segmentCount];
            allSegments.rewind();
            segmentSlices[0] = allSegments.slice();
            segmentSlices[0].limit(segment0Size * Capnproto.Constants.BYTES_PER_WORD);
            segmentSlices[0].order(java.nio.ByteOrder.LITTLE_ENDIAN);
            int offset = segment0Size;

            for (int ii = 1; ii < segmentCount; ++ii)
            {
                allSegments.position(offset * Capnproto.Constants.BYTES_PER_WORD);
                segmentSlices[ii] = allSegments.slice();
                segmentSlices[ii].limit(moreSizes[ii - 1] * Capnproto.Constants.BYTES_PER_WORD);
                segmentSlices[ii].order(java.nio.ByteOrder.LITTLE_ENDIAN);
                offset += moreSizes[ii - 1];
            }
            return(new Capnproto.MessageReader(segmentSlices, options));
        }
Пример #3
0
 /// <exception cref="System.IO.IOException"/>
 public static Capnproto.MessageReader ReadFromUnbuffered(java.nio.channels.ReadableByteChannel input, Capnproto.ReaderOptions options)
 {
     Capnproto.PackedInputStream packedInput = new Capnproto.PackedInputStream(new Capnproto.BufferedInputStreamWrapper(input));
     return(Capnproto.Serialize.Read(packedInput, options));
 }
Пример #4
0
 public MessageReader(java.nio.ByteBuffer[] segmentSlices, Capnproto.ReaderOptions options)
 {
     this.nestingLimit = options.nestingLimit;
     this.arena        = new Capnproto.ReaderArena(segmentSlices, options.traversalLimitInWords);
 }
Пример #5
0
 /// <exception cref="System.IO.IOException"/>
 public static Capnproto.MessageReader Read(Capnproto.BufferedInputStream input, Capnproto.ReaderOptions options)
 {
     Capnproto.PackedInputStream packedInput = new Capnproto.PackedInputStream(input);
     return(Capnproto.Serialize.Read(packedInput, options));
 }