Java ByteBuffer Put put(ByteBuffer dst, ByteBuffer src, int transferLimit)

Here you can find the source of put(ByteBuffer dst, ByteBuffer src, int transferLimit)

Description

Transfer from src to dst without throwing exception if src.remaining() > dst.remaining() but copying dst.remaining() bytes from src instead.

License

Apache License

Declaration

public static ByteBuffer put(ByteBuffer dst, ByteBuffer src, int transferLimit) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2015 University of Massachusetts
 * /*from ww w  .  jav a2 s  .  c  o m*/
 * 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 {
    /**
     * Transfer from src to dst without throwing exception if src.remaining() >
     * dst.remaining() but copying dst.remaining() bytes from src instead.
     * {@code transferLimit} further limits the number of bytes transferred from
     * src to dst.
     */
    public static ByteBuffer put(ByteBuffer dst, ByteBuffer src, int transferLimit) {
        if (src.remaining() <= dst.remaining() && transferLimit >= dst.remaining())
            return dst.put(src);
        int oldLimit = src.limit();
        src.limit(src.position() + Math.min(dst.remaining(), transferLimit));
        dst.put(src);
        src.limit(oldLimit);
        return dst;
    }

    public static ByteBuffer put(ByteBuffer dst, ByteBuffer src) {
        return put(dst, src, dst.remaining());
    }
}

Related

  1. outputBuffer(ByteBuffer buffer)
  2. put(ByteBuffer bb, byte[] bytes, int from)
  3. put(ByteBuffer bb, String s)
  4. put(ByteBuffer bbuf, byte[] post)
  5. put(ByteBuffer buf, String s, String charsetName)
  6. put(ByteBuffer from, ByteBuffer to)
  7. put(ByteBuffer src, ByteBuffer dst)
  8. put3ByteInt(ByteBuffer buffer, int val)
  9. putAscii(ByteBuffer bytes, String value)