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

/**
 * Reads the given local file and returns the contents as a byte array.
 *
 * @param inputFileName the name of the file to read from
 * @return the file's contents as a byte array
 *//*from ww  w .  j av a2s .  c o m*/
public static byte[] readFile(String inputFileName) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    InputStream in = new FileInputStream(inputFileName);
    try {
        copy(in, out);
    } finally {
        in.close();
    }
    return out.toByteArray();
}

From source file:Main.java

public final static void closeQuietly(InputStream is) {
    try {/*from  www. j  a v  a2  s .  c  o m*/
        if (is != null)
            is.close();
    } catch (Exception e) {
        Log.e("OpenVPN", "closing InputStream", e);
    }
}

From source file:Main.java

/**
 * DB test//from w w w .j av  a 2 s  .  c o m
 */
public static void runBackup(Context context) {
    File file = context.getDatabasePath("PhotoDeskHiddenFolder.db");
    int size = (int) file.length();

    String path = Environment.getExternalStorageDirectory() + "/PhotoDesk/";
    try {
        byte[] buffer = new byte[size];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(buffer);
        inputStream.close();

        File outputDBDirectory = new File(path);
        if (!outputDBDirectory.isDirectory())
            outputDBDirectory.mkdir();

        path += "test.db";

        File outputFile = new File(path);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
    }
}

From source file:Main.java

/**
 * Parse a XML file and return the resulting DOM tree.
 *//*  ww w  . j a v a2  s .c  o  m*/
public static Document readXml(URL xmlUrl) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = newNamespaceAwareFactory();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream input = xmlUrl.openStream();
    try {
        return builder.parse(input);
    } finally {
        input.close();
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(File file, int reqWidth, int reqHeight) throws IOException {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w  w  w .  j  ava2s .  c o  m*/
    InputStream stream = new FileInputStream(file);
    BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    stream = new FileInputStream(file);
    Bitmap bmp = BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    return bmp;
}

From source file:edu.harvard.hms.dbmi.scidb.examples.Apply.java

private static String inputStreamToString(InputStream inputStream) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(inputStream, writer, Charset.defaultCharset());
    inputStream.close();
    return writer.toString();
}

From source file:Main.java

/**
 * Get file content by filename/*from   ww w .  ja  va2 s . co m*/
 * 
 * @param c
 * @param filename
 * @return content String
 */
public static String getFileContent(Context c, String filename) {
    try {
        InputStream fin = c.getAssets().open(filename);
        byte[] buffer = new byte[fin.available()];
        fin.read(buffer);
        fin.close();
        return new String(buffer);
    } catch (IOException e) {
        Log.e("inspector", e.getLocalizedMessage());
    }
    return "";
}

From source file:Main.java

public static void write(InputStream in, OutputStream out) throws Throwable {
    int read = 0;
    while ((read = in.read()) != -1) {
        out.write(read);/*from   w w w . j a  va 2  s.com*/
    }
    in.close();
    out.close();
    out.flush();
}

From source file:Main.java

public static byte[] suckFile(String inputfile) throws IOException {
    InputStream is = context.openFileInput(inputfile);
    int size = is.available();
    byte[] content = new byte[size];

    is.read(content);/*from w w  w.  j  a  va  2s.com*/

    is.close();
    return content;
}

From source file:Main.java

public static void write(InputStream in, OutputStream out) throws IOException {
    int read = 0;
    while ((read = in.read()) != -1) {
        out.write(read);//w  w  w  .j a  v  a 2s.  c  om
    }
    in.close();
    out.close();
    out.flush();
}