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.ewcms.common.io.HtmlFileUtil.java

public static String readText(File f, String encoding) {
    try {// w w w  .  j  av a 2s  .  co  m
        f = normalizeFile(f);
        InputStream is = new FileInputStream(f);
        String str = readText(is, encoding);
        is.close();
        return str;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String loadJSONFromAsset(Context context, String fileName) {
    String json = null;/*from ww  w .ja v  a  2s  .  c o  m*/
    try {
        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");
    } catch (IOException ex) {
        Log.d(TAG, "Exception Occurred : " + ex.getMessage());
        return null;
    }
    return json;

}

From source file:com.canoo.webtest.boundary.StreamBoundary.java

/**
 * Close an InputStream ignoring an errors.
 *
 * Wraps IOException's.//ww  w .j  a v a  2  s .  co  m
 *
 * @param inStream
 */
public static void closeInputStream(final InputStream inStream) {
    if (inStream != null) {
        try {
            inStream.close();
        } catch (IOException e) {
            LOG.warn("Error closing stream: " + e.getMessage(), e);
        }
    }
}

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

protected static void configure(BatchTest batchTest) throws Exception {
    Properties properties = new Properties();
    InputStream is = new FileInputStream(CONFIG_FILE);
    properties.load(is);//w  w  w. j a va  2s.  co  m
    is.close();
    batchTest.setAudioFileUri(properties.getProperty("audioFileUri", "/provisioned/behave.wav"));
    batchTest.setNumberOfRuns(1);
    batchTest.setNumberOfConcurrentStarts(1);
    batchTest.setMaximumScenarioCompletionWaitTimeSeconds(
            Integer.parseInt(properties.getProperty("maximumScenarioCompletionWaitTimeSeconds", "60")));
    batchTest.setExecutorService(Executors.newFixedThreadPool(4));
    addBatchScenarios(batchTest);
}

From source file:jfix.zk.Medias.java

public static String asString(Media media) {
    try {//from w  ww  . ja v a2 s.  com
        String result = null;
        if (media.isBinary()) {
            InputStream input = Medias.asStream(media);
            result = IOUtils.toString(input, "UTF-8");
            input.close();
        } else {
            Reader reader = Medias.asReader(media);
            result = IOUtils.toString(reader);
            reader.close();
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

public static String readAsStringAndClose(InputStream source) throws IOException {
    try {/*from  w  w  w  . jav a 2s .c o  m*/
        return readAsString(source, UTF8_ENCODING);
    } finally {
        source.close();
    }
}

From source file:com.incosyz.sms.other.UploadBackup.java

public static void uploadBackup(String filePath, String fileName) {
    String server = "ftp.bambooblindslanka.com";
    int port = 21;
    String user = "samagi@bambooblindslanka.com";
    String pass = "fs82xKzHZvtU";

    FTPClient client = new FTPClient();
    try {// www  .ja  v  a 2 s . c  om
        client.connect(server, port);
        client.login(user, pass);
        client.enterLocalPassiveMode();
        client.setFileType(FTP.BINARY_FILE_TYPE);
        File firstLocalFile = new File(filePath);

        String firstRemoteFile = fileName;
        InputStream inputStream = new FileInputStream(firstLocalFile);

        boolean done = client.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
    } catch (IOException ex) {
        Logger.getLogger(UploadBackup.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Main.java

public static boolean IsFolder(AssetManager am, String uri) {
    uri = RemoveLastPathSeperator(uri);/*from  w w w .ja va2s . c  om*/
    try {

        InputStream is = am.open(uri);
        if (am.open(uri) == null) {
            return true;
        } else {
            is.close();
            is = null;
            return false;
        }
    } catch (IOException e) {
        return true;
    }
}

From source file:Main.java

public static String copyAsset(Context context, String assetName, File dir) {
    try {/*from  w  ww  . j a  v  a  2s  .c om*/
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File outFile = new File(dir, assetName);
        if (!outFile.exists()) {
            AssetManager assetManager = context.getAssets();
            InputStream in = assetManager.open(assetName);
            OutputStream out = new FileOutputStream(outFile);
            copyFile(in, out);
            in.close();
            out.close();
        }
        return outFile.getAbsolutePath();
    } catch (Exception e) {

    }
    return "";
}

From source file:Main.java

/**
 * Loads a file into string//  w w w  .  j a va  2s.  c  om
 */
public static String readFile(String fileName, AssetManager assetManager) throws IOException {
    InputStream input;
    input = assetManager.open(fileName);
    int size = input.available();
    byte[] buffer = new byte[size];
    input.read(buffer);
    input.close();
    return new String(buffer);
}