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:ch.rgw.compress.CompEx.java

public static byte[] CompressBZ2(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    baos.write(buf, 0, 4); // Lnge des Originalstroms
    CBZip2OutputStream bzo = new CBZip2OutputStream(baos);
    int l;/*from   w w w.  ja  va2  s  .c om*/
    int total = 0;
    ;
    while ((l = in.read(buf, 0, buf.length)) != -1) {
        bzo.write(buf, 0, l);
        total += l;
    }
    bzo.close();
    byte[] ret = baos.toByteArray();
    // Die hchstwertigen 3 Bit als Typmarker setzen
    total &= 0x1fffffff;
    total |= BZIP2;
    BinConverter.intToByteArray(total, ret, 0);
    return ret;
}

From source file:com.example.gauth.GAuthTask.java

/**
 * Reads the response from the input stream and returns it as a string.
 *//*from ww w .ja v  a  2  s.co m*/
private static String readResponse(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] data = new byte[2048];
    int len;
    while ((len = is.read(data, 0, data.length)) >= 0) {
        bos.write(data, 0, len);
    }
    return new String(bos.toByteArray(), "UTF-8");
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] CompressZIP(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    baos.write(buf, 0, 4); // Lnge des Originalstroms
    ZipOutputStream zo = new ZipOutputStream(baos);
    zo.putNextEntry(new ZipEntry("Data"));
    int l;/* w w  w.ja  v  a2s . com*/
    long total = 0;
    ;
    while ((l = in.read(buf, 0, buf.length)) != -1) {
        zo.write(buf, 0, l);
        total += l;
    }
    zo.close();
    byte[] ret = baos.toByteArray();
    // Die hchstwertigen 3 Bit als Typmarker setzen
    total &= 0x1fffffff;
    total |= ZIP;
    BinConverter.intToByteArray((int) total, ret, 0);
    return ret;
}

From source file:Main.java

public static String loadImageFromUrl(Context context, String imageURL, File file) {
    try {/*from  www  . j  av a2  s  . co  m*/
        URL url = new URL(imageURL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(5 * 1000);
        con.setDoInput(true);
        con.connect();
        if (con.getResponseCode() == 200) {
            InputStream inputStream = con.getInputStream();

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024 * 20];
            int length = -1;
            while ((length = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, length);
            }
            byteArrayOutputStream.close();
            inputStream.close();

            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
            return file.getPath();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:Main.java

public static byte[] toByteArray(InputStream istream) {
    ByteArrayOutputStream byteostream = null;
    try {//from  w w w.  j  a v a  2 s . c  o m
        byteostream = new ByteArrayOutputStream(8192);
        byte[] buffer = new byte[8192];
        int lenght;
        while ((lenght = istream.read(buffer)) != -1) {
            byteostream.write(buffer, 0, lenght);
        }
        return byteostream.toByteArray();
    } catch (Exception e) {
        return null;
    } finally {
        try {
            istream.close();
            byteostream.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static byte[] zipDecompress(byte[] input) throws IOException {
    Inflater inflator = new Inflater();
    inflator.setInput(input);//from www  . j  a  v  a  2  s  . c o  m
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    byte[] buf = new byte[1024];
    try {
        while (true) {
            int count = inflator.inflate(buf);
            if (count > 0) {
                bos.write(buf, 0, count);
            } else if (count == 0 && inflator.finished()) {
                break;
            } else {
                throw new RuntimeException("bad zip data, size:" + input.length);
            }
        }
    } catch (DataFormatException t) {
        throw new RuntimeException(t);
    } finally {
        inflator.end();
    }
    return bos.toByteArray();
}

From source file:Main.java

public static String readAssets(Context context, String fileName) {
    InputStream is = null;/*from w ww . j  a  v a 2  s.  c o  m*/
    String content = null;
    try {
        is = context.getAssets().open(fileName);
        if (is != null) {

            byte[] buffer = new byte[1024];
            ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
            while (true) {
                int readLength = is.read(buffer);
                if (readLength == -1)
                    break;
                arrayOutputStream.write(buffer, 0, readLength);
            }
            is.close();
            arrayOutputStream.close();
            content = new String(arrayOutputStream.toByteArray());

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        content = null;
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return content;
}

From source file:com.hdezninirola.auth.AbstractGetNameTask.java

/**
 * Reads the response from the input stream and returns it as a string.
 *//*w  w w . j a v  a 2 s.  c  o m*/
private static String readResponse(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] data = new byte[2048];
    int len = 0;
    while ((len = is.read(data, 0, data.length)) >= 0) {
        bos.write(data, 0, len);
    }
    System.out.println(new String(bos.toByteArray(), "UTF-8"));
    return new String(bos.toByteArray(), "UTF-8");
}

From source file:Main.java

public static byte[] compress(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setInput(data);/*from w  w w .j a  v  a  2s .c  o m*/
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        deflater.end();
    }

}

From source file:fedora.utilities.Base64.java

/**
 * Encodes an input stream to base64, returning bytes.
 * <p>//ww  w. j  a v a 2  s.co  m
 * The stream is guaranteed to be closed when this method returns, whether
 * successful or not.
 *
 * @param in stream to encode
 * @param encoded bytes, or null if there's an error reading the stream
 */
public static byte[] encode(InputStream in) {
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        return encode(out.toByteArray());
    } catch (IOException e) {
        return null;
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            throw new FaultException(e);
        }
    }
}