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[] inputStreamToByte(InputStream in) throws IOException {

    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;//from   ww w  .j  a  v a2s  . c  o  m
    return outStream.toByteArray();
}

From source file:Main.java

/**
 * Saves a file from the given URL using HTTPS to the given filename and returns the file
 * @param link URL to file//from   ww  w  .  j  a va2  s .c o  m
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static void saveFileFromNetHTTPS(URL link, String fileName) throws IOException {
    HttpsURLConnection con = (HttpsURLConnection) link.openConnection();

    InputStream in = new BufferedInputStream(con.getInputStream());
    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 f = new File(fileName);
    if (f.getParentFile() != null) {
        if (f.getParentFile().mkdirs()) {
            System.out.println("Created Directory Structure");
        }
    }

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(response);
    fos.close();
}

From source file:Main.java

private static byte[] getBytes(InputStream is) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] b = new byte[2048];
    int len = 0;/*from  w  ww .ja  v a  2  s  . c om*/
    try {
        while ((len = is.read(b, 0, 2048)) != -1) {
            baos.write(b, 0, len);
            baos.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] bytes = baos.toByteArray();
    return bytes;
}

From source file:Main.java

public static String inputStream2String(InputStream inputStream) throws Exception {
    if (inputStream == null) {
        return null;
    }//from  ww w  .  jav a  2s . c  o  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count = 0;
    while ((count = inputStream.read(buffer)) >= 0) {
        outputStream.write(buffer, 0, count);
    }
    String convertedBuffer = new String(outputStream.toByteArray());
    outputStream.close();
    return convertedBuffer;
}

From source file:Main.java

/**
 * Write stream to bytes array.//from   w  w  w.  j  a  v a 2s. co m
 *
 * @param source stream
 * @return bytes array
 */
public static byte[] streamToBytes(InputStream source) {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int l;
    try {
        while ((l = source.read(buffer)) >= 0) {
            result.write(buffer, 0, l);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return result.toByteArray();
}

From source file:Main.java

/**
 * Write the contents of an input stream into a byte array.
 * @param inputStream The stream to convert into a byte array.
 * @return A byte array containing the contents of the input stream.
 * @throws IOException/*  w w w.  j a v  a2  s . com*/
 */
private static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte data[] = new byte[1024];
    int count;
    while ((count = inputStream.read(data)) != -1) {
        bos.write(data, 0, count);
    }
    bos.flush();
    bos.close();
    inputStream.close();
    bytes = bos.toByteArray();
    return bytes;
}

From source file:Main.java

public static byte[] getBytes(String filePath) {
    byte[] buffer = null;
    try {/* www.j  av  a2 s .c  om*/
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        byte[] b = new byte[1000];
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buffer;
}

From source file:Main.java

private static byte[] readStreamToEnd(final InputStream is) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    if (is != null) {
        final byte[] buff = new byte[1024];
        int read;
        do {// ww  w  .  j av a  2  s .co  m
            bos.write(buff, 0, (read = is.read(buff)) < 0 ? 0 : read);
        } while (read >= 0);
        is.close();
    }
    return bos.toByteArray();
}

From source file:Main.java

private static byte[] readInputStream(InputStream inStream) throws Exception {
    if (inStream != null) {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }//from w w w  .j av a  2  s.  com
        inStream.close();
        return outStream.toByteArray();
    }
    return null;
}

From source file:Main.java

public static String readInputStream(InputStream is) {
    try {/*from  ww w  .  j  a v a  2s.c o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
        baos.close();
        byte[] result = baos.toByteArray();
        String temp = new String(result);
        return temp;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}