Java ByteBuffer Put putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src)

Here you can find the source of putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src)

Description

Put as many bytes as possible from the src buffer into the destination buffer.

License

Open Source License

Parameter

Parameter Description
dest the destination buffer
src the source buffer

Return

true if src.remaining() == 0

Declaration

public static boolean putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src) 

Method Source Code


//package com.java2s;
/*//from   w ww.  ja  v a  2  s .co  m
 * This file is part of the JDrupes non-blocking HTTP Codec
 * Copyright (C) 2016, 2017  Michael N. Lipp
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along 
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.ByteBuffer;

public class Main {
    /**
     * Put as many bytes as possible from the src buffer into the destination
     * buffer.
     * 
     * @param dest
     *            the destination buffer
     * @param src
     *            the source buffer
     * @return {@code true} if {@code src.remaining() == 0}
     */
    public static boolean putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src) {
        if (dest.remaining() >= src.remaining()) {
            dest.put(src);
            return true;
        }
        if (dest.remaining() > 0) {
            int oldLimit = src.limit();
            src.limit(src.position() + dest.remaining());
            dest.put(src);
            src.limit(oldLimit);
        }
        return false;
    }

    /**
     * Put as many bytes as possible from the src buffer into the destination
     * buffer but not more than specified by limit.
     * 
     * @param dest
     *            the destination buffer
     * @param src
     *            the source buffer
     * @param limit
     *            the maximum number of bytes to transfer
     * @return {@code true} if {@code src.remaining() == 0}
     */
    public static boolean putAsMuchAsPossible(ByteBuffer dest, ByteBuffer src, int limit) {
        if (src.remaining() <= limit) {
            return putAsMuchAsPossible(dest, src);
        }
        int oldLimit = src.limit();
        try {
            src.limit(src.position() + limit);
            return putAsMuchAsPossible(dest, src);
        } finally {
            src.limit(oldLimit);
        }
    }
}

Related

  1. put(ByteBuffer dst, ByteBuffer src, int transferLimit)
  2. put(ByteBuffer from, ByteBuffer to)
  3. put(ByteBuffer src, ByteBuffer dst)
  4. put3ByteInt(ByteBuffer buffer, int val)
  5. putAscii(ByteBuffer bytes, String value)
  6. putBoolean(ByteBuffer bb, boolean value)
  7. putByte(ByteBuffer byteBuffer, int value)
  8. putByteArray(ByteBuffer byteBuffer, byte[] array)
  9. putByteAsString(ByteBuffer buffer, byte value)