예제 #1
0
        /// <summary>
        /// Return an array of the underlying storage from <paramref name="buf"/> into a byte array.
        /// The copy will start at {@code start} and copy {@code length} bytes.
        /// If <paramref name="copy"/> is true a copy will be made of the memory.
        /// If <paramref name="copy"/> is false the underlying storage will be shared, if possible.
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="copy"></param>
        /// <returns></returns>
        public static byte[] GetBytes(IByteBuffer buf, int start, int length, bool copy)
        {
            var capacity = buf.Capacity;

            if (MathUtil.IsOutOfBounds(start, length, capacity))
            {
                ThrowHelper.ThrowIndexOutOfRangeException_Expected(start, length, capacity);
            }

            if (buf.HasArray)
            {
                if (copy || start != 0 || length != capacity)
                {
                    int baseOffset = buf.ArrayOffset + start;
                    var bytes      = new byte[length];
                    PlatformDependent.CopyMemory(buf.Array, baseOffset, bytes, 0, length);
                    return(bytes);
                }
                else
                {
                    return(buf.Array);
                }
            }

            byte[] v = new byte[length];
            _ = buf.GetBytes(start, v);
            return(v);
        }
예제 #2
0
            public static void DoAppendPrettyHexDump(StringBuilder dump, IByteBuffer buf, int offset, int length)
            {
                if (MathUtil.IsOutOfBounds(offset, length, buf.Capacity))
                {
                    ThrowHelper.ThrowIndexOutOfRangeException_Expected(offset, length, buf.Capacity);
                }
                if (0u >= (uint)length)
                {
                    return;
                }
                _ = dump.Append(
                    "         +-------------------------------------------------+" +
                    Newline + "         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |" +
                    Newline + "+--------+-------------------------------------------------+----------------+");

                int startIndex = offset;
                int fullRows   = length.RightUShift(4);
                int remainder  = length & 0xF;

                // Dump the rows which have 16 bytes.
                for (int row = 0; row < fullRows; row++)
                {
                    int rowStartIndex = (row << 4) + startIndex;

                    // Per-row prefix.
                    AppendHexDumpRowPrefix(dump, row, rowStartIndex);

                    // Hex dump
                    int rowEndIndex = rowStartIndex + 16;
                    for (int j = rowStartIndex; j < rowEndIndex; j++)
                    {
                        _ = dump.Append(Byte2Hex[buf.GetByte(j)]);
                    }
                    _ = dump.Append(" |");

                    // ASCII dump
                    for (int j = rowStartIndex; j < rowEndIndex; j++)
                    {
                        _ = dump.Append(Byte2Char[buf.GetByte(j)]);
                    }
                    _ = dump.Append('|');
                }

                // Dump the last row which has less than 16 bytes.
                if (remainder != 0)
                {
                    int rowStartIndex = (fullRows << 4) + startIndex;
                    AppendHexDumpRowPrefix(dump, fullRows, rowStartIndex);

                    // Hex dump
                    int rowEndIndex = rowStartIndex + remainder;
                    for (int j = rowStartIndex; j < rowEndIndex; j++)
                    {
                        _ = dump.Append(Byte2Hex[buf.GetByte(j)]);
                    }
                    _ = dump.Append(HexPadding[remainder]);
                    _ = dump.Append(" |");

                    // Ascii dump
                    for (int j = rowStartIndex; j < rowEndIndex; j++)
                    {
                        _ = dump.Append(Byte2Char[buf.GetByte(j)]);
                    }
                    _ = dump.Append(BytePadding[remainder]);
                    _ = dump.Append('|');
                }

                _ = dump.Append(Newline + "+--------+-------------------------------------------------+----------------+");
            }