Example usage for java.io OutputStream close

List of usage examples for java.io OutputStream close

Introduction

In this page you can find the example usage for java.io OutputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with this stream.

Usage

From source file:com.eryansky.common.utils.io.IoUtils.java

/**
 * Closes the given stream. The same as calling {@link java.io.OutputStream#close()}, but
 * errors while closing are silently ignored.
 *///from  w w  w  .ja v  a 2 s . com
public static void closeSilently(OutputStream outputStream) {
    try {
        if (outputStream != null) {
            outputStream.close();
        }
    } catch (IOException ignore) {
        // Exception is silently ignored
    }
}

From source file:Main.java

/**
 * A copy of the Android internals StoreThumbnail method, it used with the insertImage to
 * populate the android.provider.MediaStore.Images.Media#insertImage with all the correct
 * meta data. The StoreThumbnail method is private so it must be duplicated here.
 * @see android.provider.MediaStore.Images.Media (StoreThumbnail private method)
 *///from  w  w  w .  ja  va2 s . c o  m
private static final Bitmap storeThumbnail(ContentResolver cr, Bitmap source, long id, float width,
        float height, int kind) {

    // create the matrix to scale it
    Matrix matrix = new Matrix();

    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();

    matrix.setScale(scaleX, scaleY);

    Bitmap thumb = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    ContentValues values = new ContentValues(4);
    values.put(MediaStore.Images.Thumbnails.KIND, kind);
    values.put(MediaStore.Images.Thumbnails.IMAGE_ID, (int) id);
    values.put(MediaStore.Images.Thumbnails.HEIGHT, thumb.getHeight());
    values.put(MediaStore.Images.Thumbnails.WIDTH, thumb.getWidth());

    Uri url = cr.insert(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, values);

    try {
        OutputStream thumbOut = cr.openOutputStream(url);
        thumb.compress(Bitmap.CompressFormat.JPEG, 100, thumbOut);
        thumbOut.close();
        return thumb;
    } catch (FileNotFoundException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
}

From source file:Main.java

public static void copyStream2File(InputStream is, File desc) throws IOException {
    OutputStream os = null;
    try {/*  w w  w . j a  v  a 2 s.  c  om*/
        os = new FileOutputStream(desc);
        copyStream(is, os);
    } finally {
        if (os != null)
            os.close();
    }
}

From source file:com.amazonaws.services.logs.subscriptions.util.TestUtils.java

public static byte[] getCompressedTestFile(String filename) throws IOException {
    byte[] uncompressedData = FileUtils
            .readFileToByteArray(new File(TestUtils.class.getResource(filename).getFile()));

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    OutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);

    try {//from  w w w  .j a v a2 s . c o  m
        gzipOutputStream.write(uncompressedData);
        gzipOutputStream.close();

        return byteArrayOutputStream.toByteArray();
    } finally {
        byteArrayOutputStream.close();
    }
}

From source file:Main.java

/**
 * @param obj// w  w w.j av a2s  . c om
 * @param clazz
 * @param file
 * @throws JAXBException
 * @throws IOException   ((com.zte.cmre.api.Resource)((com.zte.cmdt.base.cmdset.CmdSetCmd
 *                       )((com.zte.cmdt.base.cmdset.CmdSet)((java.util.ArrayList)((
 *                       com.zte.ums.womcn.cmdt.base.cmdset.CmdSetModel)obj).cmdsets).
 *                       get(0)).getCmds().get(3)).getLabel()).getLabel(0).getBytes()
 */
public static void exportXmlJAXB(Object obj, Class<?>[] clazz, File file) throws JAXBException, IOException {
    OutputStream os = new FileOutputStream(file);
    exportXmlJAXB(obj, clazz, os);
    os.close();
}

From source file:gui.CompressDecompress.java

public static List<List<BigInteger>> decompressBuffer(byte[] bytes) {

    try {/*from  w w w .  j a va2  s  . c  o  m*/
        String loc = "temp.dat";

        OutputStream out = new FileOutputStream(loc);
        IOUtils.write(bytes, out);
        out.close();

        byte[] decArray = AdaptiveArithmeticDecompress.decoder(loc);
        List<List<BigInteger>> encBuffer = deserialize(decArray);
        return encBuffer;

    } catch (IOException ex) {
        Logger.getLogger(CompressDecompress.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(CompressDecompress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

From source file:com.canoo.webtest.boundary.StreamBoundary.java

/**
 * Close an OutputStream ignoring an errors.
 *
 * Wraps IOException's.//w w  w .jav a  2 s  . c  o m
 *
 * @param outStream
 */
public static void closeOutputStream(final OutputStream outStream) {
    if (outStream != null) {
        try {
            outStream.close();
        } catch (IOException e) {
            LOG.warn("Error closing stream: " + e.getMessage(), e);
        }
    }
}

From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java

public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException {
    String type = "application/json";
    URL u = new URL(URL);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);//from   w ww  .  j ava  2  s. c o  m
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", type);
    conn.setRequestProperty("User-Agent", UserAgent);
    OutputStream os = conn.getOutputStream();
    os.write(JSONRaw.getBytes());
    os.flush();
    os.close();

    String response = null;
    DataInputStream input = new DataInputStream(conn.getInputStream());
    while (null != ((response = input.readLine()))) {
        input.close();
        return response;
    }
    return null;
}

From source file:Main.java

public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws IOException {
    HttpURLConnection urlConnection = null;
    try {//from  w  w w  .j  a  v  a  2s. co m
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:Main.java

public static String getPosthtml(String posturl, String postData, String encode) {

    String html = "";
    URL url;//from www.  ja va2s. c  o  m
    try {
        url = new URL(posturl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestProperty("User-Agent", userAgent);

        String postDataStr = postData;
        byte[] bytes = postDataStr.getBytes("utf-8");
        connection.setRequestProperty("Content-Length", "" + bytes.length);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Cache-Control", "no-cache");
        connection.setDoOutput(true);
        connection.setReadTimeout(timeout);
        connection.setFollowRedirects(true);
        connection.connect();
        OutputStream outStrm = connection.getOutputStream();
        outStrm.write(bytes);
        outStrm.flush();
        outStrm.close();
        InputStream inStrm = connection.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(inStrm, encode));

        String temp = "";
        while ((temp = br.readLine()) != null) {
            html += (temp + '\n');
        }
        br.close();
        connection.disconnect();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return html;

}