Android ByteBuffer to OutputStream Convert 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;
/*/*from w  ww .j  a va 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.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. moveBufferToStream(OutputStream out, ByteBuffer in, int length)