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 loadFromFile(File src, OutputStream out) throws FileNotFoundException, IOException {
    InputStream in = new FileInputStream(src);
    try {/* w  ww  .java 2 s. c  o m*/
        transfer(in, out);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String accessToFlashAir(String uri) throws IOException {
    URL url = new URL(uri);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    String result = null;//w ww.  jav a2 s .c o m
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        result = inputStreamToString(in);
        in.close();
    } finally {
        urlConnection.disconnect();
    }

    return result;
}

From source file:Main.java

public static Bitmap getBitmapFromUri(@NonNull Context context, @NonNull Uri uri) throws IOException {
    InputStream stream = context.getContentResolver().openInputStream(uri);
    Bitmap rawImage = BitmapFactory.decodeStream(stream);

    try {/*from  w ww  .ja  va  2  s .  c o  m*/
        stream.close();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return rawImage;
}

From source file:Main.java

public static String loadJSONFromAsset(final Context context, final String fileName) throws IOException {
    String json;/*from  w  w w.  j  a v  a2s. c  o m*/

    InputStream is = context.getAssets().open(fileName);
    int size = is.available();
    byte[] buffer = new byte[size];

    is.read(buffer);
    is.close();

    json = new String(buffer, "UTF-8");

    return json;
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;//w  ww. j a v  a  2  s . c  o  m

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}

From source file:com.bt.aloha.batchtest.WeekendBatchTest.java

protected static void configure(BatchTest batchTest) throws Exception {
    Properties properties = new Properties();
    InputStream is = new FileInputStream(CONFIG_FILE);
    properties.load(is);/*ww w. j  a  v a 2s . c o  m*/
    is.close();
    sleepTime = Integer.parseInt(properties.getProperty("sleepTime", Integer.toString(defaultSleepTime)));
    batchTest.setAudioFileUri(properties.getProperty("audioFileUri", "/provisioned/behave.wav"));
    stop = Boolean.parseBoolean(properties.getProperty("stop", "false"));
    batchTest.setNumberOfRuns(Integer.parseInt(properties.getProperty("numberOfRuns", "1000")));
    int concurrentStarts = Integer.parseInt(properties.getProperty("numberOfConcurrentStarts", "4"));
    batchTest.setNumberOfConcurrentStarts(concurrentStarts);
    if (executorService == null)
        executorService = Executors.newFixedThreadPool(concurrentStarts);
    batchTest.setExecutorService(executorService);
    batchTest.setMaximumScenarioCompletionWaitTimeSeconds(
            Integer.parseInt(properties.getProperty("maximumScenarioCompletionWaitTimeSeconds", "60")));
    addBatchScenarios(batchTest);
}

From source file:Main.java

/**
 * Copy the entire content of an InputStream into an OutputStream and close
 * only the input stream./*from w w w .j a v  a  2 s.  c  o  m*/
 * 
 * @param in
 *            The InputStream to read data from
 * @param out
 *            The OutputStream to write data to
 * 
 * @throws IOException
 *             I/O exception from read or write
 */
public static void copyWholeStreamAndCloseInput(InputStream in, OutputStream out) throws IOException {
    try {
        readFromAndWriteTo(in, out);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static String readAsset(Context context, String assetPath) throws IOException {
    String asset = null;/*w  w  w  .  jav  a  2 s . c o  m*/
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(assetPath);
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        is.close();
        asset = new String(data, "ASCII");
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return asset;
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String jsonFileName) throws IOException {

    AssetManager manager = context.getAssets();
    InputStream is = manager.open(jsonFileName);

    int size = is.available();
    byte[] buffer = new byte[size];
    is.read(buffer);/*  w  ww  .j  a  v  a2s  .co  m*/
    is.close();

    return new String(buffer, "UTF-8");
}

From source file:com.mitrapps.mocaFileTransfer.FileUploader.java

public static void transferFile(MocaConnection conn, String inputFileName, String outputPath) {

    try {/*from  w ww. j  av a 2  s .  c om*/
        // get pointer to the file and print ack message to user
        File file = new File(inputFileName);
        //System.out.println("File is size " + file.length());

        // Convert contents to byte array and print ack message to user
        InputStream in = new FileInputStream(file);
        byte[] fileContents = IOUtils.toByteArray(in);
        in.close();
        //System.out.println("Byte array size is " + fileContents.length);

        String command = String.format(
                "decode from base64" + " where str = '%s'" + " | " + "write stream to file "
                        + " where dataStream = @base64_decoded" + "   and filePath = '%s'",
                com.redprairie.util.Base64.encode(fileContents), outputPath);
        conn.executeCommand(command);
    } catch (MocaException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}