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:AkashApplications.src.GenerateBarcode.java

public static String convertToBase64(String barcodePath) {
    String type = "jpg";
    BufferedImage img = null;/*  ww w.j  a v a2 s  .  co  m*/
    try {
        img = ImageIO.read(new File(barcodePath));
    } catch (IOException e) {
    }

    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
        ImageIO.write(img, type, bos);
        byte[] imageBytes = bos.toByteArray();

        BASE64Encoder encoder = new BASE64Encoder();
        imageString = encoder.encode(imageBytes);

        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

From source file:FileUtil.java

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

    byte[] byteData = null;

    try {//from w  ww  .  j  a  v  a2s.  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:Main.java

public static byte[] readInputStream(InputStream in) {
    try {//from   ww w.  j av  a2  s .c om
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        byte[] b = new byte[in.available()];
        int length = 0;
        while ((length = in.read(b)) != -1) {
            os.write(b, 0, length);
        }

        b = os.toByteArray();

        in.close();
        in = null;

        os.close();
        os = null;

        return b;

    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFd(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w  w  w .java  2s  .  c  o  m*/
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    //        if(Build.VERSION.SDK_INT < 19)
    //        {
    //            options.inSampleSize = calculateInSampleSize(options, 300, 300);
    //        }
    //        else
    //        {
    options.inSampleSize = calculateInSampleSize(options, 480, 480);

    //        }

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    Bitmap bm = BitmapFactory.decodeFile(filePath, options);
    if (bm == null) {
        return null;
    }
    int degree = readPictureDegree(filePath);
    bm = rotateBitmap(bm, degree);
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

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

}

From source file:net.padaf.preflight.helpers.MetadataValidationHelper.java

/**
 * Return the xpacket from the dictionary's stream
 *///from  w  ww .ja  v a2s .  c o m
public static byte[] getXpacket(COSDocument cdocument) throws IOException, XpacketParsingException {
    COSObject catalog = cdocument.getCatalog();
    COSBase cb = catalog.getDictionaryObject(COSName.METADATA);
    if (cb == null) {
        // missing Metadata Key in catalog
        ValidationError error = new ValidationError(ValidationConstants.ERROR_METADATA_FORMAT,
                "Missing Metadata Key in catalog");
        throw new XpacketParsingException("Failed while retrieving xpacket", error);
    }
    // no filter key
    COSDictionary metadataDictionnary = COSUtils.getAsDictionary(cb, cdocument);
    if (metadataDictionnary.getItem(COSName.FILTER) != null) {
        // should not be defined
        ValidationError error = new ValidationError(ValidationConstants.ERROR_SYNTAX_STREAM_INVALID_FILTER,
                "Filter specified in metadata dictionnary");
        throw new XpacketParsingException("Failed while retrieving xpacket", error);
    }

    PDStream stream = PDStream.createFromCOS(metadataDictionnary);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    InputStream is = stream.createInputStream();
    IOUtils.copy(is, bos);
    is.close();
    bos.close();
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] readFromFile(File file) throws IOException {
    FileInputStream fin = null;//from w w w  .j  av  a 2  s .  c  o  m
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        fin = new FileInputStream(file);
        byte[] buffer = new byte[8192];
        int read = 0;
        while ((read = fin.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        byte[] data = out.toByteArray();
        out.close();
        return data;
    } finally {
        if (fin != null) {
            try {
                fin.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:Main.java

public static byte[] transStreamToBytes(InputStream is, int buffSize) {
    if (is == null) {
        return null;
    }//w ww  .  ja  v  a 2s  . c  om
    if (buffSize <= 0) {
        throw new IllegalArgumentException("buffSize can not less than zero.....");
    }
    byte[] data = null;
    byte[] buffer = new byte[buffSize];
    int actualSize = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((actualSize = is.read(buffer)) != -1) {
            baos.write(buffer, 0, actualSize);
        }
        data = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return data;
}

From source file:com.amazonaws.services.logs.subscriptions.util.TestUtils.java

public static byte[] getCompressedTestFile(String filename) throws IOException {
    byte[] uncompressedData = FileUtils
            .readFileToByteArray(new File(TestUtils.class.getResource(filename).getFile()));

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);

    try {//from w w w  .ja  v a  2 s. c o  m
        gzipOutputStream.write(uncompressedData);
        gzipOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    } finally {
        byteArrayOutputStream.close();
    }
}

From source file:Main.java

private static byte[] transStreamToBytes(InputStream is, int buffSize) {
    if (is == null) {
        return null;
    }/*  ww w. j a  v a 2s.  co  m*/
    if (buffSize <= 0) {
        throw new IllegalArgumentException("buffSize can not less than zero.....");
    }
    byte[] data = null;
    byte[] buffer = new byte[buffSize];
    int actualSize = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        while ((actualSize = is.read(buffer)) != -1) {
            baos.write(buffer, 0, actualSize);
        }
        data = baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return data;
}

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

/**
 * Encodes the given byte array and then GZIP compresses it.
 *
 * @param value byte array input/*w w w.  j a  v  a 2 s .c  o  m*/
 * @return compressed byte array output
 * @throws IOException
 */
public static byte[] compressBytesNonBase64(byte[] value) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(value.length);
    GZIPOutputStream gos = new GZIPOutputStream(baos);
    gos.write(value);
    gos.close();
    byte[] compressed = baos.toByteArray();
    baos.close();
    return compressed;
}