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

private static String readInStream(FileInputStream inStream) {
    try {//from w  ww. j a  v  a  2s . c om
        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) {
        // UIHelper.Log("e", "", "FileReadError", true);
    }
    return null;
}

From source file:fi.mystes.synapse.mediator.vfs.VFSTestHelper.java

public static void assertFileContentEquals(String path, String expectedContent) throws IOException {
    FileObject file = VFS.getManager().resolveFile(path);
    InputStream in = file.getContent().getInputStream();
    int length = 0;
    byte[] buffer = new byte[256];
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }//from   ww w  .  j a  v  a 2s  .c o m

    assertEquals("Unexpected file content", expectedContent, out.toString());
}

From source file:Main.java

public static byte[] readStreamContent(InputStream stream) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    while (true) {
        try {//  w w w.java2s  .c  o m
            int read = stream.read(buf);
            if (read == -1)
                break;
            bos.write(buf, 0, read);
        } catch (IOException e) {
            return null;
        }
    }
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*from   ww w  .ja  v  a2s. c  om*/
    inflater.finished();

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

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

From source file:Main.java

public static byte[] decompress(byte[] data) {
    byte[] output = new byte[0];

    Inflater decompresser = new Inflater();
    decompresser.reset();/*ww w  .j  a  v  a  2 s  .  c  om*/
    decompresser.setInput(data);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(2 * data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int length = decompresser.inflate(buf);
            bos.write(buf, 0, length);
        }
        output = bos.toByteArray();
        bos.close();
    } catch (Exception e) {
        output = data;
        e.printStackTrace();
    }

    decompresser.end();
    return output;
}

From source file:FileUtil.java

public static byte[] readFileAsBytes(String path, Integer start, Integer length) {

    byte[] byteData = null;

    try {/*from   w w  w  .j a  v a 2  s . c o  m*/

        File file = new File(path);

        DataInputStream dis;
        dis = new DataInputStream(new FileInputStream(file));

        if (dis.available() > Integer.MAX_VALUE) {
            System.out.println("dis.available() > Integer.MAX_VALUE");
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream(length);
        byte[] bytes = new byte[length];

        dis.skipBytes(start);
        int readBytes = dis.read(bytes, 0, length);
        os.write(bytes, 0, readBytes);

        byteData = os.toByteArray();

        dis.close();
        os.close();

    } catch (Exception e) {
        System.out.println(e);
    }

    return byteData;
}

From source file:com.net.plus.common.Utils.Util.java

public static byte[] getBytesFromFile(String file) {
    if (file == null) {
        return null;
    }/*from  w w w  .ja v a 2  s.co  m*/
    byte[] ret = null;
    FileInputStream in = null;
    try {
        in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
        byte[] b = new byte[4096];
        int n;
        while ((n = in.read(b)) != -1) {
            out.write(b, 0, n);
        }
        ret = out.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

            }
        }
    }
    return ret;
}

From source file:eu.planets_project.pp.plato.util.FileUtils.java

public static byte[] inputStreamToBytes(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;// w  w  w .ja v  a2 s .c  om

    while ((len = in.read(buffer)) >= 0)
        out.write(buffer, 0, len);

    in.close();
    out.close();
    return out.toByteArray();
}

From source file:co.marcin.novaguilds.util.IOUtils.java

public static String toString(InputStream in, String encoding) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    int len;/*from w w w . j a  v  a 2  s.  co m*/

    while ((len = in.read(buf)) != -1) {
        byteArrayOutputStream.write(buf, 0, len);
    }

    return new String(byteArrayOutputStream.toByteArray(), encoding);
}

From source file:Main.java

public static String readFile(String path) {
    try {//from   w  w  w  .  j  a v a2s.  c o  m
        FileInputStream in = new FileInputStream(path);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte buffer[] = new byte[1024];
        int count = 0;

        while ((count = in.read(buffer)) != -1) {
            if (out != null) {
                out.write(buffer, 0, count);
            }
        }

        return out.toString();

    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: " + path, e);

    } catch (IOException e) {
        Log.e(TAG, "Error while reading file: " + path, e);
    }

    return null;
}