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

private static byte[] stringToBytes(String string) {
    if (string == null) {
        return null;
    }//from  w  w  w .  j  av  a2  s .c o m
    byte[] buffer = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    try {
        dos.writeUTF(string);
        buffer = baos.toByteArray();
    } catch (IOException ex) {
    } finally {
        try {
            baos.close();
            dos.close();
        } catch (IOException ex1) {
        }
        baos = null;
        dos = null;
    }
    return buffer;
}

From source file:HexDecoder.java

/**
 * Encodes the input data producing a Hex output stream.
 * @param data Input data to encode./*w ww  . jav  a2  s  .  c  o m*/
 * @return the number of bytes produced.
 */
public static String encode(final byte[] data) {
    try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        encode(data, 0, data.length, out);
        out.close();
        return new String(out.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

public static final byte[] compress(byte[] bytes) {
    if (bytes == null) {
        throw new NullPointerException("byte[] is NULL !");
    }/*from  w ww . j a va  2  s  . co m*/
    ByteArrayOutputStream bos = new ByteArrayOutputStream(8192);
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = bos.toByteArray();
        gzip.close();
        bos.close();
        gzip = null;
        bos = null;
        return fewerBytes;
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static byte[] compressInGzip(byte[] originalData, int offset, int length) throws Exception {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(bos);
    gzipOutStream.write(originalData, offset, length);
    gzipOutStream.finish();//from w w  w  .j  av a 2  s  .  c  o  m
    gzipOutStream.flush();
    gzipOutStream.close();
    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}

From source file:Main.java

public static String readFile(Activity activity, String fileName) {
    AssetManager assets = activity.getAssets();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;//from  w  w w  . ja va  2  s  .c o  m
    try {
        InputStream inputStream = assets.open(fileName);
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static byte[] getBodyBytes(String ip, String port, String key)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);/*  w  w  w .  ja v  a 2s .co m*/
    dos.writeShort(Short.parseShort(port));
    dos.writeByte(key.getBytes().length);
    dos.write(key.getBytes());
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}

From source file:Main.java

public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception {

    byte[] input = bytes;

    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    compressor.setLevel(Deflater.BEST_COMPRESSION);

    // Give the compressor the data to compress
    compressor.setInput(input);/*  w  ww . java 2  s  .c  o m*/
    compressor.finish();

    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Compress the data
    byte[] buf = new byte[32];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    }
    try {
        bos.close();
    } catch (IOException e) {
    }

    // Get the compressed data
    byte[] compressedData = bos.toByteArray();

    return new String(Base64.encode(compressedData), "UTF-8");
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static byte[] zipDirectory(File directory) throws IOException {
    byte[] data;//  w w  w . jav  a  2 s  .  c o  m

    log.debug("zipDirectory: " + directory.getName());

    ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
    ZipOutputStream zos = new ZipOutputStream(baos);
    zipDirectory(directory, directory.getName(), zos);

    data = baos.toByteArray();
    baos.close();

    return data;
}

From source file:Main.java

public static byte[] readFileToByteArray2(File file) {
    try {/*from w  w w . j  av a2  s . c  om*/
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        }
        bos.flush();
        byte[] bytes = bos.toByteArray();
        fis.close();
        bos.close();
        return bytes;

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

From source file:com.reydentx.core.common.JSONUtils.java

private static String encodeToString(BufferedImage image, String type) {
    String imageString = null;//from   w w  w .  ja va  2  s . co m
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ImageIO.write(image, type, bos);
        byte[] imageBytes = bos.toByteArray();
        imageString = Base64.encodeBase64String(imageBytes);
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}