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

/**
 * BitMap2Byte//from   w w  w. java  2s  . c  o  m
 *
 * @param bitmap
 * @return
 */
public static byte[] getBitmapByte(Bitmap bitmap) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    try {
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

From source file:org.radiognu.radiognu.utils.utilsradiognu.java

public static final String getResponseServiceAPI(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;/*w  ww  . jav  a2s . c  om*/
    String responseString = null;
    try {
        // make a HTTP request
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            // request successful - read the response and close the connection
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else {
            // request failed - close the connection
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (Exception e) {
    }
    return responseString;
}

From source file:com.github.khandroid.http.misc.FileDownloader.java

public static byte[] download(HttpClient httpClient, URI source) throws ClientProtocolException, IOException {
    byte[] ret;/*from www  .  j  av  a2  s .co m*/

    KhandroidLog.v("Downloading " + source.toString());
    HttpGet req = new HttpGet(source);
    HttpResponse response = httpClient.execute(req);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    KhandroidLog.v("Status code:" + statusCode);
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        entity.writeTo(output);
        output.close();

        ret = output.toByteArray();
    } else {
        throw new IOException(
                "Download failed, HTTP response code " + statusCode + " - " + statusLine.getReasonPhrase());
    }
    req.releaseConnection();

    return ret;
}

From source file:Main.java

public static byte[] decompressGZIP(byte bytes[]) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipis = new GZIPInputStream(is);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int i;/*from  ww  w .j  a v a  2s.c  o m*/
    while ((i = gzipis.read()) != -1) {
        os.write(i);
    }
    gzipis.close();
    os.close();
    return os.toByteArray();
}

From source file:Main.java

public static byte[] convertToBytes(Bitmap bitmap, CompressFormat format, int quality) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(format, quality, stream);
    byte[] data = stream.toByteArray();
    try {/* w w  w .  j  a  va 2 s  .c  o  m*/
        stream.close();
    } catch (Exception e) {
        // nothing
    }
    return data;
}

From source file:Main.java

private static byte[] bitmap2Bytes(Bitmap bm) {
    try {/*w w  w  .  ja va 2  s .com*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.Kuesty.services.JSONRequest.java

public static void execute(String uri, JSONRequestTaskHandler rsh) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;/*from  w w  w . j  a v  a 2  s  . co  m*/
    String responseString = null;

    try {
        response = httpclient.execute(new HttpGet(uri));
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();

        } else {
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        rsh.onError(e.getMessage());
    } catch (IOException e) {
        rsh.onError(e.getMessage());
    }
    try {
        JSONObject response1 = new JSONObject(responseString);
        rsh.onSuccess(response1);

    } catch (JSONException e) {
        rsh.onError(e.getMessage());
    }
}

From source file:Main.java

public static byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream os = null;
    try {//from   w  ww  .  ja  v  a  2 s  .  co  m
        os = new ByteArrayOutputStream();
        copyStream(is, os);
    } finally {
        os.close();
    }
    return os.toByteArray();
}

From source file:Main.java

public static byte[] bitampToByteArray(Bitmap bitmap) {
    byte[] array = null;
    try {//from  ww  w. j  a v a  2  s.c o m
        if (null != bitmap) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
            array = os.toByteArray();
            os.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return array;
}

From source file:Main.java

public static byte[] bitmapToByteArray(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] bytes = baos.toByteArray();
    try {/*from   w  ww  .  j a va 2  s  .  com*/
        baos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytes;

}