Java ByteBuffer Append append(ByteBuffer source, int index, byte[] dest)

Here you can find the source of append(ByteBuffer source, int index, byte[] dest)

Description

append

License

Apache License

Declaration

protected static byte[] append(ByteBuffer source, int index, byte[] dest) 

Method Source Code

//package com.java2s;
/**   /*from   ww w  .  j  av a 2s  .  com*/
 * Copyright 2011 The Buzz Media, LLC
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteBuffer;

public class Main {
    protected static byte[] append(ByteBuffer source, int index, byte[] dest) {
        // Do nothing if there is nothing to append.
        if (source == null || source.remaining() == 0)
            return dest;

        int byteCount = source.remaining();
        int requiredCapacity = index + byteCount;

        // Make sure our dest array is large enough; resize as necessary.
        if (requiredCapacity > dest.length) {
            byte[] newArray = new byte[requiredCapacity];
            System.arraycopy(dest, 0, newArray, 0, dest.length);
            dest = newArray;
        }

        // Ask the buffer to copy the chars directly into our target array.
        source.get(dest, index, byteCount);

        // Return the modified array to the caller.
        return dest;
    }
}

Related

  1. append(ByteBuffer buffer, String s)
  2. append(ByteBuffer to, byte[] b, int off, int len)
  3. appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, int incomingBufferSize, int BUFFER_STEP_SIZE)
  4. appendHex(StringBuilder sb, ByteBuffer bb)
  5. appendLong(long value, ByteBuffer buffer)