Example usage for java.io FileInputStream available

List of usage examples for java.io FileInputStream available

Introduction

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

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Usage

From source file:com.jamesgiang.aussnowcam.Utils.java

public static String ReadSettings(Context context, String file) throws IOException {
    FileInputStream fIn = null;
    InputStreamReader isr = null;
    String data = null;//from  w  w  w  .  ja  v a 2s.c  o m
    fIn = context.openFileInput(file);
    isr = new InputStreamReader(fIn);
    char[] inputBuffer = new char[fIn.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fIn.close();
    return data;
}

From source file:com.owncloud.android.utils.PushUtils.java

public static Key readKeyFromFile(boolean readPublicKey) {
    String keyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder() + File.separator
            + KEYPAIR_FOLDER;/*ww w . java 2 s.c om*/

    String privateKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PRIV_EXTENSION;
    String publicKeyPath = keyPath + File.separator + KEYPAIR_FILE_NAME + KEYPAIR_PUB_EXTENSION;

    String path;

    if (readPublicKey) {
        path = publicKeyPath;
    } else {
        path = privateKeyPath;
    }

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(path);
        byte[] bytes = new byte[fileInputStream.available()];
        fileInputStream.read(bytes);
        fileInputStream.close();

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        if (readPublicKey) {
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
            return keyFactory.generatePublic(keySpec);
        } else {
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
            return keyFactory.generatePrivate(keySpec);
        }

    } catch (FileNotFoundException e) {
        Log_OC.d(TAG, "Failed to find path while reading the Key");
    } catch (IOException e) {
        Log_OC.d(TAG, "IOException while reading the key");
    } catch (InvalidKeySpecException e) {
        Log_OC.d(TAG, "InvalidKeySpecException while reading the key");
    } catch (NoSuchAlgorithmException e) {
        Log_OC.d(TAG, "RSA algorithm not supported");
    }

    return null;
}

From source file:org.mitre.opensextant.util.FileUtility.java

/**
 * Slurps a text file into a string and returns the string.
 * @param fileinput //from  w ww  .ja  va2s .  c  o m
 * @param enc
 * @return
 * @throws IOException  
 */
public static String readFile(File fileinput, String enc) throws IOException {
    if (fileinput == null) {
        return null;
    }

    FileInputStream instream = new FileInputStream(fileinput);
    byte[] inputBytes = new byte[instream.available()];
    instream.read(inputBytes);
    instream.close();
    return new String(inputBytes, enc);
}

From source file:org.mitre.opensextant.util.FileUtility.java

/** Given a file get the byte array
 * @param fileinput //from w ww . j  a  va2 s. co  m
 * @return 
 * @throws IOException 
 */
public static byte[] readBytesFrom(File fileinput) throws IOException {
    if (fileinput == null) {
        return null;
    }

    FileInputStream instream = new FileInputStream(fileinput);
    byte[] inputBytes = new byte[instream.available()];
    instream.read(inputBytes);
    instream.close();
    return inputBytes;
}

From source file:eu.scape_project.bitwiser.utils.SSDeep.java

static boolean ssUpdate(ss_context ctx, File handle) throws IOException {
    int bytesRead = 0;
    byte[] buffer;

    if (null == ctx || null == handle) {
        return true;
    }/*from ww w  . ja v  a  2 s .c  o m*/

    buffer = new byte[BUFFER_SIZE];

    ctx.ret.blocksize = ctx.block_size;
    // ctx.p = ctx.ret + strlen(ctx.ret)  
    ctx.p = new char[SPAMSUM_LENGTH];

    //memset(ctx.p, 0, SPAMSUM_LENGTH+1)
    Arrays.fill(ctx.p, (char) 0);
    //memset(ctx.ret2, 0, sizeof(ctx.ret2.length))
    Arrays.fill(ctx.ret2, (char) 0);

    ctx.k = ctx.j = 0;
    ctx.h3 = ctx.h2 = HASH_INIT;
    ctx.h = 0;
    rollReset();

    FileInputStream in = new FileInputStream(handle);
    // while ((bytes_read = fread(buffer,sizeof(byte),BUFFER_SIZE,handle)) > 0)
    while (in.available() > 0) {
        bytesRead = in.read(buffer);
        ssEngine(ctx, buffer, bytesRead);
    }
    in.close();

    if (ctx.h != 0) {
        ctx.p[ctx.j] = b64[(int) ((ctx.h2 & 0xFFFF) % 64)];
        ctx.ret2[ctx.k] = b64[(int) ((ctx.h3 & 0xFFFF) % 64)];
    }

    ctx.ret.hash = new String(ctx.p);
    ctx.ret.hash2 = new String(ctx.ret2);

    return false;
}

From source file:org.solmix.commons.util.Files.java

/**
 * ??//ww  w. j a v  a 2s . c om
 *
 * @param file
 * @return
 */
public static long getFileLength(File file) throws IOException {
    FileInputStream fis = null;
    fis = new FileInputStream(file);

    long size = fis.available();
    IOUtils.closeQuietly(fis);
    return size;
}

From source file:org.codesecure.dependencycheck.utils.SSDeep.java

static boolean ss_update(ss_context ctx, File handle) throws IOException {
    int bytes_read = 0;
    byte[] buffer;

    if (null == ctx || null == handle)
        return true;

    buffer = new byte[BUFFER_SIZE];
    if (buffer == null)
        return true;

    // snprintf(ctx.ret, 12, "%u:", ctx.block_size);
    ctx.ret = (ctx.block_size + ":").toCharArray();
    // ctx.p = ctx.ret + strlen(ctx.ret);  
    ctx.p = new char[SPAMSUM_LENGTH];

    //memset(ctx.p, 0, SPAMSUM_LENGTH+1);
    Arrays.fill(ctx.p, (char) 0);
    //memset(ctx.ret2, 0, sizeof(ctx.ret2.length));
    Arrays.fill(ctx.ret2, (char) 0);

    ctx.k = ctx.j = 0;//from  w  w w. j  a  va2s.  c  o  m
    ctx.h3 = ctx.h2 = HASH_INIT;
    ctx.h = 0;
    roll_reset();

    System.out.println("Opening file:" + handle);
    FileInputStream in = new FileInputStream(handle);
    // while ((bytes_read = fread(buffer,sizeof(byte),BUFFER_SIZE,handle)) > 0)
    while (in.available() > 0) {
        bytes_read = in.read(buffer);
        ss_engine(ctx, buffer, bytes_read);
    }

    if (ctx.h != 0) {
        ctx.p[ctx.j] = b64[(int) ((ctx.h2 & 0xFFFF) % 64)];
        ctx.ret2[ctx.k] = b64[(int) ((ctx.h3 & 0xFFFF) % 64)];
    }

    //  strcat(ctx.p+ctx.j, ":");
    //  strcat(ctx.p+ctx.j, ctx.ret2);
    ctx.ret = (new String(ctx.ret) + new String(ctx.p) + ":" + new String(ctx.ret2)).toCharArray();

    //  free(buffer);
    return false;
}

From source file:org.lockss.util.KeyStoreUtil.java

private static void readKeyStore(String domainNames[], KeyStore kss[], String passwords[], int i, File inDir)
        throws Exception {
    String domainName = domainNames[i];
    if (domainName == null) {
        return;/*w w  w . ja va2  s  .c o  m*/
    }
    File keyStoreFile = new File(inDir, domainName + ".jceks");
    File passwordFile = new File(inDir, domainName + ".pass");
    String password = null;
    try {
        if (!passwordFile.exists() || !passwordFile.isFile()) {
            log.debug("No password file " + passwordFile);
            return;
        }
        log.debug("Trying to read password from " + passwordFile);
        FileInputStream fis = new FileInputStream(passwordFile);
        byte[] buf = new byte[fis.available()];
        int l = fis.read(buf);
        if (l != buf.length) {
            log.debug("password read short " + l + " != " + buf.length);
            return;
        }
        password = new String(buf);
    } catch (IOException e) {
        log.debug("Read password threw " + e);
        throw e;
    }
    KeyStore ks = null;
    try {
        ks = KeyStore.getInstance(keyStoreType[0], keyStoreSPI[0]);
        log.debug("Trying to read KeyStore from " + keyStoreFile);
        FileInputStream fis = new FileInputStream(keyStoreFile);
        ks.load(fis, domainName.toCharArray());
    } catch (Exception e) {
        log.debug("ks.load(" + keyStoreFile + ") threw " + e);
        throw e;
    }
    String keyStorePassword = domainName;
    passwords[i] = password;
    kss[i] = ks;
    log.debug("KeyStore and password for " + domainName + " read");
}

From source file:org.xmlactions.common.io.ResourceUtils.java

/**
 * Copy a FileInputStream to a FileOutputStream.
 * /*ww w  .  j a v a 2  s  .  c o  m*/
 * @param fis
 *            is the FileInputStream to copy from.
 * @param fos
 *            is the FileOutputStream to copy to.
 * @throws exception
 *             if something goes wrong.
 */
public static void copyFileStreams(FileInputStream fis, FileOutputStream fos) throws Exception {

    try {
        int size = fis.available();
        for (int iLoop = 0; iLoop < size; iLoop += FILE_COPY_BLOCK_SIZE) {
            byte[] data = blockReadInputStream(fis, FILE_COPY_BLOCK_SIZE);
            fos.write(data);
        }
    } finally {
        fis.close();
        fos.close();
    }
}

From source file:Main.java

private static byte[] getBytesFromFile(File file) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
    try {/*from www.  j ava 2  s .  co  m*/
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int maxBufferSize = 1024 * 1024;
        int bufferSize = (int) Math.min(file.getTotalSpace(), maxBufferSize);
        byte[] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = 0;

        if (fileInputStream != null) {
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        while (bytesRead > 0) {
            dataOutputStream.write(buffer, 0, bufferSize);
            int bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}