<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;

namespace TomP2P.Extensions.Netty
{
    public sealed class UnpooledHeapByteBuf : AbstractByteBuf
    {
        private readonly IByteBufAllocator _alloc;
        private sbyte[] _array;
        private MemoryStream _tmpNioBuf;

        public UnpooledHeapByteBuf(IByteBufAllocator alloc, int initialCapacity, int maxCapacity)
            : this(alloc, new sbyte[initialCapacity], 0, 0, maxCapacity)
        { }

        public UnpooledHeapByteBuf(IByteBufAllocator alloc, sbyte[] initialArray, int maxCapacity)
            : this(alloc, initialArray, 0, initialArray.Length, maxCapacity)
        { }

        private UnpooledHeapByteBuf(IByteBufAllocator alloc, sbyte[] initialArray, int readerIndex, int writerIndex,
            int maxCapacity)
            : base(maxCapacity)
        {
            if (alloc == null)
            {
                throw new NullReferenceException("alloc");
            }
            if (initialArray == null)
            {
                throw new NullReferenceException("initialArray");
            }
            if (initialArray.Length > maxCapacity)
            {
                throw new ArgumentException(String.Format(
                        "initialCapacity({0}) > maxCapacity({1})", initialArray.Length, maxCapacity));
            }

            _alloc = alloc;
            SetArray(initialArray);
            SetIndex(readerIndex, writerIndex);
        }

        private void SetArray(sbyte[] initialArray)
        {
            _array = initialArray;
            _tmpNioBuf = null;
        }

        public override IByteBufAllocator Alloc
        {
            get { return _alloc; }
        }

        public override int Capacity
        {
            get
            {
                // TODO reference, ensureAccessible()
                return _array.Length;
            }
        }

        public override ByteBuf SetCapacity(int newCapacity)
        {
            // TODO< ensureAccessible();
            if (newCapacity < 0 || newCapacity > MaxCapacity)
            {
                throw new ArgumentException("newCapacity: " + newCapacity);
            }

            int oldCapacity = _array.Length;
            if (newCapacity > oldCapacity)
            {
                sbyte[] newArray = new sbyte[newCapacity];
                Array.Copy(_array, 0, newArray, 0, _array.Length);
                SetArray(newArray);
            }
            else if (newCapacity < oldCapacity)
            {
                sbyte[] newArray = new sbyte[newCapacity];
                int readerIndex = ReaderIndex;
                if (readerIndex < newCapacity)
                {
                    int writerIndex = WriterIndex;
                    if (writerIndex > newCapacity)
                    {
                        SetWriterIndex(writerIndex = newCapacity);
                    }
                    Array.Copy(_array, readerIndex, newArray, readerIndex, writerIndex - readerIndex);
                }
                else
                {
                    SetIndex(newCapacity, newCapacity);
                }
                SetArray(newArray);
            }
            return this;
        }

        public override ByteBuf GetBytes(int index, sbyte[] dst, int dstIndex, int length)
        {
            CheckDstIndex(index, length, dstIndex, dst.Length);
            Array.Copy(_array, index, dst, dstIndex, length);
            return this;
        }

        public override ByteBuf SetBytes(int index, sbyte[] src, int srcIndex, int length)
        {
            CheckSrcIndex(index, length, srcIndex, src.Length);

            try
            {
                Array.Copy(src, srcIndex, _array, index, length);

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            return this;
        }

        public override ByteBuf SetBytes(int index, ByteBuf src, int srcIndex, int length)
        {
            throw new NotImplementedException();<commit_msg>TestEncodeBytes works when RUN<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;

namespace TomP2P.Extensions.Netty
{
    public sealed class UnpooledHeapByteBuf : AbstractByteBuf
    {
        private readonly IByteBufAllocator _alloc;
        private sbyte[] _array;
        private MemoryStream _tmpNioBuf;

        public UnpooledHeapByteBuf(IByteBufAllocator alloc, int initialCapacity, int maxCapacity)
            : this(alloc, new sbyte[initialCapacity], 0, 0, maxCapacity)
        { }

        public UnpooledHeapByteBuf(IByteBufAllocator alloc, sbyte[] initialArray, int maxCapacity)
            : this(alloc, initialArray, 0, initialArray.Length, maxCapacity)
        { }

        private UnpooledHeapByteBuf(IByteBufAllocator alloc, sbyte[] initialArray, int readerIndex, int writerIndex,
            int maxCapacity)
            : base(maxCapacity)
        {
            if (alloc == null)
            {
                throw new NullReferenceException("alloc");
            }
            if (initialArray == null)
            {
                throw new NullReferenceException("initialArray");
            }
            if (initialArray.Length > maxCapacity)
            {
                throw new ArgumentException(String.Format(
                        "initialCapacity({0}) > maxCapacity({1})", initialArray.Length, maxCapacity));
            }

            _alloc = alloc;
            SetArray(initialArray);
            SetIndex(readerIndex, writerIndex);
        }

        private void SetArray(sbyte[] initialArray)
        {
            _array = initialArray;
            _tmpNioBuf = null;
        }

        public override IByteBufAllocator Alloc
        {
            get { return _alloc; }
        }

        public override int Capacity
        {
            get
            {
                // TODO reference, ensureAccessible()
                return _array.Length;
            }
        }

        public override ByteBuf SetCapacity(int newCapacity)
        {
            // TODO< ensureAccessible();
            if (newCapacity < 0 || newCapacity > MaxCapacity)
            {
                throw new ArgumentException("newCapacity: " + newCapacity);
            }

            int oldCapacity = _array.Length;
            if (newCapacity > oldCapacity)
            {
                sbyte[] newArray = new sbyte[newCapacity];
                Array.Copy(_array, 0, newArray, 0, _array.Length);
                SetArray(newArray);
            }
            else if (newCapacity < oldCapacity)
            {
                sbyte[] newArray = new sbyte[newCapacity];
                int readerIndex = ReaderIndex;
                if (readerIndex < newCapacity)
                {
                    int writerIndex = WriterIndex;
                    if (writerIndex > newCapacity)
                    {
                        SetWriterIndex(writerIndex = newCapacity);
                    }
                    Array.Copy(_array, readerIndex, newArray, readerIndex, writerIndex - readerIndex);
                }
                else
                {
                    SetIndex(newCapacity, newCapacity);
                }
                SetArray(newArray);
            }
            return this;
        }

        public override ByteBuf GetBytes(int index, sbyte[] dst, int dstIndex, int length)
        {
            CheckDstIndex(index, length, dstIndex, dst.Length);
            Array.Copy(_array, index, dst, dstIndex, length);
            return this;
        }

        public override ByteBuf SetBytes(int index, sbyte[] src, int srcIndex, int length)
        {
            CheckSrcIndex(index, length, srcIndex, src.Length);
            Array.Copy(src, srcIndex, _array, index, length);
            return this;
        }

        public override ByteBuf SetBytes(int index, ByteBuf src, int srcIndex, int length)
        {
            throw new NotImplementedException();
{'fraction_non_alphanumeric': 0.07708619184233503, 'fraction_numerical': 0.0022452288886117, 'mean_word_length': 1.51821608040201, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1366355', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1905, 'n_words': 479}