Java ByteBuffer Flip transfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer, final boolean bNeedsFlip)

Here you can find the source of transfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer, final boolean bNeedsFlip)

Description

Transfer as much as possible from source to dest buffer.

License

Apache License

Parameter

Parameter Description
aSrcBuffer Source buffer. May not be <code>null</code>.
aDstBuffer Destination buffer. May not be <code>null</code>.
bNeedsFlip whether or not to flip src

Return

The amount of data transferred. Always ≥ 0.

Declaration

@Nonnegative
public static int transfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer,
        final boolean bNeedsFlip) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .  java 2  s .co m*/
 * Copyright (C) 2006-2013 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * 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;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

public class Main {
    /**
     * Transfer as much as possible from source to dest buffer.
     * 
     * @param aSrcBuffer
     *        Source buffer. May not be <code>null</code>.
     * @param aDstBuffer
     *        Destination buffer. May not be <code>null</code>.
     * @param bNeedsFlip
     *        whether or not to flip src
     * @return The amount of data transferred. Always &ge; 0.
     */
    @Nonnegative
    public static int transfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer,
            final boolean bNeedsFlip) {
        if (aSrcBuffer == null)
            throw new NullPointerException("srcBuffer");
        if (aDstBuffer == null)
            throw new NullPointerException("dstBuffer");

        int nRead = 0;
        if (bNeedsFlip) {
            if (aSrcBuffer.position() > 0) {
                aSrcBuffer.flip();
                nRead = _doTransfer(aSrcBuffer, aDstBuffer);
                if (aSrcBuffer.hasRemaining())
                    aSrcBuffer.compact();
                else
                    aSrcBuffer.clear();
            }
        } else {
            if (aSrcBuffer.hasRemaining())
                nRead = _doTransfer(aSrcBuffer, aDstBuffer);
        }
        return nRead;
    }

    @Nonnegative
    private static int _doTransfer(@Nonnull final ByteBuffer aSrcBuffer, @Nonnull final ByteBuffer aDstBuffer) {
        final int nSrcRemaining = aSrcBuffer.remaining();
        final int nDstRemaining = aDstBuffer.remaining();
        if (nDstRemaining >= nSrcRemaining) {
            // Dest buffer is large enough to hold complete source buffer content
            aDstBuffer.put(aSrcBuffer);
            return nSrcRemaining;
        }

        final int nSrcLimit = aSrcBuffer.limit();
        final int nSrcPos = aSrcBuffer.position();
        aSrcBuffer.limit(nSrcPos + nDstRemaining);
        aDstBuffer.put(aSrcBuffer);
        aSrcBuffer.limit(nSrcLimit);
        return nDstRemaining;
    }
}

Related

  1. flip(ByteBuffer[] bufs)
  2. flipPutFlip(ByteBuffer from, ByteBuffer to)
  3. flipToFill(ByteBuffer buffer)
  4. flipToFlush(ByteBuffer buffer, int position)
  5. readAndFlip(ReadableByteChannel channel, ByteBuffer buffer, int bytes)