Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream write.

Prototype

public synchronized void write(byte b[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] extract(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int read;/*from   www.j a  v  a  2  s .  c om*/
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, read);
    }
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] readInputStream(InputStream inStream) throws IOException {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[BUFFER_SIZE];
    int len = 0;/*from   w w  w.  ja  va 2s  . com*/
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);
    }
    outSteam.close();
    inStream.close();
    return outSteam.toByteArray();
}

From source file:FileUtil.java

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

    try {// w  ww .j av  a 2  s.  c  o  m
        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];
    }
}

From source file:Main.java

public static byte[] readStreamToBytes(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] byteArray = new byte[BYTE_ARRAY_SIZE];
    int length = 0;
    while ((length = is.read(byteArray)) != -1) {
        bos.write(byteArray, 0, length);
    }/*from  www .  j a v  a  2 s . c  om*/
    bos.close();
    is.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] extract(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int read = 0;
    while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) {
        baos.write(buffer, 0, read);
    }// www  . ja  va2s.c  o  m
    baos.flush();
    return baos.toByteArray();
}

From source file:Main.java

public static String inToString(InputStream in) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[BUFFER_SIZE];
    int count = -1;
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
        outStream.write(data, 0, count);

    data = null;//w ww  . j a  va  2s . c o m
    return new String(outStream.toByteArray());
}

From source file:Main.java

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file/* ww w .j a  v  a2s .com*/
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:Utils.java

public static byte[] getBytes(InputStream is) throws IOException {

    int len;//w  w w .j a  va2s. c o  m
    int size = 1024;
    byte[] buf;

    if (is instanceof ByteArrayInputStream) {
        size = is.available();
        buf = new byte[size];
        len = is.read(buf, 0, size);
    } else {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        buf = new byte[size];
        while ((len = is.read(buf, 0, size)) != -1)
            bos.write(buf, 0, len);
        buf = bos.toByteArray();
    }
    return buf;
}

From source file:Main.java

static private byte[] readBytesFromStream(InputStream input) throws IOException {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;/*from w ww .ja v  a  2  s.co m*/
    byte[] data = new byte[16384];

    while ((nRead = input.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    input.close();
    return buffer.toByteArray();
}

From source file:Main.java

public static String decompress(byte[] bytes) {
    try {/*from www  . j  av  a 2 s.c  om*/
        InputStream in = new GZIPInputStream(new ByteArrayInputStream(bytes));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[262144]; // about 300kb
        int len;
        while ((len = in.read(buffer)) > 0)
            baos.write(buffer, 0, len);
        return new String(baos.toByteArray(), "UTF-8");
    } catch (Exception e) {
        return "";
    }
}