Here you can find the source of copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length)
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 |
public static void copyBufferToStream(OutputStream out, ByteBuffer in, int offset, int length) throws IOException
//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)); } } } }