Java ByteBuffer Put moveBufferToStream(OutputStream out, ByteBuffer in, int length)

Here you can find the source of moveBufferToStream(OutputStream out, ByteBuffer in, int length)

Description

Copy the data to the output stream and update position in buffer.

License

Apache License

Parameter

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

Declaration

public static void moveBufferToStream(OutputStream out, ByteBuffer in, int length) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w  w w . j a va 2 s  . co  m*/
 * 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.IOException;

import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Main {
    /**
     * Copy the data to the output stream and update position in buffer.
     * @param out the stream to write bytes to
     * @param in the buffer to read bytes from
     * @param length the number of bytes to copy
     */
    public static void moveBufferToStream(OutputStream out, ByteBuffer in, int length) throws IOException {
        copyBufferToStream(out, in, in.position(), length);
        skip(in, length);
    }

    /**
     * Copy data from a buffer to an output stream. Does not update the position
     * in the buffer.
     * @param out the stream to write bytes to
     * @param in the buffer to read bytes from
     * @param offset the offset in the buffer (from the buffer's array offset)
     *      to start copying bytes from
     * @param length the number of bytes to copy
     */
    public static void copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)
            throws IOException {
        if (in.hasArray()) {
            out.write(in.array(), in.arrayOffset() + offset, length);
        } else {
            for (int i = 0; i < length; ++i) {
                out.write(in.get(offset + i));
            }
        }
    }

    /**
     * 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. getZeroTerminatedStringBytesArray( ByteBuffer inputBuffer)
  2. inputStream(ByteBuffer bytes)
  3. inputStreamToByteBuffer(InputStream input)
  4. inputStreamToByteBuffer(InputStream is)
  5. makeInputStream(final ByteBuffer buffer)
  6. outputBuffer(ByteBuffer buffer)
  7. put(ByteBuffer bb, byte[] bytes, int from)
  8. put(ByteBuffer bb, String s)
  9. put(ByteBuffer bbuf, byte[] post)