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 ByteArrayOutputStream inputStreamToByteArrayOutputStream(InputStream inputStream)
        throws IOException {
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    if (inputStream != null) {
        int readBytes = 0;
        byte[] sBuffer = new byte[4096];
        while ((readBytes = inputStream.read(sBuffer)) != -1) {
            content.write(sBuffer, 0, readBytes);
        }/*from w  w w  . j  a v  a2s. c  om*/
    }
    return content;
}

From source file:Main.java

/**
 * Fully reads the given InputStream and returns it as a byte array
 *
 * @param inputStream The input stream to read
 * @return The byte array containing the data from the input stream
 * @throws IOException If an I/O error occurs
 */// w w w . j a v  a2 s. co m
public static byte[] toByteArray(InputStream inputStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[8192];
    while (true) {
        int read = inputStream.read(buffer);
        if (read == -1) {
            break;
        }
        baos.write(buffer, 0, read);
    }
    return baos.toByteArray();
}

From source file:Main.java

public static byte[] replaceAll(byte[] src, byte[] target, byte[] replacement) {
    final ByteArrayOutputStream result = new ByteArrayOutputStream(src.length);
    int i = 0;/* w w  w.  j  a v a2 s  .co m*/
    int wrote = 0;
    while ((i = indexOf(src, target, i)) != -1) {
        int len = i - wrote;
        result.write(src, wrote, len);
        result.write(replacement, 0, replacement.length);
        wrote += len + target.length;
        i += target.length;
    }
    result.write(src, wrote, src.length - wrote);
    return result.toByteArray();
}

From source file:Main.java

/**
 * Converts an inputstream to a byte array (Mostly useful for sending images via JSON)
 * @param is Input stream, if using a URI, open it by calling:
 *     InputStream iStream = context.getContentResolver().openInputStream(uri);
 * @return Byte Array/*from   w w  w  .  j av  a 2s  . c  om*/
 */
public static byte[] getBytes(InputStream is) {
    try {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

private static byte[] getBytesFromDriveVideoUri(Context context, Uri uri) {
    InputStream inputStream = null;
    try {/*  www  . j  av  a 2s .  c  o m*/
        inputStream = context.getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    if (inputStream == null) {
        return null;
    }

    int maxBufferSize = 1024 * 1024;
    int available = 0;
    try {
        available = inputStream.available();
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (available == 0) {
        available = maxBufferSize;
    }

    int bufferSize = Math.min(available, maxBufferSize);

    byte[] data = new byte[bufferSize];
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;

    try {
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        buffer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return buffer.toByteArray();
}

From source file:Main.java

public static byte[] zlibDecompress(InputStream is) {
    if (null == is)
        return null;
    InflaterInputStream iis = new InflaterInputStream(is);
    ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
    try {/*from  w w w  .  j av  a2  s.c o  m*/
        int i = 1024;
        byte[] buf = new byte[i];

        while ((i = iis.read(buf, 0, i)) > 0) {
            o.write(buf, 0, i);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return o.toByteArray();
}

From source file:Main.java

public static byte[] compressZLIB(byte[] input) throws IOException {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    // Give the compressor the data to compress
    compressor.setInput(input);/*from  ww w. j a  v  a  2s . co  m*/
    compressor.finish();
    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    // Get the compressed data
    byte[] compressedData = bos.toByteArray();
    return compressedData;
}

From source file:Main.java

public static byte[] compress(byte[] input) {
    if (input == null || input.length == 0)
        return input;
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*from w  ww.  j  a  v a 2  s.c om*/
    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[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }

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

From source file:Main.java

public static byte[] InputStram2byteArray(InputStream in) {
    try {//from  ww  w.j  a v  a  2s.c  o  m
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (in != null) {
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
            }
            out.close();
            in.close();
            return out.toByteArray();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

private static String readInStream(FileInputStream inStream) {
    try {/* w w  w  . j av  a  2 s . c  o m*/
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length = -1;
        while ((length = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }

        outStream.close();
        inStream.close();
        return outStream.toString();
    } catch (IOException e) {
        Log.i("FileTest", e.getMessage());
    }
    return null;
}