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[] readStream(InputStream inStream) throws Exception {
    byte[] buffer = new byte[1024];
    int len = -1;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }//from   w ww  .java2 s .c  om
    byte[] data = outStream.toByteArray();
    outStream.close();
    inStream.close();
    return data;

}

From source file:com.github.carlomicieli.nerdmovies.utility.ImageUtils.java

private static byte[] convertToArray(BufferedImage image, String contentType) throws IOException {
    byte[] imageInByte;

    String typeName = "jpg";
    if (contentType.equals(MediaType.IMAGE_PNG))
        typeName = "png";

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, typeName, baos);
    baos.flush();//  w w  w.  j av  a 2  s  .c  o m
    imageInByte = baos.toByteArray();
    baos.close();

    return imageInByte;
}

From source file:Main.java

static public byte[] gunzip(byte src[], byte default_value[]) {
    try {// w w w  . ja  v  a  2 s . c  o  m
        if (src == null)
            return default_value;

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(src));

        IOUtils.copy(in, out);

        in.close();
        out.close();

        return out.toByteArray();
    } catch (Exception e) {
        return default_value;
    }
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;//from w  w  w  .  j av a  2  s . c o m
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    return outStream.toByteArray();
}

From source file:Main.java

static byte[] compress(byte[] decompressed) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(decompressed);//from w ww  .  j a  v a 2s . c om
    gzip.flush();
    gzip.close();
    bos.flush();
    bos.close();
    return bos.toByteArray();
}

From source file:at.asitplus.regkassen.demo.testsuites.TestSuiteGenerator.java

public static List<CashBoxSimulation> getSimulationRuns() {
    try {//ww w.  jav  a  2s.  c  o m
        //define the to-be-executed test suites
        List<String> testSuites = new ArrayList<>();
        testSuites.add("TESTSUITE_TEST_SZENARIO_1.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_2.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_3.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_4.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_5.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_6.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_7.json");
        testSuites.add("TESTSUITE_TEST_SZENARIO_8.json");

        //prepare cashbox simulation files
        List<CashBoxSimulation> cashBoxSimulationList = new ArrayList<>();
        for (String testSuiteIdentifier : testSuites) {
            //load from file system
            InputStream inputStream = TestSuiteGenerator.class.getClassLoader()
                    .getResourceAsStream(testSuiteIdentifier);
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, bOut);
            inputStream.close();
            bOut.close();

            //parse JSON structure
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            CashBoxSimulation cashBoxSimulation = gson.fromJson(new String(bOut.toByteArray()),
                    CashBoxSimulation.class);
            cashBoxSimulationList.add(cashBoxSimulation);
        }

        return cashBoxSimulationList;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ArrayList<>();
}

From source file:Main.java

public static byte[] getBytes(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;//  w ww.  ja v a  2  s  . co m
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outSteam.write(buffer, 0, len);//ww  w . j a v a2 s  . co m
    }
    outSteam.close();
    inStream.close();
    return outSteam.toByteArray();
}

From source file:Main.java

public static byte[] compress(String str) throws Exception {
    if (str == null || str.length() == 0) {
        return null;
    }//from  www  . j ava 2s  .c o m

    ByteArrayOutputStream obj = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(obj);

    gzip.write(str.getBytes());
    gzip.close();

    byte[] compressed = obj.toByteArray();
    obj.close();

    return compressed;
}

From source file:Main.java

public static byte[] readStream(InputStream inStream) throws Exception {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int len = 0;//  www .ja v a  2s .c  om
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    return outStream.toByteArray();
}