Java ByteArrayOutputStream Write load(InputStream in, byte[] buffer, int offset, int initialBufferSize)

Here you can find the source of load(InputStream in, byte[] buffer, int offset, int initialBufferSize)

Description

load

License

Open Source License

Declaration

public static byte[] load(InputStream in, byte[] buffer, int offset, int initialBufferSize) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public final static byte[] load(String fileName) {
        try {/*from ww  w.  j ava 2s . c  o  m*/
            FileInputStream fin = new FileInputStream(fileName);
            return load(fin);
        } catch (Exception e) {

            return new byte[0];
        }
    }

    public final static byte[] load(File file) {
        try {
            long fileLength = file.length();
            if (fileLength > Integer.MAX_VALUE) {
                throw new IOException("File '" + file.getName() + "' too big");
            }

            FileInputStream fin = new FileInputStream(file);
            return load(fin);
        } catch (Exception e) {

            return new byte[0];
        }
    }

    public static byte[] load(InputStream in) throws IOException {
        return load(in, 4096);
    }

    public final static byte[] load(FileInputStream fin) {
        byte readBuf[] = new byte[512 * 1024];

        try {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();

            int readCnt = fin.read(readBuf);
            while (0 < readCnt) {
                bout.write(readBuf, 0, readCnt);
                readCnt = fin.read(readBuf);
            }

            fin.close();

            return bout.toByteArray();
        } catch (Exception e) {

            return new byte[0];
        }
    }

    public static byte[] load(InputStream in, byte[] buffer, int offset, int initialBufferSize) throws IOException {
        if (initialBufferSize == 0) {
            initialBufferSize = 1;
        }
        if (buffer.length < offset + initialBufferSize) {
            initialBufferSize = buffer.length - offset;
        }
        for (;;) {
            int numRead = in.read(buffer, offset, initialBufferSize);
            if (numRead == -1 || numRead == initialBufferSize) {
                break;
            }
            offset += numRead;
        }
        if (offset < buffer.length) {
            byte[] newBuffer = new byte[buffer.length];
            System.arraycopy(buffer, offset, newBuffer, offset, initialBufferSize);
            buffer = newBuffer;
        }
        return buffer;
    }

    public static byte[] load(InputStream in, int initialBufferSize) throws IOException {
        if (initialBufferSize == 0) {
            initialBufferSize = 1;
        }
        byte[] buffer = new byte[initialBufferSize];
        int offset = 0;
        for (;;) {
            int remain = buffer.length - offset;
            if (remain <= 0) {
                int newSize = buffer.length * 2;
                byte[] newBuffer = new byte[newSize];
                System.arraycopy(buffer, 0, newBuffer, 0, offset);
                buffer = newBuffer;
                remain = buffer.length - offset;
            }
            int numRead = in.read(buffer, offset, remain);
            if (numRead == -1) {
                break;
            }
            offset += numRead;
        }
        if (offset < buffer.length) {
            byte[] newBuffer = new byte[offset];
            System.arraycopy(buffer, 0, newBuffer, 0, offset);
            buffer = newBuffer;
        }
        return buffer;
    }
}

Related

  1. load(final File file, final String encoding)
  2. load(final String resource, final byte[] defaultResult)
  3. load(InputStream inputStream, Class type)
  4. load(InputStream is)
  5. load(InputStream is, boolean close)
  6. load(String fileName)