Example usage for java.io InputStream close

List of usage examples for java.io InputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void pushFileFromRAW(Context mContext, File outputFile, int RAW, boolean Override)
        throws IOException {
    if (!outputFile.exists() || Override) {
        if (Override && outputFile.exists())
            if (!outputFile.delete()) {
                throw new IOException(outputFile.getName() + " can't be deleted!");
            }/*  w  w w.  j  a  v  a 2s  . c om*/
        InputStream is = mContext.getResources().openRawResource(RAW);
        OutputStream os = new FileOutputStream(outputFile);
        byte[] data = new byte[is.available()];
        is.read(data);
        os.write(data);
        is.close();
        os.close();
    }
}

From source file:org.keycloak.example.CustomerDatabaseClient.java

public static List<String> getCustomers(HttpServletRequest req) throws Failure {
    KeycloakSecurityContext session = (KeycloakSecurityContext) req
            .getAttribute(KeycloakSecurityContext.class.getName());

    HttpClient client = new DefaultHttpClient();
    try {//w ww.  j av  a2s  .  c  o  m
        HttpGet get = new HttpGet(UriUtils.getOrigin(req.getRequestURL().toString()) + "/database/customers");
        get.addHeader("Authorization", "Bearer " + session.getTokenString());
        try {
            HttpResponse response = client.execute(get);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Failure(response.getStatusLine().getStatusCode());
            }
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            try {
                return JsonSerialization.readValue(is, TypedList.class);
            } finally {
                is.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } finally {
        client.getConnectionManager().shutdown();
    }
}

From source file:io.hawkcd.agent.AgentConfiguration.java

private static Properties fetchConfigFileProperties() {
    File configFile = new File(ConfigConstants.AGENT_CONFIG_FILE_LOCATION);
    if (!configFile.exists()) {
        createConfigFile(configFile);/*from  w  ww .  j a  va2s . co m*/
    }

    Properties properties = new Properties();
    try {
        InputStream inputStream = new FileInputStream(configFile);
        properties.load(inputStream);
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return properties;
}

From source file:Main.java

/**
 * Copy a file by using the file streams
 * //from   ww w.  j  a v  a2s  . com
 * @param src     the source file
 * @param dest    the destination file
 * @throws IOException
 */
public static void copyFileUsingFileStreams(File source, File dest) throws IOException {
    InputStream input = null;
    OutputStream output = null;

    try {
        input = new FileInputStream(source);
        output = new FileOutputStream(dest);

        copyFileUsingStream(input, output);
    } finally {
        input.close();
        output.close();
    }
}

From source file:Main.java

public static int getHistory(String extractPath, String md5) {
    StringBuilder reval = new StringBuilder();
    try {//from w w  w .j a va 2 s  .  co  m
        InputStream in = new FileInputStream(extractPath + "/history_" + md5);
        byte[] buf = new byte[1024];
        int c = 0;
        while ((c = in.read(buf)) >= 0) {
            reval.append(new String(buf, 0, c));
        }
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
        return 1;
    }
    try {
        return Integer.parseInt(reval.toString());
    } catch (Exception e) {
        return 0;
    }

}

From source file:Main.java

public static byte[] getBytes(InputStream in) {

    try {/*from www .j  av  a  2 s  .c om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int len = 0;
        while ((len = in.read(buffer)) != -1)
            baos.write(buffer, 0, len);
        in.close();
        return baos.toByteArray();
    } catch (IOException e) {
        return null;
    }
}

From source file:Main.java

public static String readFromStream(InputStream is) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buff = new byte[1024];
    int len = 0;/*from w w  w  .  j a  v a2 s. co  m*/
    while ((len = is.read(buff)) != -1) {
        bos.write(buff, 0, len);
    }
    is.close();
    String result = bos.toString();
    bos.close();
    return result;
}

From source file:Main.java

private static String getVerNameFromAssert(Context context) {

    String versionName = "";
    try {/*w w w  .j a  v a  2  s.  c o  m*/
        Properties pro = new Properties();
        InputStream is = context.getAssets().open("channel.properties");
        pro.load(is);
        String tmpVersionName = pro.getProperty("versionName");

        versionName = new String(tmpVersionName.getBytes("ISO-8859-1"), "UTF-8");

        is.close();
        is = null;
    } catch (Exception e) {
        versionName = "";
        Log.e(TAG, "AppConfig.loadVersion have Exception e = " + e.getMessage());
    }
    return versionName;

}

From source file:Main.java

public static Bitmap createBitmapFromUri(Context context, Uri uri) {

    Bitmap largeImage = null;/* w w w .  j a  v  a  2 s. com*/
    try {
        InputStream iStream = context.getContentResolver().openInputStream(uri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        largeImage = BitmapFactory.decodeStream(iStream, null, options);
        iStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return largeImage;
}

From source file:Main.java

public static void writeExtractedFileToDisk(InputStream in, OutputStream outs) throws IOException {
    byte[] buffer = new byte[1024];
    int length;/*from   w  w  w .j ava 2 s  . co  m*/
    while ((length = in.read(buffer)) > 0) {
        outs.write(buffer, 0, length);
    }
    outs.flush();
    outs.close();
    in.close();
}