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:gemlite.core.util.RSAUtils.java

/**
 * <p>/*w w w  .j  a  v  a 2s  . co  m*/
 * 
 * </p>
 * 
 * @param data
 *          ??
 * @param publicKey
 *          (BASE64?)
 * @return
 * @throws Exception
 */
public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception {
    byte[] keyBytes = Base64Utils.decode(publicKey);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key publicK = keyFactory.generatePublic(x509KeySpec);
    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicK);
    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;
}

From source file:com.navercorp.pinpoint.web.filter.Base64.java

public static byte[] decode(String s, int options) {
    byte[] bytes;
    try {//from www. j a  va2  s. c  o  m
        bytes = s.getBytes("UTF-8");
    } catch (UnsupportedEncodingException var21) {
        bytes = s.getBytes();
    }

    bytes = decode(bytes, 0, bytes.length, options);
    if (bytes != null && bytes.length >= 4) {
        int head = bytes[0] & 255 | bytes[1] << 8 & '\uff00';
        if (35615 == head) {
            GZIPInputStream gzis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            try {
                gzis = new GZIPInputStream(new ByteArrayInputStream(bytes));
                byte[] buffer = new byte[2048];

                int length;
                while ((length = gzis.read(buffer)) >= 0) {
                    baos.write(buffer, 0, length);
                }

                bytes = baos.toByteArray();
            } catch (IOException var22) {
                ;
            } finally {
                try {
                    baos.close();
                } catch (Exception var20) {
                    LOG.error("error closing ByteArrayOutputStream", var20);
                }

                if (gzis != null) {
                    try {
                        gzis.close();
                    } catch (Exception var19) {
                        LOG.error("error closing GZIPInputStream", var19);
                    }
                }

            }
        }
    }

    return bytes;
}

From source file:org.hl7.fhir.client.ClientUtils.java

public static byte[] getFeedAsByteArray(AtomFeed feed, boolean pretty, boolean isJson) {
    ByteArrayOutputStream baos = null;
    byte[] byteArray = null;
    try {//  ww w . ja v a2 s .c  o  m
        baos = new ByteArrayOutputStream();
        Composer composer = null;
        if (isJson) {
            composer = new JsonComposer();
        } else {
            composer = new XmlComposer();
        }
        composer.compose(baos, feed, pretty);
        byteArray = baos.toByteArray();
        baos.close();
    } catch (Exception e) {
        try {
            baos.close();
        } catch (Exception ex) {
            throw new EFhirClientException("Error closing output stream", ex);
        }
        throw new EFhirClientException("Error converting output stream to byte array", e);
    }
    return byteArray;
}

From source file:org.hl7.fhir.client.ClientUtils.java

/**
 * ****************************************************************
 * Other general helper methods/*from   www .j a  va2  s .  c  o  m*/
* ***************************************************************
 */
public static <T extends Resource> byte[] getTagListAsByteArray(List<AtomCategory> tags, boolean pretty,
        boolean isJson) {
    ByteArrayOutputStream baos = null;
    byte[] byteArray = null;
    try {
        baos = new ByteArrayOutputStream();
        Composer composer = null;
        if (isJson) {
            composer = new JsonComposer();
        } else {
            composer = new XmlComposer();
        }
        composer.compose(baos, tags, pretty);
        byteArray = baos.toByteArray();
        baos.close();
    } catch (Exception e) {
        try {
            baos.close();
        } catch (Exception ex) {
            throw new EFhirClientException("Error closing output stream", ex);
        }
        throw new EFhirClientException("Error converting output stream to byte array", e);
    }
    return byteArray;
}

From source file:com.iStudy.Study.Renren.Util.java

/**
 * ???//from   w  ww  .j  a va2  s.  co m
 * 
 * @param inputStream
 * @return
 * @throws IOException
 */
public static byte[] streamToByteArray(InputStream inputStream) {
    byte[] content = null;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream bis = new BufferedInputStream(inputStream);

    try {
        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = bis.read(buffer)) != -1) {
            baos.write(buffer, 0, length);
        }

        content = baos.toByteArray();
        if (content.length == 0) {
            content = null;
        }

        baos.close();
        bis.close();
    } catch (IOException e) {
        logger(e.getMessage());
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (IOException e) {
                logger(e.getMessage());
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
                logger(e.getMessage());
            }
        }
    }

    return content;
}

From source file:org.hl7.fhir.client.ClientUtils.java

public static <T extends Resource> byte[] getResourceAsByteArray(T resource, boolean pretty, boolean isJson) {
    ByteArrayOutputStream baos = null;
    byte[] byteArray = null;
    try {//from  w  ww  .j a va 2  s .c om
        baos = new ByteArrayOutputStream();
        Composer composer = null;
        if (isJson) {
            composer = new JsonComposer();
        } else {
            composer = new XmlComposer();
        }
        composer.compose(baos, resource, pretty);
        byteArray = baos.toByteArray();
        baos.close();
    } catch (Exception e) {
        try {
            baos.close();
        } catch (Exception ex) {
            throw new EFhirClientException("Error closing output stream", ex);
        }
        throw new EFhirClientException("Error converting output stream to byte array", e);
    }
    return byteArray;
}

From source file:com.faceye.feature.util.http.DeflateUtils.java

/**
 * Returns an inflated copy of the input array, truncated to
 * <code>sizeLimit</code> bytes, if necessary.  If the deflated input
 * has been truncated or corrupted, a best-effort attempt is made to
 * inflate as much as possible.  If no data can be extracted
 * <code>null</code> is returned.
 *///from  ww w .ja  v a  2s  .  c  o  m
public static final byte[] inflateBestEffort(byte[] in, int sizeLimit) {
    // decompress using InflaterInputStream 
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

    // "true" because HTTP does not provide zlib headers
    Inflater inflater = new Inflater(true);
    InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(in), inflater);

    byte[] buf = new byte[BUF_SIZE];
    int written = 0;
    while (true) {
        try {
            int size = inStream.read(buf);
            if (size <= 0)
                break;
            if ((written + size) > sizeLimit) {
                outStream.write(buf, 0, sizeLimit - written);
                break;
            }
            outStream.write(buf, 0, size);
            written += size;
        } catch (Exception e) {
            LOG.info("Caught Exception in inflateBestEffort", e);
            break;
        }
    }
    try {
        outStream.close();
    } catch (IOException e) {
    }

    return outStream.toByteArray();
}

From source file:SystemUtils.java

public static byte[] LoadDataFromStream(InputStream ins) throws IOException {
    byte[] data = null, buf;
    ByteArrayOutputStream baos = null;
    int cnt;//  www  .ja v a 2s  . c om

    try {
        buf = new byte[1024];
        baos = new ByteArrayOutputStream();

        cnt = ins.read(buf);
        while (cnt != -1) {
            baos.write(buf, 0, cnt);
            cnt = ins.read(buf);
        }

        data = baos.toByteArray();
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (Exception e) {
            }
            baos = null;
        }
    }

    return data;
}

From source file:com.zzl.zl_app.connection.Utility.java

/**
 * Implement a weibo http request and return results .
 * /*  w  ww. j a va 2 s  .  co  m*/
 * @param context
 *            context of activity
 * @param url
 * @param method
 *            (GET|POST)
 * @param bm
 * @return
 * @throws Exception
 */
public static String openUrl(Context context, String url, String method, Bitmap bm) throws Exception {
    String result = "";
    try {
        HttpClient client = getNewHttpClient(context);
        HttpUriRequest request = null;
        ByteArrayOutputStream bos = null;
        if (method.equals("GET")) {

        } else if (method.equals("POST")) {
            Log.i("IO", url);
            HttpPost post = new HttpPost(url);
            byte[] data = null;
            bos = new ByteArrayOutputStream(1024 * 50);
            post.setHeader("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
            Utility.imageContentToUpload(bos, bm);
            data = bos.toByteArray();
            bos.close();
            // UrlEncodedFormEntity entity = getPostParamters(params);
            ByteArrayEntity formEntity = new ByteArrayEntity(data);
            post.setEntity(formEntity);
            request = post;
        } else if (method.equals("DELETE")) {
            request = new HttpDelete(url);
        }
        HttpResponse response = client.execute(request);
        StatusLine status = response.getStatusLine();
        int statusCode = status.getStatusCode();

        if (statusCode != 200) {
            result = read(response);
            throw new Exception();
        }
        // parse content stream from response
        result = read(response);
        return result;
    } catch (IOException e) {

    }
    return "";
}