首页
登录 | 注册

[Design] Buffer design

  1. Used multiple chunk to compose a buffer.
    ie:
    public static class Chunk
    {
        public byte[]  mArray;
        public int     mLength;

        public Chunk(int length)
        {
            mArray = new byte[length];
            mLength = 0;
        }
    }

    class ByteArrayBuilder
    {
        private LinkedList mChunks;

        public ByteArrayBuilder()
        {
            mChunks = new LinkedList();
        }

        public synchronized void append(byte[] array, int offset, int length)
        {
            while (length > 0)
            {
                Chunk c = null;
                if (mChunks.isEmpty())
                {
                    c = new Chunk(length);
                    mChunks.addLast(c);
                }
                else
                {
                    c = mChunks.getLast();
                    if (c.mLength == c.mArray.length)
                    {
                        c = new Chunk(length);
                        mChunks.addLast(c);
                    }
                }

                int amount = Math.min(length, c.mArray.length - c.mLength);
                System.arraycopy(array, offset, c.mArray, c.mLength, amount);

                c.mLength += amount;
                length -= amount;
                offset += amount;
            }
        }
  2. xxx

相关文章


2019 unjeep.com webmaster#unjeep.com
12 q. 0.011 s.
京ICP备10005923号