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:br.gov.lexml.parser.documentoarticulado.TestUtil.java

public static String sampleText(String resourceName) {
    try {// w ww  .  j ava2 s . c  om
        InputStream input = new BOMInputStream(TestUtil.class.getResourceAsStream(resourceName));
        try {
            return IOUtils.toString(input, ENCODING);
        } finally {
            input.close();
        }
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:Main.java

/**
 * <pre>/*from   ww  w .ja va  2 s .  c o m*/
 * Convenient method to copy file.
 * </pre>
 * @param in {@link InputStream} of source file
 * @param out {@link OutputStream} of destination file
 */
public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }

    in.close();
    out.flush();
    out.close();
}

From source file:Main.java

public static byte[] readAsset(Context context, String filename) throws IOException {
    InputStream in = context.getAssets().open(filename);
    try {//from   w  w w  .  j a v  a  2s  . co  m
        return readAllBytes(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static long getFileLen(File file) {
    long total = 0;
    try {/* w ww.  j a va2s  .  c om*/
        InputStream is = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = is.read(bytes)) != -1) {
            total += len;
        }
        is.close();
    } catch (Exception e) {

    }
    return total;
}

From source file:Main.java

public static void copyFileStream(InputStream input, OutputStream output) throws IOException {
    byte[] buffer = new byte[BUFFSIZE];
    int n = 0;/* ww w  . j  a v  a  2  s . c  om*/
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
    input.close();
    output.close();
}

From source file:Main.java

public static byte[] calculateMd5(String filePath) throws IOException {
    try {//  w w w.j a  va  2 s. c  o  m
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[4 * 1024];
        InputStream is = new FileInputStream(new File(filePath));
        int lent;
        while ((lent = is.read(buffer)) != -1) {
            digest.update(buffer, 0, lent);
        }
        is.close();
        return digest.digest();
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("MD5 algorithm not found.");
    }
}

From source file:Main.java

public static String readFile(Activity activity, String fileName) {
    AssetManager assets = activity.getAssets();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;//  w ww  .j  a va2 s .c  o  m
    try {
        InputStream inputStream = assets.open(fileName);
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:StreamUtils.java

public static byte[] readInByteArray(InputStream in) throws IOException {
    byte[] result = new byte[in.available()];

    in.read(result);//from ww  w.j a  v  a 2s .  c  om

    in.close();

    return result;
}

From source file:Main.java

private static String loadJSONFromAsset(Context context, String pathNameFile) {
    String output = null;/*from ww  w.j  av a2  s .c  o m*/
    try {
        InputStream is = context.getAssets().open(DIR_BOARDS_ASSETS + "/" + pathNameFile);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return output;
}

From source file:com.sharneng.io.IOUtils.java

/**
 * Quietly close a {@linkplain java.io.InputStream} without throwing any exception. Exceptions will be logged as
 * error if the <code>logError</code> is <code>true</code>.
 * /*  ww  w .ja v  a2s  .  co m*/
 * @param in
 *            the input stream to close
 * @param logError
 *            true to log exception as error
 * @see #close(InputStream)
 * @see #close(OutputStream, boolean)
 */
public static void close(InputStream in, boolean logError) {
    if (in != null) {
        try {
            in.close();
        } catch (Throwable t) {
            if (logError)
                log.error(t.getMessage(), t);
            else
                log.debug(t.getMessage(), t);
        }
    }
}