Java ByteBuffer Copy copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)

Here you can find the source of copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length)

Description

Copy the given number of bytes from the given stream and put it at the current position of the given buffer, updating the position in the buffer.

License

Apache License

Parameter

Parameter Description
out the buffer to write data to
in the stream to read data from
length the number of bytes to read/write

Declaration

public static void copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w ww . j a  v a 2  s .c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to you 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.io.DataInputStream;
import java.io.IOException;

import java.nio.ByteBuffer;

public class Main {
    /**
     * Copy the given number of bytes from the given stream and put it at the
     * current position of the given buffer, updating the position in the buffer.
     * @param out the buffer to write data to
     * @param in the stream to read data from
     * @param length the number of bytes to read/write
     */
    public static void copyFromStreamToBuffer(ByteBuffer out, DataInputStream in, int length) throws IOException {
        if (out.hasArray()) {
            in.readFully(out.array(), out.position() + out.arrayOffset(), length);
            skip(out, length);
        } else {
            for (int i = 0; i < length; ++i) {
                out.put(in.readByte());
            }
        }
    }

    /**
     * Increment position in buffer.
     * @param buffer In this buffer.
     * @param length By that many bytes.
     */
    public static void skip(ByteBuffer buffer, int length) {
        buffer.position(buffer.position() + length);
    }
}

Related

  1. copyBytesFrom(ByteBuffer bb)
  2. copyContentsToArray(ByteBuffer src)
  3. copyFileByMappedByteBuffer(String srcFileName, String dstFileName)
  4. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  5. copyFromBufferToBuffer(ByteBuffer out, ByteBuffer in, int sourceOffset, int length)
  6. copyOf(ByteBuffer payload)
  7. copyRemaining(ByteBuffer src, ByteBuffer dst)
  8. copyTo(ByteBuffer original, ByteBuffer copy)
  9. copyToArray(@Nonnull ByteBuffer data)