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:edu.kit.dama.rest.util.RestClientUtils.java

/**
 * Check for log level and log inputstream if necessary.
 *
 * @param stream inputstream to be logged.
 * @return inputstream//from   w  w  w .  j a  v  a 2  s  . c o m
 */
private static InputStream getInputStream(InputStream stream) {
    InputStream returnValue = stream;
    if (LOGGER.isDebugEnabled()) {
        byte[] data = new byte[0];
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            byte[] block = new byte[4096]; // for a 4k block size
            int count;

            while ((count = stream.read(block)) != -1) {
                baos.write(block, 0, count);
            }
            data = baos.toByteArray();
            LOGGER.debug("Client response: " + new String(data));
        } catch (IOException ioe) {
            LOGGER.error(null, ioe);
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException ex) {
                }
            }

            returnValue = new ByteArrayInputStream(data);
        }
    }
    return returnValue;
}

From source file:com.mucommander.ui.viewer.image.ImageViewer.java

private static BufferedImage transcodeSVGDocument(AbstractFile file, float width, float height)
        throws IOException {
    // create a PNG transcoder.
    Transcoder t = new PNGTranscoder();
    // Set the transcoding hints.
    if (width > 0) {
        t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, width);
    }/*from  ww  w  .  jav a  2 s .  c o m*/
    if (height > 0) {
        t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, height);
    }
    t.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_VALIDATING, false);

    // create the transcoder input.
    TranscoderInput input = new TranscoderInput(file.getInputStream());
    ByteArrayOutputStream ostream = null;
    try {
        // create the transcoder output.
        ostream = new ByteArrayOutputStream();
        TranscoderOutput output = new TranscoderOutput(ostream);

        // Save the image.
        t.transcode(input, output);

        // Flush and close the stream.
        ostream.flush();
        ostream.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // Convert the byte stream into an image.
    byte[] imgData = ostream.toByteArray();

    // Return the newly rendered image.
    return ImageIO.read(new ByteArrayInputStream(imgData));
}

From source file:com.recomdata.datasetexplorer.proxy.XmlHttpProxy.java

public static JSONObject loadJSONObject(InputStream in) {
    ByteArrayOutputStream out = null;
    try {/*from ww  w  .  j a va 2  s.  c  o  m*/
        byte[] buffer = new byte[1024];
        int read = 0;
        out = new ByteArrayOutputStream();
        while (true) {
            read = in.read(buffer);
            if (read <= 0)
                break;
            out.write(buffer, 0, read);
        }
        return new JSONObject(out.toString());
    } catch (Exception e) {
        getLogger().severe("XmlHttpProxy error reading in json " + e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (Exception e) {
        }
    }
    return null;
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ??/*from  w  w w . j ava  2s .c o m*/
 *
 * @param filePath
 *            ?
 * @return ??
 */
public static String readFileContent(String filePath) {
    if (null == filePath) {
        return null;
    }
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileInputStream fis = new FileInputStream(filePath);

        byte[] buffer = new byte[XConstant.BUFFER_LEN];
        int len = 0;
        while ((len = fis.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }

        String content = bos.toString();
        bos.close();
        fis.close();
        return content;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:io.joynr.util.JoynrUtil.java

private static Object getResource(InputStream inputStream, boolean asByteArray) throws JoynrRuntimeException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
    byte[] bytes = new byte[512];

    // Read bytes from the input stream in bytes.length-sized chunks and
    // write/*  w  w  w.j a v a2 s.  co m*/
    // them into the output stream
    int readBytes;
    try {
        while ((readBytes = inputStream.read(bytes)) > 0) {
            outputStream.write(bytes, 0, readBytes);
        }
        Object result = null;

        if (asByteArray) {
            result = ArrayUtils.toObject(outputStream.toByteArray());
        } else {
            result = outputStream.toString("UTF-8");

        }
        // Close the streams
        inputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        throw new JoynrRuntimeException(e.getMessage(), e) {
            private static final long serialVersionUID = 1L;
        };
    }
}

From source file:jfs.sync.encryption.JFSEncryptedStream.java

/**
 * //  w ww.  j  ava2  s.c  o  m
 * @param fis
 * @param expectedLength
 *            length to be expected or -2 if you don't want the check
 * @param cipher
 * @return
 */
public static InputStream createInputStream(InputStream fis, long expectedLength, Cipher cipher) {
    try {
        InputStream in = fis;
        ObjectInputStream ois = new ObjectInputStream(in);
        byte marker = readMarker(ois);
        long l = readLength(ois);
        if (log.isDebugEnabled()) {
            log.debug(
                    "JFSEncryptedStream.createInputStream() length check " + expectedLength + " == " + l + "?");
        } // if
        if (expectedLength != DONT_CHECK_LENGTH) {
            if (l != expectedLength) {
                log.error("JFSEncryptedStream.createInputStream() length check failed");
                return null;
            } // if
        } // if
        if (cipher == null) {
            log.error("JFSEncryptedStream.createInputStream() no cipher for length " + expectedLength);
        } else {
            in = new CipherInputStream(in, cipher);
        } // if
        if (marker == COMPRESSION_DEFLATE) {
            Inflater inflater = new Inflater(true);
            in = new InflaterInputStream(in, inflater, COMPRESSION_BUFFER_SIZE);
        } // if
        if (marker == COMPRESSION_BZIP2) {
            in = new BZip2CompressorInputStream(in);
        } // if
        if (marker == COMPRESSION_LZMA) {
            Decoder decoder = new Decoder();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

            byte[] properties = new byte[5];
            int readBytes = in.read(properties, 0, properties.length);
            boolean result = decoder.SetDecoderProperties(properties);
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() readBytes=" + readBytes);
                log.debug("JFSEncryptedStream.createInputStream() result=" + result);
            } // if

            decoder.Code(in, outputStream, l);
            in.close();
            outputStream.close();
            if (log.isDebugEnabled()) {
                log.debug("JFSEncryptedStream.createInputStream() " + outputStream.size());
            } // if
            in = new ByteArrayInputStream(outputStream.toByteArray());
        } // if
        return in;
    } catch (IOException ioe) {
        log.error("JFSEncryptedStream.createInputStream() I/O Exception " + ioe.getLocalizedMessage());
        return null;
    } // try/catch
}

From source file:com.redhat.rhn.common.hibernate.HibernateFactory.java

/**
 * utility to convert blob to byte array
 * @param fromBlob blob to convert//from  w w w . j  a va  2  s . co  m
 * @return byte array converted from blob
 */
public static byte[] blobToByteArray(Blob fromBlob) {

    if (fromBlob == null) {
        return new byte[0];
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        return toByteArrayImpl(fromBlob, baos);
    } catch (SQLException e) {
        LOG.error("SQL Error converting blob to byte array", e);
        throw new DatabaseException(e.toString());
    } catch (IOException e) {
        LOG.error("I/O Error converting blob to byte array", e);
        throw new DatabaseException(e.toString());
    } finally {
        try {
            baos.close();
        } catch (IOException ex) {
            throw new DatabaseException(ex.toString());
        }
    }
}

From source file:Main.java

public static Bitmap getCompressBitmap(Bitmap bitmap, File file, int quality, int fileSize) {
    Bitmap bmp = null;//from   w w w  .  j  a v a2  s. c o m
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    boolean result = bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
    LOG("getCompressBitmap result: " + result);
    try {
        if (file != null) {
            if (result) {
                byte[] bt = bos.toByteArray();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(bt);
                fos.close();

                LOG("file.length(): " + file.length());

                if (file.length() > fileSize) {
                    bmp = getCompressBitmap(bmp, file, (int) (quality * 0.8), fileSize);
                } else {
                    bmp = BitmapFactory.decodeFile(file.getPath());
                }
            }

        } else {
            bmp = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.size());
        }
        bos.close();
    } catch (Exception e) {
        LOG("getCompressBitmap result: e" + e.toString());
        e.printStackTrace();
        return null;
    }
    return bmp;
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static String getManifest(JarFile jar) throws IOException {

    JarEntry manifestEntry = jar.getJarEntry(MANIFEST_LOCATION);

    InputStream jis = null;//from ww  w . ja  v  a  2  s  . c  o m
    ByteArrayOutputStream baos = null;

    try {
        jis = jar.getInputStream(manifestEntry);
        baos = new ByteArrayOutputStream();

        CopyUtil.copyClose(jis, baos);
        baos.close();

        String manifest = baos.toString();

        return manifest;
    } finally {
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(baos);
    }
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>//from ww  w .  ja v a  2s. c om
 * ?
 * </p>
 * 
 * @param data
 *          ??
 * @param privateKey
 *          ?(BASE64?)
 * @return
 * @throws Exception
 */
public static byte[] encryptByPrivateKey(byte[] data, String privateKey) throws Exception {
    byte[] keyBytes = Base64Utils.decode(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 i = 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);
        i++;
        offSet = i * MAX_ENCRYPT_BLOCK;
    }
    byte[] encryptedData = out.toByteArray();
    out.close();
    return encryptedData;
}