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:com.honnix.yaacs.util.StreamUtil.java

public static byte[] inputStream2ByteArray(InputStream inputStream) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[255];
    byte[] rawData = null;

    try {//from ww w .j a  v a 2  s .co  m
        int length = -1;

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

        rawData = baos.toByteArray();
    } catch (IOException e) {
        LOG.error("Error reading input album cover data.", e);

        rawData = new byte[0];
    }

    return rawData;
}

From source file:Main.java

/**
 * Compress the byte array passed/*from  w  w w  .  j av  a  2s .com*/
 * <p>
 * @param input byte array
 * @param bufferLength buffer length
 * @return compressed byte array
 * @throws IOException thrown if we can't close the output stream
 */
public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException {
    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[bufferLength];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

    // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ )
    compressor.end();
    bos.close();

    // Get the compressed data
    return bos.toByteArray();

}

From source file:com.eryansky.common.utils.io.IoUtils.java

public static byte[] readInputStream(InputStream inputStream, String inputStreamName) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[16 * 1024];
    try {//  ww w. j a v  a2s  .  c om
        int bytesRead = inputStream.read(buffer);
        while (bytesRead != -1) {
            outputStream.write(buffer, 0, bytesRead);
            bytesRead = inputStream.read(buffer);
        }
    } catch (Exception e) {
        //      throw new ServiceException("couldn't read input stream "+inputStreamName, e);
    }
    return outputStream.toByteArray();
}

From source file:net.heroicefforts.viable.android.rep.it.auth.Authenticate.java

private static String readResponse(HttpResponse response) throws IOException {
    InputStream instream = response.getEntity().getContent();
    Header contentEncoding = response.getFirstHeader("Content-Encoding");
    if (Config.LOGV) //NOPMD
        if (contentEncoding != null) //NOPMD
            Log.v(TAG, "Response content encoding was '" + contentEncoding.getValue() + "'");
    if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
        if (Config.LOGD)
            Log.d(TAG, "Handling GZIP response.");
        instream = new GZIPInputStream(instream);
    }//from  w ww . j av a  2s .  c  om

    BufferedInputStream bis = new BufferedInputStream(instream);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int read = 0;
    while ((read = bis.read(buf)) > 0)
        baos.write(buf, 0, read);
    String body = baos.toString();
    if (Config.LOGV)
        Log.v(TAG, "Response:  " + body);
    return body;
}

From source file:brickhouse.udf.bloom.BloomFactory.java

public static Filter ReadBloomFromStream(InputStream stream) throws IOException {
    /// Need to UUDecode first, 
    /// TODO - read bytes directly when hive handles byte arrays better
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    byte[] bufferArr = new byte[4096];
    int len = 0;/* w w  w.  ja  v  a2 s .c o m*/
    while ((len = stream.read(bufferArr, 0, 4096)) > 0) {
        buffer.write(bufferArr, 0, len);
    }
    if (buffer.size() == 0) {
        return BloomFactory.NewBloomInstance();
    }
    return ReadBloomFromString(new String(buffer.toByteArray()));
}

From source file:Main.java

public static final byte[] uncompress(final byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }/*from  ww w.j  a  v  a 2  s.c om*/
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    byte[] buffer = new byte[bytes.length];
    int length;
    try {
        GZIPInputStream gis = new GZIPInputStream(bais);
        while ((length = gis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        byte[] moreBytes = bos.toByteArray();
        bos.close();
        bais.close();
        gis.close();
        bos = null;
        bais = null;
        gis = null;
        return moreBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] inflate(InputStream in, int bufferSize) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(bufferSize);

    InflaterInputStream inStream = new InflaterInputStream(in);

    byte[] buf = new byte[4096];
    while (true) {
        int size = inStream.read(buf);
        if (size <= 0)
            break;
        outStream.write(buf, 0, size);
    }//  w w  w . j av a  2 s .c  o m
    outStream.close();

    return outStream.toByteArray();
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {/*from   ww  w .j  av a 2s.c o m*/
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:Main.java

/**
 * File to Base64/*from ww w  .j  ava  2s .c om*/
 * @param filePath
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String getBase64StringFromFile(String filePath) throws FileNotFoundException, IOException {
    byte[] buffer = null;
    File file = new File(filePath);
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    byte[] b = new byte[1000];
    try {
        fis = new FileInputStream(file);
        bos = new ByteArrayOutputStream(1000);
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    return Base64.encodeToString(buffer, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] getBytesFromFile(File f) {

    if (f == null) {
        return null;
    }//ww w .  j ava2  s  .  co m

    FileInputStream stream = null;
    ByteArrayOutputStream out = null;

    try {
        stream = new FileInputStream(f);
        out = new ByteArrayOutputStream(1024);
        byte[] b = new byte[1024];
        int n;
        while ((n = stream.read(b)) != -1)
            out.write(b, 0, n);

    } catch (Exception e) {
    }

    try {
        if (out != null)
            out.close();

        if (stream != null)
            stream.close();
    } catch (Exception e) {
    }

    System.gc();

    if (out == null)
        return null;

    return out.toByteArray();
}