当前位置: 首页>>代码示例>>C#>>正文


C# ByteBuffer.slice方法代码示例

本文整理汇总了C#中System.ByteBuffer.slice方法的典型用法代码示例。如果您正苦于以下问题:C# ByteBuffer.slice方法的具体用法?C# ByteBuffer.slice怎么用?C# ByteBuffer.slice使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.ByteBuffer的用法示例。


在下文中一共展示了ByteBuffer.slice方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: slice

            /* slices a read only contiguous buffer of chunkLength */
            private ByteBuffer slice(int chunkLength)
            {
                int len = chunkLength;
                long oldOffset = currentOffset;
                ByteBuffer slice;
                if (compressed.remaining() >= len)
                {
                    slice = compressed.slice();
                    // simple case
                    slice.limit(len);
                    currentOffset += len;
                    compressed.position(compressed.position() + len);
                    return slice;
                }
                else if (currentRange >= (bytes.Count - 1))
                {
                    // nothing has been modified yet
                    throw new IOException("EOF in " + this + " while trying to read " +
                        chunkLength + " bytes");
                }

                if (LOG.isDebugEnabled())
                {
                    LOG.debug(String.Format(
                        "Crossing into next BufferChunk because compressed only has %d bytes (needs %d)",
                        compressed.remaining(), len));
                }

                // we need to consolidate 2 or more buffers into 1
                // first copy out compressed buffers
                ByteBuffer copy = allocateBuffer(chunkLength, compressed.isDirect());
                currentOffset += compressed.remaining();
                len -= compressed.remaining();
                copy.put(compressed);

                for (int i = currentRange; i < bytes.Count && len > 0; i++)
                {
                    ++currentRange;
                    if (LOG.isDebugEnabled())
                    {
                        LOG.debug(String.Format("Read slow-path, >1 cross block reads with {0}", this.ToString()));
                    }
                    DiskRange range = bytes[i];
                    compressed = range.getData().duplicate();
                    if (compressed.remaining() >= len)
                    {
                        slice = compressed.slice();
                        slice.limit(len);
                        copy.put(slice);
                        currentOffset += len;
                        compressed.position(compressed.position() + len);
                        return copy;
                    }
                    currentOffset += compressed.remaining();
                    len -= compressed.remaining();
                    copy.put(compressed);
                }

                // restore offsets for exception clarity
                seek(oldOffset);
                throw new IOException("EOF in " + this + " while trying to read " +
                    chunkLength + " bytes");
            }
开发者ID:CurtHagenlocher,项目名称:OrcSharp,代码行数:64,代码来源:InStream.cs

示例2: onReceivedData

        /// <summary>
        /// Continue to read data until the end of an element, then call
        /// elementListener.onReceivedElement(element ). The buffer passed to
        /// onReceivedElement is only valid during this call.  If you need the data
        /// later, you must copy.
        /// </summary>
        ///
        /// <param name="data"></param>
        /// <exception cref="EncodingException">For invalid encoding.</exception>
        public void onReceivedData(ByteBuffer data)
        {
            // We may repeatedly set data to a slice as we read elements.
            data = data.slice();

            // Process multiple objects in the data.
            while (true) {
                bool gotElementEnd;
                int offset;

                try {
                    if (!usePartialData_) {
                        // This is the beginning of an element.
                        if (data.remaining() <= 0)
                            // Wait for more data.
                            return;
                    }

                    // Scan the input to check if a whole TLV object has been read.
                    tlvStructureDecoder_.seek(0);
                    gotElementEnd = tlvStructureDecoder_.findElementEnd(data);
                    offset = tlvStructureDecoder_.getOffset();
                } catch (EncodingException ex) {
                    // Reset to read a new element on the next call.
                    usePartialData_ = false;
                    tlvStructureDecoder_ = new TlvStructureDecoder();

                    throw ex;
                }

                if (gotElementEnd) {
                    // Got the remainder of an element.  Report to the caller.
                    ByteBuffer element;
                    if (usePartialData_) {
                        // We have partial data from a previous call, so append this data and point to partialData.
                        partialData_.ensuredPut(data, 0, offset);

                        element = partialData_.flippedBuffer();
                        // Assume we don't need to use partialData anymore until needed.
                        usePartialData_ = false;
                    } else {
                        // We are not using partialData, so just point to the input data buffer.
                        element = data.duplicate();
                        element.limit(offset);
                    }

                    // Reset to read a new object. Do this before calling onReceivedElement
                    // in case it throws an exception.
                    data.position(offset);
                    data = data.slice();
                    tlvStructureDecoder_ = new TlvStructureDecoder();

                    elementListener_.onReceivedElement(element);
                    if (data.remaining() <= 0)
                        // No more data in the packet.
                        return;

                    // else loop back to decode.
                } else {
                    // Save remaining data for a later call.
                    if (!usePartialData_) {
                        usePartialData_ = true;
                        partialData_.position(0);
                    }

                    if (partialData_.buffer().position() + data.remaining() > net.named_data.jndn.util.Common.MAX_NDN_PACKET_SIZE) {
                        // Reset to read a new element on the next call.
                        usePartialData_ = false;
                        tlvStructureDecoder_ = new TlvStructureDecoder();

                        throw new EncodingException(
                                "The incoming packet exceeds the maximum limit Face.getMaxNdnPacketSize()");
                    }

                    partialData_.ensuredPut(data);
                    return;
                }
            }
        }
开发者ID:named-data,项目名称:ndn-dot-net,代码行数:88,代码来源:ElementReader.cs

示例3: Blob

        /// <summary>
        /// Create a new Blob from an existing ByteBuffer.  IMPORTANT: If copy is
        /// false, after calling this constructor, if you keep a pointer to the buffer
        /// then you must treat it as immutable and promise not to change it.
        /// </summary>
        ///
        /// <param name="buffer"></param>
        /// <param name="copy"></param>
        public Blob(ByteBuffer buffer, bool copy)
        {
            this.haveHashCode_ = false;
            if (buffer != null) {
                if (copy) {
                    buffer_ = ILOG.J2CsMapping.NIO.ByteBuffer.allocate(buffer.remaining());

                    // Put updates buffer.position(), so save and restore it.
                    int savePosition = buffer.position();
                    buffer_.put(buffer);
                    buffer.position(savePosition);

                    buffer_.flip();
                } else
                    buffer_ = buffer.slice();
            } else
                buffer_ = null;
        }
开发者ID:named-data,项目名称:ndn-dot-net,代码行数:26,代码来源:Blob.cs


注:本文中的System.ByteBuffer.slice方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。