Java InputStream Read Bytes readBytes(InputStream in, int bufferSize, boolean forceClose)

Here you can find the source of readBytes(InputStream in, int bufferSize, boolean forceClose)

Description

read Bytes

License

Apache License

Declaration

public static byte[] readBytes(InputStream in, int bufferSize, boolean forceClose) throws Exception 

Method Source Code

//package com.java2s;
/**//from   w w w  .  ja  va2s.  co m
 * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
 *
 * Licensed 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static byte[] readBytes(InputStream in, int bufferSize, boolean forceClose) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);

        copyInToOut(in, baos, bufferSize, forceClose);
        return baos.toByteArray();
    }

    public static void copyInToOut(InputStream in, String inputEncoding, OutputStream out, String outputEncoding,
            int bufferSize, boolean forceClose) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(in);
        BufferedOutputStream bos = new BufferedOutputStream(out);

        try {
            byte[] buffer = new byte[bufferSize];
            for (;;) {
                int len = bis.read(buffer);
                if (len < 0)
                    break;
                String b = new String(buffer, 0, len, inputEncoding);
                bos.write(b.getBytes(outputEncoding));
            }
        } finally {
            if (forceClose) {
                try {
                    bis.close();
                } catch (Exception e) {
                    // ignore
                }
                try {
                    bos.flush();
                    bos.close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
    }

    public static void copyInToOut(InputStream in, OutputStream out, int bufferSize, boolean forceClose)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(in);
        BufferedOutputStream bos = new BufferedOutputStream(out);

        try {
            byte[] buffer = new byte[bufferSize];
            for (;;) {
                int len = bis.read(buffer);
                if (len < 0)
                    break;
                bos.write(buffer, 0, len);
            }
        } finally {
            if (forceClose) {
                try {
                    bis.close();
                } catch (Exception e) {
                    // ignore
                }
                try {
                    bos.flush();
                    bos.close();
                } catch (Exception e) {
                    // ignore
                }
            }
        }
    }
}

Related

  1. readBytes(InputStream in, byte[] b)
  2. readBytes(InputStream in, byte[] buf, int length)
  3. readBytes(InputStream in, byte[] buffer)
  4. readBytes(InputStream in, byte[] buffer, int maxBytesAtOnce)
  5. readBytes(InputStream in, byte[] data, int offset, int length)
  6. readBytes(InputStream in, int bytesToRead, byte[] buffer, int bufferOffset)
  7. readBytes(InputStream in, int expectedBytes, long timeoutMs)
  8. readBytes(InputStream in, int len, boolean isLE)
  9. readBytes(InputStream in, int length)