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:com.canoo.webtest.boundary.StreamBoundary.java

/**
 * Close an InputStream and abort step if an error occur.
 *
 * Wraps IOException's./*w  ww. ja  va2s  .c  o m*/
 *
 * @param is
 * @param step
 */
public static void tryCloseInputStreamWithFail(final InputStream is, final Step step) {
    if (is != null) {
        try {
            is.close();
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            throw new StepExecutionException("Error closing stream: " + e.getMessage(), step);
        }
    }
}

From source file:com.newrelic.agent.deps.org.apache.http.conn.util.PublicSuffixMatcherLoader.java

public static PublicSuffixMatcher load(final URL url) throws IOException {
    Args.notNull(url, "URL");
    final InputStream in = url.openStream();
    try {/*from  w  w  w  . j  a  v  a 2  s  . c om*/
        return load(in);
    } finally {
        in.close();
    }
}

From source file:Main.java

/**
 * Downloads the resource with the given URL and writes it as a local file.
 *
 * @param url the URL to read from/*w w  w .  j  a  va2  s  .c o  m*/
 * @param outputFileName the name of the file to write to
 * @return the URL for the local file
 */
public static String downloadUrlToFile(String url, String outputFileName) throws IOException {
    InputStream in = new URL(url).openStream();
    try {
        return writeStreamToFile(in, outputFileName);
    } finally {
        in.close();
    }
}

From source file:Main.java

public static boolean isAssetExists(String path) {
    boolean ret = false;
    if (mFileTable.containsKey(path))
        return (boolean) mFileTable.get(path);
    if (sAssetManager != null) {
        InputStream input = null;
        try {/*from  w w  w. j a  va 2s  .c  o m*/
            input = sAssetManager.open(path);
            ret = true;
            mFileTable.put(path, true);
            input.close();
        } catch (Exception e) {
            mFileTable.put(path, false);
        }
    }
    return ret;
}

From source file:Main.java

public static String LoadData(Context context, String inFile) {
    String tContents = "";
    try {/*  w w w.j  a  va2s . c o m*/
        InputStream stream = context.getAssets().open(inFile);
        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions

    }

    return tContents;
}

From source file:att.jaxrs.util.Util.java

public static String getStringFromInputStream(InputStream inputStream) {
    CachedOutputStream bos = new CachedOutputStream();
    try {//from  w w w . j  a  v  a 2 s .c  om
        IOUtils.copy(inputStream, bos);
        inputStream.close();
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bos.getOut().toString();

}

From source file:com.o2d.pkayjava.editor.utils.Overlap2DUtils.java

private static String getMyDocumentsLocation() {
    String myDocuments = null;/*from   w  w  w. ja va2  s  . c o  m*/
    try {
        if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }
        if (SystemUtils.IS_OS_WINDOWS) {
            Process p = Runtime.getRuntime().exec(
                    "reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal");
            p.waitFor();

            InputStream in = p.getInputStream();
            byte[] b = new byte[in.available()];
            in.read(b);
            in.close();

            myDocuments = new String(b);
            myDocuments = myDocuments.split("\\s\\s+")[4];
        }
        if (SystemUtils.IS_OS_LINUX) {
            myDocuments = System.getProperty("user.home") + File.separator + "Documents";
        }

    } catch (Throwable t) {
        t.printStackTrace();
    }

    return myDocuments;
}

From source file:Main.java

public static String readAssetTextFile(Context context, String inFile) {
    String tContents = "";

    try {//from   www. ja va 2  s . co  m
        InputStream stream = context.getAssets().open(inFile);

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions here
    }

    return tContents;

}

From source file:Main.java

public static boolean fileCopy(File srcFile, File objFile) {
    if (srcFile == null || objFile == null) {
        return false;
    }//  www.java  2 s  .  c o  m

    boolean result;
    try {
        InputStream iStream = new FileInputStream(srcFile);
        OutputStream oStream = new FileOutputStream(objFile);
        streamCopy(iStream, oStream);
        iStream.close();
        oStream.close();
        result = true;
    } catch (IOException e) {
        e.printStackTrace();
        result = false;
    }
    return result;
}

From source file:gui.CompressDecompress.java

public static byte[] compressBuffer(List<List<BigInteger>> encBuffer) {

    try {/*from w  w w.j a v a  2s.c  o m*/
        File loc = new File("temp.dat");
        if (loc.exists()) {
            Files.delete(loc.toPath());
        }
        byte[] byteArray = serialize(encBuffer);
        AdaptiveArithmeticCompress.encoder(byteArray, loc.getAbsolutePath());

        InputStream is = new FileInputStream(loc);
        byte[] bytes = IOUtils.toByteArray(is);
        is.close();

        return bytes;

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

}