Here you can find the source of concat(ByteBuffer b1, ByteBuffer b2)
Parameter | Description |
---|---|
b1 | a parameter |
b2 | a parameter |
public static ByteBuffer concat(ByteBuffer b1, ByteBuffer b2)
//package com.java2s; /**//ww w . j ava 2 s. co m * Created on June 29, 2009. * * Copyright 2010- The MITRE Corporation. All rights reserved. * * 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 andlimitations under * the License. * * $Id$ */ import java.nio.ByteBuffer; public class Main { /** * Allocates a new ByteBuffer with a capacity of the sum of the <b>{@link ByteBuffer#limit()}</b> * of the two given ByteBuffers and cancatinates them together. * If a limit is not set, then {@link ByteBuffer#capacity()} will be used. * Also if one buffer is directly allocated, then the returning buffer will be directly allocated. * * @param b1 * @param b2 * @return */ public static ByteBuffer concat(ByteBuffer b1, ByteBuffer b2) { int capacity = ((b1.limit() == 0 ? b1.capacity() : b1.limit())) + ((b2.limit() == 0 ? b2.capacity() : b2.limit())); ByteBuffer dst; if (b1.isDirect() || b2.isDirect()) { dst = ByteBuffer.allocateDirect(capacity); } else { dst = ByteBuffer.allocate(capacity); } b1.rewind(); dst.put(b1); b2.rewind(); dst.put(b2); return dst; } }