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[] decompress(byte[] compressedBuffer) {
    Inflater inflater = new Inflater();
    inflater.setInput(compressedBuffer);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(compressedBuffer.length);
    try {//  ww  w  . j a va2 s.com
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;
    } catch (DataFormatException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            inflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.amazonaws.client.util.sdk.IOUtils.java

/**
 * Reads and returns the rest of the given input stream as a byte array,
 * closing the input stream afterwards.//from   w ww . jav a2  s  . co  m
 */
public static byte[] toByteArray(InputStream is) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        byte[] b = new byte[BUFFER_SIZE];
        int n = 0;
        while ((n = is.read(b)) != -1) {
            output.write(b, 0, n);
        }
        return output.toByteArray();
    } finally {
        output.close();
    }
}

From source file:Main.java

protected static ByteArrayOutputStream inflate(final FileInputStream pInputStream) throws IOException {
    final ByteArrayOutputStream uncompressed = new ByteArrayOutputStream(ONE_MB);
    final GZIPInputStream zin = new GZIPInputStream(pInputStream);

    int read;//from www.  j a  va2  s  .  c  om
    final byte[] buf = new byte[ONE_MB];
    while ((read = zin.read(buf)) != -1) {
        uncompressed.write(buf, 0, read);
    }

    return uncompressed;
}

From source file:InsertClobToMySqlServlet.java

public static String getClobsContentAsString(String urlAsString) throws Exception {
    InputStream content = null;//from   w  w w . j a  v a2 s . c  o m
    try {
        URL url = new URL(urlAsString);
        URLConnection urlConn = url.openConnection();
        urlConn.connect();
        content = urlConn.getInputStream();

        int BUFFER_SIZE = 1024;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int length;
        byte[] buffer = new byte[BUFFER_SIZE];

        while ((length = content.read(buffer)) != -1) {
            output.write(buffer, 0, length);
        }
        return new String(output.toByteArray());
    } finally {
        content.close();
    }
}

From source file:Main.java

private static ByteArrayOutputStream readFileToStream(String file) {
    FileInputStream fileInputStream = null;
    try {/* ww w .jav  a 2s .com*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        fileInputStream = new FileInputStream(file);
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = fileInputStream.read(buf)) > 0) {
            bos.write(buf, 0, readedBytes);
        }
        return bos;
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (fileInputStream != null)
                fileInputStream.close();
        } catch (Exception ignored) {

        }
    }
}

From source file:Main.java

public static byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];

    int len = 0;//from w  w w .j  a  v  a2 s.c  o m
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }

    return byteBuffer.toByteArray();
}

From source file:Main.java

public static Bitmap loadBitmap(String url) {
    Log.d(TAG, "Start Load Url : " + url);
    //Bitmap bitmap = null;
    InputStream in = null;/*www  .ja v a 2s  . c  o m*/
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream());
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = in.read(data, 0, data.length)) != -1) {
            dataStream.write(data, 0, nRead);
        }
        dataStream.flush();
        final byte[] newData = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options);
    } catch (IOException e) {
        Log.e("My fault", "Could not load Bitmap from: " + url);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bitmapResult;
}

From source file:net.sf.keystore_explorer.utilities.io.ReadUtil.java

/**
 * Read all bytes from the supplied input stream. Closes the input stream.
 *
 * @param is/*from   w  w  w .j a  va  2  s  .  co  m*/
 *            Input stream
 * @return All bytes
 * @throws IOException
 *             If an I/O problem occurs
 */
public static byte[] readFully(InputStream is) throws IOException {
    ByteArrayOutputStream baos = null;

    try {
        baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[2048];
        int read = 0;

        while ((read = is.read(buffer)) != -1) {
            baos.write(buffer, 0, read);
        }

        return baos.toByteArray();
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(is);
    }
}

From source file:Main.java

public final static byte[] decompress(byte[] input) throws IOException {
    if (input == null || input.length == 0) {
        return input;
    }/*from  ww w.  ja  va2  s.  c  o  m*/
    Inflater inflator = new Inflater();
    inflator.setInput(input);
    ByteArrayOutputStream bin = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[BUFFER_SIZE];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bin.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new IOException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new IOException(t);
    } finally {
        inflator.end();
    }
    return bin.toByteArray();
}

From source file:Main.java

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);//w  w  w  . j  a va2 s .  com
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IOException(e.toString());
        }
    }
    bos.close();

    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();
    return decompressedData;
}