Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

In this page you can find the example usage for java.io FileInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file input stream and releases any system resources associated with the stream.

Usage

From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java

static PrivateKey loadPrivateKey(String file, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Private Key.
    File filePrivateKey = new File(file);
    FileInputStream fis = new FileInputStream(file);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);//from  w w  w .j  a va2  s.  c  o m
    fis.close();

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return privateKey;
}

From source file:Main.java

/** Copia un fichero.
 * @param source Fichero origen con el contenido que queremos copiar.
 * @param dest Fichero destino de los datos.
 * @throws IOException SI ocurre algun problema durante la copia */
public static void copyFile(final File source, final File dest) throws IOException {
    if (source == null || dest == null) {
        throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$
    }//  ww  w  .  j a  v  a2 s  .c  o m

    final FileInputStream is = new FileInputStream(source);
    final FileOutputStream os = new FileOutputStream(dest);
    final FileChannel in = is.getChannel();
    final FileChannel out = os.getChannel();
    final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
    out.write(buf);

    in.close();
    out.close();
    is.close();
    os.close();

}

From source file:edu.kit.cockpit.valuationserver.sfmpersistency.FileUtil.java

/**
 * @param file//from   ww  w .  ja v  a  2 s.com
 * @return string representation of File
 * @throws IOException
 */
public static String readFile(File file) throws IOException {
    FileInputStream stream = new FileInputStream(file);
    try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
    } finally {
        stream.close();
    }
}

From source file:Main.java

public static boolean copyFile(String quelle, String ziel) {
    if ((new File(quelle)).equals(new File(ziel))) {
        return false; // Quelle und Ziel sind dieselbe Datei!
    }/* w ww .ja va 2  s  .com*/
    final int BUFSIZE = 4096;
    FileInputStream f1;
    FileOutputStream f2;
    byte[] buf = new byte[BUFSIZE];
    int n;
    if (!canOpenFile(quelle)) {
        return false;
    }
    try {
        f1 = new FileInputStream(quelle);
        f2 = new FileOutputStream(ziel);
        while ((n = f1.read(buf, 0, BUFSIZE)) > 0) {
            f2.write(buf, 0, n);
        }
        f1.close();
        f2.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean copyFileTo(File srcFile, File destFile) throws IOException {
    if (srcFile == null || destFile == null) {
        return false;
    }/*from   ww  w.j av a2  s  .  com*/
    if (srcFile.isDirectory() || destFile.isDirectory())
        return false;
    if (!srcFile.exists()) {
        return false;
    }
    if (!destFile.exists()) {
        createFile(destFile.getAbsolutePath());
    }
    FileInputStream fis = new FileInputStream(srcFile);
    FileOutputStream fos = new FileOutputStream(destFile);
    int readLen = 0;
    byte[] buf = new byte[1024];
    while ((readLen = fis.read(buf)) != -1) {
        fos.write(buf, 0, readLen);
    }
    fos.flush();
    fos.close();
    fis.close();
    return true;
}

From source file:com.ginstr.android.service.opencellid.library.data.ApiKeyHandler.java

/**
 * Reads a random generated API key from a file stored on external storage.
 * File is located on external storage so it can be accessed from other
 * libraries./* w  w w .j  a va  2  s  . co  m*/
 * 
 * @return null if no key otherwise a key read from file
 */
private static String getKeyFromFile(boolean testMode) {
    String keyFilePath = testMode ? API_KEY_FILE_TEST_DEFAULT : API_KEY_FILE_DEFAULT;

    // check if the key file exists
    File keyFile = new File(keyFilePath);
    if (keyFile.exists()) {
        try {
            FileInputStream fis = new FileInputStream(keyFile);
            int ch = -1;
            StringBuffer sb = new StringBuffer();
            while ((ch = fis.read()) >= 0) {
                sb.append((char) ch);
            }
            fis.close();

            // read the key from file
            return sb.toString();
        } catch (Exception e) {
            Log.e(ApiKeyHandler.class.getSimpleName(), "Error reading key from file", e);
        }
    }

    return null;
}

From source file:controllers.user.UserAvatarApp.java

@Deprecated
public static Result render(File file) {
    try {//from w  w w .j a  v a  2  s. c om
        FileInputStream in = new FileInputStream(file);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[2048];
        int len = -1;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        in.close();
        return ok(out.toByteArray());
    } catch (Exception e) {
        Logger.error("render user avatar error ", e);
    }
    return ok();
}

From source file:AmazonKinesisAuditVerify.java

/**
 * @param propertiesFile /*from   w  w w .  ja  v a2  s .  c  o  m*/
 * @throws IOException Thrown when we run into issues reading properties 
 */
private static void loadProperties(String propertiesFile) throws IOException {
    FileInputStream inputStream = new FileInputStream(propertiesFile);
    Properties properties = new Properties();
    try {
        properties.load(inputStream);
    } finally {
        inputStream.close();
    }

    String appNameOverride = properties.getProperty(ConfigKeys.APPLICATION_NAME_KEY);
    if (appNameOverride != null) {
        applicationName = appNameOverride;
    }
    LOG.info("Using application name " + applicationName);

    String streamNameOverride = properties.getProperty(ConfigKeys.STREAM_NAME_KEY);
    if (streamNameOverride != null) {
        streamName = streamNameOverride;
    }
    LOG.info("Using stream name " + streamName);

    String kinesisEndpointOverride = properties.getProperty(ConfigKeys.KINESIS_ENDPOINT_KEY);
    if (kinesisEndpointOverride != null) {
        kinesisEndpoint = kinesisEndpointOverride;
    }
    String initialPositionOverride = properties.getProperty(ConfigKeys.INITIAL_POSITION_IN_STREAM_KEY);
    if (initialPositionOverride != null) {
        initialPositionInStream = InitialPositionInStream.valueOf(initialPositionOverride);
    }
    LOG.info("Using initial position " + initialPositionInStream.toString()
            + " (if a checkpoint is not found).");
}

From source file:jo.alexa.sim.ui.logic.RuntimeLogic.java

private static void loadProps() {
    if (mProps != null)
        return;//from ww  w . j  a  v a  2s . c  om
    mProps = new Properties();
    File propFile = new File(System.getProperty("user.home"), ".echosim.properties");
    if (!propFile.exists())
        return;
    try {
        FileInputStream fis = new FileInputStream(propFile);
        mProps.load(fis);
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:cn.mrdear.pay.util.WebUtils.java

/**
 * ?/* ww  w.  ja v  a 2 s.  c  om*/
 * @param certPath ?
 * @param passwd  ??
 * @param uri ?
 * @param entity xml
 * @return 
 */
public static String post(String certPath, String passwd, String uri, InputStreamEntity entity)
        throws Exception {
    String result = null;
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileInputStream instream = new FileInputStream(new File(certPath));
    try {
        keyStore.load(instream, passwd.toCharArray());
    } finally {
        instream.close();
    }
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, passwd.toCharArray()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
            null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    try {
        HttpPost httpPost = new HttpPost(uri);
        entity.setContentEncoding("UTF-8");
        httpPost.setEntity(entity);
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
        result = consumeResponse(httpResponse);
    } finally {
        httpclient.close();
    }
    return result;
}