Example usage for java.io ByteArrayOutputStream close

List of usage examples for java.io ByteArrayOutputStream close

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closing a ByteArrayOutputStream has no effect.

Usage

From source file:Main.java

public static byte[] stream2ByteArray(InputStream inStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int len = 0;/*from   w  w  w.  ja va2s .  c  om*/
    byte[] buffer = new byte[1024 * 10];
    while ((len = inStream.read(buffer)) != -1) {
        baos.write(buffer, 0, len);
    }
    byte[] result = baos.toByteArray();
    baos.close();
    inStream.close();
    return result;
}

From source file:Main.java

/**
 * //from   w  ww . j a v  a 2  s . c  om
 * @param in
 * @return
 * @throws IOException
 */
public static String readFormStream(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    String result = out.toString();
    in.close();
    out.close();

    return result;
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) {
    byte[] b = null;
    try {//from   w ww  .  j  av a  2  s. co  m
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] unZip(byte[] data) {
    byte[] b = null;
    try {//from w  w  w .j  av a2s  .c o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ZipInputStream zip = new ZipInputStream(bis);
        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:com.philips.hsdp.feed.HelloWorldManager.java

public static String getStringFromHttpResponse(HttpResponse inResponse) {
    String responseString = null;
    try {/*from w ww .  ja  v  a  2s.c  o m*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        inResponse.getEntity().writeTo(out);
        out.close();
        responseString = out.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return responseString;
}

From source file:Main.java

private static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
    byte[] bytes = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte data[] = new byte[1024];
    int count;/*from   ww  w .  j a  v a 2 s .  c o  m*/
    while ((count = inputStream.read(data)) != -1) {
        bos.write(data, 0, count);
    }
    bos.flush();
    bos.close();
    inputStream.close();
    bytes = bos.toByteArray();
    return bytes;
}

From source file:Main.java

public static byte[] unZip(byte[] bContent) throws IOException {
    byte[] b = null;
    try {/*from  w  w w.  jav  a 2s  .  c  o  m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bContent);
        ZipInputStream zip = new ZipInputStream(bis);

        while (zip.getNextEntry() != null) {
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = zip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
        }
        zip.close();
        bis.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] fileToByte(String filePath) throws Exception {
    byte[] data = new byte[0];
    File file = new File(filePath);
    if (file.exists()) {
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
        byte[] cache = new byte[CACHE_SIZE];
        int nRead = 0;
        while ((nRead = in.read(cache)) != -1) {
            out.write(cache, 0, nRead);// w ww.  j  a va 2s  .  c  o  m
            out.flush();
        }
        out.close();
        in.close();
        data = out.toByteArray();
    }
    return data;
}

From source file:Main.java

public static byte[] compressZLIB(byte[] input) throws IOException {
    // Create the compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_SPEED);
    // Give the compressor the data to compress
    compressor.setInput(input);/*from  w ww. ja v  a2 s . co  m*/
    compressor.finish();
    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    bos.close();
    // Get the compressed data
    byte[] compressedData = bos.toByteArray();
    return compressedData;
}

From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.feature.meta.AbstractSequenceMetaDataFeatureGenerator.java

public static String encodeToString(Object object) throws TextClassificationException {
    try {//  w  w  w  .  j  av a2 s.  c o  m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

        objectOutputStream.writeObject(object);
        byteArrayOutputStream.close();

        return Base64.encodeBase64String(byteArrayOutputStream.toByteArray());
    } catch (IOException e) {
        throw new TextClassificationException(e);
    }
}