Android ByteBuffer to OutputStream Convert 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;
/*// w w  w .  ja 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 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. copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)