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[] fetchUrlToByte(String urlStr) {

    try {//from  www .java 2s .  co m
        URL url = new URL(urlStr);
        URLConnection urlConnection = url.openConnection();
        InputStream is = urlConnection.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int len = 0;

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

        return baos.toByteArray();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String uncompress(byte[] data) throws IOException, DataFormatException {

    Inflater inflater = new Inflater();
    inflater.setInput(data);//w w  w.  j a va 2 s .  co  m
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    // Decode the bytes into a String
    return new String(output, 0, output.length, "UTF-8");
}

From source file:Main.java

public static String getStringFromResRaw(int iResId, Resources resources) {
    InputStream inputStream = resources.openRawResource(iResId);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    int iLen = -1;
    byte[] buffer = new byte[512];
    try {/*from www  .ja  v  a  2s  .c  o m*/
        while (-1 != (iLen = inputStream.read(buffer)))
            outputStream.write(buffer, 0, iLen);
        inputStream.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

private static byte[] getByteArrayFromInputStream(InputStream is) throws IOException {
    // byte[] byteArray = null;
    ///*from w w w  .ja v a 2s  .  c o  m*/
    // byteArray = new byte[is.available()];
    // is.read(byteArray);
    // return byteArray;
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

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

    buffer.flush();

    return buffer.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*from  w  w w. j a v  a  2 s.c om*/
    inflater.finished();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();

    byte[] output = outputStream.toByteArray();
    inflater.end();
    return output;
}

From source file:Main.java

/**
 * DEFLATEs the specified input data./*from  w  w w . j a  v  a  2  s  .co  m*/
 * 
 * @param data the input data
 * @param dictionary the dictionary, or null if none
 * @return the compressed data
 */
public static byte[] deflate(byte[] data, byte[] dictionary) {
    Deflater deflater = new Deflater(8, true);
    if (dictionary != null) {
        deflater.setDictionary(dictionary);
    }
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[256];
    while (!deflater.finished()) {
        int n = deflater.deflate(buffer);
        byteArrayOutputStream.write(buffer, 0, n);
    }
    byte[] result = byteArrayOutputStream.toByteArray();
    return result;
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException, DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*  www. j  av  a2 s  . c o m*/
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    byte[] buffer = new byte[1024];
    while (!inflater.finished()) {
        int count = inflater.inflate(buffer);
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();
    /*
    LOG.debug("Original: " + data.length);
    LOG.debug("Compressed: " + output.length);
     */
    return output;
}

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  a  2  s .  co m*/
    while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
    }
    return byteBuffer.toByteArray();
}

From source file:Main.java

public static byte[] loadImageDataFromWeb(String url) {

    byte[] imageData = null;
    try {/*from ww w.j ava  2s  . c  o  m*/

        URLConnection connection = new URL(url).openConnection();
        InputStream stream = connection.getInputStream();
        //BufferedInputStream in=new BufferedInputStream(stream);//default 8k buffer
        BufferedInputStream in = new BufferedInputStream(stream, 10240);//YG: 10k=10240, 2x8k=16384
        ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
        int read;
        byte[] b = new byte[4096];

        while ((read = in.read(b)) != -1) {
            out.write(b, 0, read);
        }

        out.flush();
        out.close();

        imageData = out.toByteArray();

    } catch (Exception e) {

        Log.w(TAG, "Exc=" + e);
        return null;
    }

    return imageData;
}

From source file:Main.java

private static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;/*from   w  ww .j a v a2 s .  com*/
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        Log.i("IO error", e.getMessage());
        return "Sorry, help file not found.";
    }
    return outputStream.toString();
}