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[] readNetWorkInputStream(InputStream in) {
    ByteArrayOutputStream os = null;
    try {// w ww.  jav  a  2  s .  co m
        os = new ByteArrayOutputStream();

        int readCount = 0;
        int len = 1024;
        byte[] buffer = new byte[len];
        while ((readCount = in.read(buffer)) != -1) {
            os.write(buffer, 0, readCount);
        }

        in.close();
        in = null;

        return os.toByteArray();

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != os) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            os = null;
        }
    }
    return null;
}

From source file:com.jaspersoft.jasperserver.api.metadata.common.service.impl.hibernate.persistent.ContentRepoFileResource.java

public static byte[] readFully(InputStream input) throws IOException {
    byte[] buffer = new byte[8192];
    int bytesRead;
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    while ((bytesRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
    }//from  w ww  .  j  a va 2  s.com
    return output.toByteArray();
}

From source file:Main.java

static public byte[] getBytes(File file) throws IOException {
    InputStream ios = null;/*from   ww w  . j a  va2s.co  m*/
    ByteArrayOutputStream ous = null;
    try {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(file);
        int read = 0;
        while ((read = ios.read(buffer)) != -1) {
            ous.write(buffer, 0, read);
        }
    } finally {
        try {
            if (ous != null)
                ous.close();
        } catch (IOException e) {
        }

        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }

    return ous.toByteArray();
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Determines if an InputStream is compressed. The java.util.zip GZip
 * implementation does not expose the GZip header so it is difficult to determine
 * if a string is compressed./*w  ww  .  j a v  a2s . c  o  m*/
 *
 * @param inputStream an array of bytes
 * @return true if the stream is compressed or false otherwise
 * @throws java.io.IOException if the byte array couldn't be read
 */
public static boolean isCompressed(InputStream inputStream) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    byte[] data = new byte[2];

    int nRead = inputStream.read(data, 0, 2);
    buffer.write(data, 0, nRead);
    buffer.flush();

    return isCompressed(buffer.toByteArray());
}

From source file:com.ticket.validation.terminal.restful.JsonObjectConverter.java

public static String inputSteamToString(InputStream is, String charset) throws IOException {
    final int BUFFER_SIZE = 1024;

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] data = new byte[BUFFER_SIZE];
    int count = -1;

    while ((count = is.read(data, 0, BUFFER_SIZE)) != -1)
        outStream.write(data, 0, count);

    data = null;//www .ja  v a 2s.  c  o m
    return new String(outStream.toByteArray(), charset);
}

From source file:com.useekm.types.AbstractGeo.java

public static byte[] gunzip(byte[] bytes) {
    try {//from ww  w.  j a v a  2 s.c  om
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bis));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0)
            bos.write(buf, 0, len);
        byte[] result = bos.toByteArray();
        bufis.close();
        bos.close();
        return result;
    } catch (IOException e) {
        throw new IllegalStateException("Unexpected IOException on inmemory gunzip", e);
    }
}

From source file:fr.mael.microrss.util.Tools.java

/**
 * Reads an image from an url and converts it to byte array
 * @param url//w w w.  j a v  a2s  . co  m
 * @return a byte array representing the image
 */
public static byte[] getImage(String url) {
    try {
        URL imgUrl = new URL(url);
        InputStream inputStream = imgUrl.openStream();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];

        int n = 0;
        while (-1 != (n = inputStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
        inputStream.close();

        byte[] data = output.toByteArray();
        return data;
    } catch (Exception e) {
        LOG.info("Error get image " + url, e);
    }
    return null;

}

From source file:Main.java

public static byte[] decompress(byte[] data, int off, int len) {
    byte[] output = null;
    Inflater decompresser = new Inflater();
    decompresser.reset();//from  www. ja va2 s. c o m
    //      decompresser.setInput(data);
    decompresser.setInput(data, off, len);

    ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
    try {
        byte[] buf = new byte[1024];
        while (!decompresser.finished()) {
            int i = decompresser.inflate(buf);
            o.write(buf, 0, i);
        }
        output = o.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            o.close();
            decompresser.end();
        } catch (Exception e) {
        }
    }

    return output;
}

From source file:Main.java

public static ByteBuffer fromStream(InputStream stream) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE);

    byte[] buffer = BUFFER_REF.getAndSet(null);
    if (buffer == null) {
        buffer = new byte[BUFFER_SIZE];
    }/*from  w w  w. j  a v  a 2s. c  o m*/

    int n = -1;
    while ((n = stream.read(buffer)) >= 0) {
        outStream.write(buffer, 0, n);
    }

    BUFFER_REF.set(buffer);

    byte[] bytes = outStream.toByteArray();

    // Some resource decoders require a direct byte buffer. Prefer allocateDirect() over wrap()
    return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0);
}

From source file:Main.java

public static byte[] compress(byte[] uncompressedBuffer) {
    Deflater deflater = new Deflater();
    deflater.setInput(uncompressedBuffer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length);

    try {//from www .  j  a va 2  s  .c  o  m
        deflater.finish();
        byte[] buffer = new byte[1024];
        while (!deflater.finished()) {
            int count = deflater.deflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        byte[] output = outputStream.toByteArray();
        return output;

    } finally {
        try {
            deflater.end();
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}