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:org.messic.server.api.musicinfo.duckduckgoimages.MusicInfoDuckDuckGoImages.java

/**
 * Read an inputstream and return a byte[] with the whole content of the readed at the inputstream
 * //  w w  w . j  a v a 2 s  .c  o m
 * @param is {@link InputStream}
 * @return byte[] content
 * @throws IOException
 */
public static byte[] readInputStream(InputStream is) throws IOException {
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int cant = is.read(buffer);
    while (cant > 0) {
        baos.write(buffer, 0, cant);
        cant = is.read(buffer);
    }

    return baos.toByteArray();
}

From source file:ByteUtils.java

public static byte[] unpackRaw(byte[] b) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteArrayInputStream bais = new ByteArrayInputStream(b);

    GZIPInputStream zis = new GZIPInputStream(bais);
    byte[] tmpBuffer = new byte[256];
    int n;//from ww  w .j av a2  s  . c  om
    while ((n = zis.read(tmpBuffer)) >= 0)
        baos.write(tmpBuffer, 0, n);
    zis.close();

    return baos.toByteArray();
}

From source file:br.com.manish.ahy.kernel.util.JPAUtil.java

public static byte[] blobToBytes(Blob blob) {
    byte[] byteData = null;
    try {/*from w ww .ja  v  a 2 s .co m*/
        InputStream is = blob.getBinaryStream();
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] bytes = new byte[512];

        int readBytes;
        while ((readBytes = is.read(bytes)) > 0) {
            os.write(bytes, 0, readBytes);
        }

        byteData = os.toByteArray();

        is.close();
        os.close();
    } catch (Exception e) {
        throw new OopsException(e, "Error reading file data.");
    }
    return byteData;
}

From source file:Main.java

public static String readFile(Context context, String fileName) {
    if (!exists(context, fileName)) {
        return null;
    }/*w  w  w.  j  av  a  2  s  .c o  m*/
    FileInputStream fis = null;
    String content = null;
    try {
        fis = context.openFileInput(fileName);
        if (fis != null) {

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

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

From source file:Main.java

public static String readFile(String filePath) {
    if (filePath == null || !new File(filePath).exists()) {
        return null;
    }/*from w  w w .  j  av a2s .  com*/
    FileInputStream fis = null;
    String content = null;
    try {
        fis = new FileInputStream(filePath);
        if (fis != null) {

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

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

From source file:Main.java

public static byte[] unzip(byte[] output) {
    if (output == null || output.length == 0) {
        return output;
    }/* w ww.  j  a  v a2 s.  co  m*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(output);
        GZIPInputStream gunzip = new GZIPInputStream(in);
        byte[] buffer = new byte[BUFFER_SIZE];
        int ret;
        while ((ret = gunzip.read(buffer)) >= 0) {
            out.write(buffer, 0, ret);
        }
        gunzip.close();
        return out.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Read all from InputStream/*from w  w  w  .j  av a  2 s  .co  m*/
 * @param is Stream to read from
 * @param size Guess the size of InputStream contents, give negative for the automatic
 * @return
 */
public static String readFromInputStream(InputStream is, int size) {
    try {
        ByteArrayOutputStream os;
        if (size <= 0) {
            os = new ByteArrayOutputStream();
        } else {
            os = new ByteArrayOutputStream(size);
        }
        byte buffer[] = new byte[4096];
        int len;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        return os.toString("UTF-8");
    } catch (IOException e) {
        Log.e("SOAP", "readFromInputStream", e);
        return "";
    }
}

From source file:com.adito.security.pki.SshKeyPairFactory.java

public static SshPrivateKey decodePrivateKey(InputStream in) throws InvalidKeyException, IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[4096];
    int read;//from w w  w.  j  a v  a  2 s. c o m

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

    return decodePrivateKey(out.toByteArray());

}

From source file:com.adito.security.pki.SshKeyPairFactory.java

public static SshPublicKey decodePublicKey(InputStream in) throws InvalidKeyException, IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[4096];
    int read;/* w  w w  .ja  v a 2s  .c  o  m*/

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

    return decodePublicKey(out.toByteArray());

}

From source file:nl.knaw.dans.easy.sword2examples.Common.java

private static byte[] readChunk(InputStream is, int size) throws Exception {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] bytes = new byte[size];
    int c = is.read(bytes);
    bos.write(bytes, 0, c);
    return bos.toByteArray();
}