Java InputStream Copy copyBytes(DataInput in, DataOutput out, int length, byte[] buf)

Here you can find the source of copyBytes(DataInput in, DataOutput out, int length, byte[] buf)

Description

Copies the specified number of bytes from the given DataInput to the given DataOutput.

License

Open Source License

Parameter

Parameter Description
in the DataInput to read from.
out the DataOutput to write to.
length the number of bytes to copy.
buf the buffer to use. Must be of length BUF_LEN.

Declaration

public static final void copyBytes(DataInput in, DataOutput out,
        int length, byte[] buf) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w  w w. j av a 2s  .c  om
 * Copyright (C) 2010-2014 Fabric project group, Cornell University
 *
 * This file is part of Fabric.
 *
 * Fabric is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 2 of the License, or (at your option) any later
 * version.
 * 
 * Fabric is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 */

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Main {
    private static final byte BUF_LEN_LOG_2 = 7;
    private static final byte BUF_LEN_MASK = 0x7f;

    /**
     * Copies the specified number of bytes from the given DataInput to the given
     * DataOutput.
     * 
     * @param in
     *          the DataInput to read from.
     * @param out
     *          the DataOutput to write to.
     * @param length
     *          the number of bytes to copy.
     * @param buf
     *          the buffer to use. Must be of length BUF_LEN.
     */
    public static final void copyBytes(DataInput in, DataOutput out,
            int length, byte[] buf) throws IOException {
        int numLoops = length >> BUF_LEN_LOG_2;
        for (int count = 0; count < numLoops; count++) {
            in.readFully(buf);
            out.write(buf);
        }
        in.readFully(buf, 0, length & BUF_LEN_MASK);
        out.write(buf, 0, length & BUF_LEN_MASK);
    }
}

Related

  1. copyByte(InputStream input, OutputStream output)
  2. copyBytes(final byte[] bytes, final OutputStream outputStream)
  3. copyBytes(final InputStream in, final OutputStream out, final int buffSize, final boolean close)
  4. copyBytes(final InputStream is, final OutputStream os)
  5. copyBytes(InputStream in, OutputStream out)