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:net.solarnetwork.node.util.ClassUtils.java

/**
 * Load a classpath SQL resource into a String.
 * // w ww  .j a  v  a 2s  .  c o  m
 * @param resourceName
 *        the SQL resource to load
 * @param clazz
 *        the Class to load the resource from
 * @return the String
 */
public static String getResourceAsString(String resourceName, Class<?> clazz) {
    InputStream in = clazz.getResourceAsStream(resourceName);
    if (in == null) {
        throw new RuntimeException("Resource [" + resourceName + "] not found");
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException("Error reading resource [" + resourceName + ']', e);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            LOG.warn("Could not close InputStream", ex);
        }
        try {
            out.close();
        } catch (IOException ex) {
            LOG.warn("Could not close OutputStream", ex);
        }
    }
}

From source file:com.comcast.cqs.util.Util.java

public static String compress(String decompressed) throws IOException {
    if (decompressed == null || decompressed.equals("")) {
        return decompressed;
    }/*w  w w. j ava  2  s  . c o  m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream(decompressed.length());
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(decompressed.getBytes());
    gzip.close();
    out.close();
    String compressed = Base64.encodeBase64String(out.toByteArray());
    logger.debug("event=compressed from=" + decompressed.length() + " to=" + compressed.length());
    return compressed;
}

From source file:com.amazonaws.tvm.Utilities.java

public static String getRawPolicyFile() {

    if (RAW_POLICY_OBJECT == null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(8196);
        InputStream in = null;/*from   w ww  . ja  v  a  2s. c  o  m*/
        try {
            in = Utilities.class.getResourceAsStream("/TokenVendingMachinePolicy.json");
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) != -1) {
                baos.write(buffer, 0, length);
            }

            RAW_POLICY_OBJECT = baos.toString();
        } catch (Exception exception) {
            log.log(Level.SEVERE, "Unable to load policy object.", exception);
            RAW_POLICY_OBJECT = "";
        } finally {
            try {
                baos.close();
                in.close();
            } catch (Exception exception) {
                log.log(Level.SEVERE, "Unable to close streams.", exception);
            }
            in = null;
            baos = null;
        }
    }

    return RAW_POLICY_OBJECT;
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] decompress(byte[] in) {
    ByteArrayOutputStream bos = null;

    if (in != null) {
        ByteArrayInputStream bis = new ByteArrayInputStream(in);

        bos = new ByteArrayOutputStream();

        GZIPInputStream gis = null;

        try {/*from   w ww  . j a  v a2s . c  om*/
            gis = new GZIPInputStream(bis);

            byte[] buf = new byte[8192];
            int r = -1;

            while ((r = gis.read(buf)) > 0) {
                bos.write(buf, 0, r);
            }
        } catch (IOException e) {
            bos = null;
            throw new RuntimeException(e);
        } finally {
            try {
                gis.close();
                bos.close();
            } catch (Exception e) {
            }
        }
    }

    return (bos == null) ? null : bos.toByteArray();
}

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

public static byte[] converImagePNGtoJPEG(InputStream data) {
    byte[] imageFinal = null;
    try {/*from w  w  w  .  ja  v a2 s .  co  m*/
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        BufferedImage bufferedImage = ImageIO.read(data);

        // create a blank, RGB, same width and height, and a white background
        BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.WHITE, null);
        ImageIO.write(newBufferedImage, "JPEG", outstream);
        imageFinal = outstream.toByteArray();
        outstream.close();
    } catch (Exception ex) {
    }

    return imageFinal;
}

From source file:com.platform.middlewares.plugins.CameraPlugin.java

private static String writeToFile(Context context, Bitmap img) {
    String name = null;/*from  w ww  . j  a  v  a 2 s  . co m*/
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        img.compress(Bitmap.CompressFormat.JPEG, 50, out);
        name = CryptoHelper.base58ofSha256(out.toByteArray());
        File storageDir = new File(context.getFilesDir().getAbsolutePath() + "/pictures/");
        File image = new File(storageDir, name + ".jpeg");
        FileUtils.writeByteArrayToFile(image, out.toByteArray());
        return name;
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.amalto.workbench.compare.Utilities.java

public static byte[] readBytes(InputStream in) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//from   ww  w.  j  a  va 2  s.c  om
        while (true) {
            int c = in.read();
            if (c == -1)
                break;
            bos.write(c);
        }

    } catch (IOException ex) {
        return null;

    } finally {
        Utilities.close(in);
        try {
            bos.close();
        } catch (IOException x) {
            // silently ignored
        }
    }

    return bos.toByteArray();
}

From source file:fr.msch.wissl.server.Library.java

private static void stfuLog4j() {
    Properties props = new Properties();
    // props.setProperty("org.jaudiotagger.level",
    // Level.WARNING.toString());
    props.setProperty(".level", Level.OFF.toString());
    // props.setProperty("handlers",
    // "java.util.logging.ConsoleHandler,java.util.logging.FileHandler");
    try {//  ww  w .  j a  v  a2 s .  co m
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        props.store(baos, null);
        byte[] data = baos.toByteArray();
        baos.close();
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        LogManager.getLogManager().readConfiguration(bais);
    }

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

From source file:net.solarnetwork.util.ClassUtils.java

/**
 * Load a classpath SQL resource into a String.
 * //ww w . java 2 s  .c  o m
 * @param resourceName the SQL resource to load
 * @param clazz the Class to load the resource from
 * @return the String
 */
public static String getResourceAsString(String resourceName, Class<?> clazz) {
    InputStream in = clazz.getResourceAsStream(resourceName);
    if (in == null) {
        throw new RuntimeException("Fesource [" + resourceName + "] not found");
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        return out.toString();
    } catch (IOException e) {
        throw new RuntimeException("Error reading resource [" + resourceName + ']', e);
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            LOG.warn("Could not close InputStream", ex);
        }
        try {
            out.close();
        } catch (IOException ex) {
            LOG.warn("Could not close OutputStream", ex);
        }
    }
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <p>//from w w  w . j  av  a  2  s  .  c om
 * ?
 * </p>
 * 
 * @param data ??
 * @param privateKey ?(BASE64?)
 * @return byte
 * @throws Exception Exception
 */
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(privateKey);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, privateK);
    int inputLen = data.length;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int offSet = 0;
    byte[] cache;
    int index = 0;
    // ?
    while (inputLen - offSet > 0) {
        if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
            cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
        } else {
            cache = cipher.doFinal(data, offSet, inputLen - offSet);
        }
        out.write(cache, 0, cache.length);
        index++;
        offSet = index * MAX_ENCRYPT_BLOCK;
    }
    byte[] encryptedData = out.toByteArray();
    out.close();
    return encryptedData;
}