Java ByteBuffer Copy copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)

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

Description

Copy data from a buffer to an output stream.

License

Apache License

Parameter

Parameter Description
out the stream to write bytes to
in the buffer to read bytes from
offset the offset in the buffer (from the buffer's array offset) to start copying bytes from
length the number of bytes to copy

Declaration

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

Method Source Code

//package com.java2s;
/*// w w  w .j a v a 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 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));
            }
        }
    }
}

Related

  1. copy(ByteBuffer src, int startindex, int endindex)
  2. copy(final ByteBuffer from, final ByteBuffer to)
  3. copy2Buffer(byte[] src, int srcOff, int srcLen, ByteBuffer dst, int dstOff)
  4. copyBinary(final ByteBuffer orig)
  5. copyBuffer(ByteBuffer dest, int pos1, ByteBuffer src, int len)
  6. copyByteBuffer(ByteBuffer buf)
  7. copyByteBuffer(ByteBuffer src)
  8. copyByteBuffer(ByteBuffer srcBuf, int srcStep, ByteBuffer dstBuf, int dstStep)
  9. copyByteBuffer(final ByteBuffer pBuffer)