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:Main.java

public static String read(String wbPath) {
    File file = new File(wbPath);
    if (file.exists()) {
        FileInputStream fis = null;
        try {/*from w  ww. j  a v a 2s.co  m*/
            fis = new FileInputStream(file);
            int len = fis.available();
            if (len > 0) {
                byte[] buf = new byte[len];
                fis.read(buf);
                String string = new String(buf, CHARSET);
                return string;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } finally {
            if (fis != null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    return null;
}

From source file:Main.java

public static String getFileSize(File file) {
    FileInputStream fis = null;
    try {/*from w  w w .  j a  va 2 s.c o m*/
        fis = new FileInputStream(file);
        int length = fis.available();
        if (length >= GB) {
            return String.format("%.2f GB", length * 1.0 / GB);
        } else if (length >= MB) {
            return String.format("%.2f MB", length * 1.0 / MB);
        } else {
            return String.format("%.2f KB", length * 1.0 / KB);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return "0";
}

From source file:com.ewcms.common.io.HtmlFileUtil.java

public static byte[] readByte(String fileName) {
    try {//from  w ww  .  j av a2 s  .  c  o m
        fileName = normalizePath(fileName);
        FileInputStream fis = new FileInputStream(fileName);
        byte r[] = new byte[fis.available()];
        fis.read(r);
        fis.close();
        return r;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:thv.th.reader.SpriteElementReader.java

public static Vector<SpriteElement> readAll(FileInputStream is) throws IOException {
    Vector<SpriteElement> res = new Vector<SpriteElement>();

    int index = 0;

    while (is.available() > 0) {
        res.add(readByIndex(is, index));
    }// w  w  w . j av a  2  s . c om

    return res;
}

From source file:org.archiviststoolkit.importer.ImportUtils.java

public static String loadFileIntoString(String filePath) throws IOException {
    FileInputStream fis = new FileInputStream(filePath);
    int x = fis.available();
    byte b[] = new byte[x];
    fis.read(b);//w w  w .  ja  v  a  2 s  .  c o m
    return new String(b);
}

From source file:Main.java

/**
 * Use the file and count the chars in the file, so we can use static arrays
 * //from   w  ww  .j a  v  a2 s .  c o  m
 * @param fileName
 * @return integer with the size of the file
 */
public static int countBytes(String fileName) {
    FileInputStream fin;
    int charCount = 0;
    try {
        // Open an input stream
        fin = new FileInputStream(fileName);
        while (fin.available() != 0) {
            fin.read();
            charCount++;
        }
        fin.close();
    }
    // Catches any error conditions
    catch (IOException e) {
        System.err.println("Unable to read image");
        System.exit(1);
    }
    return charCount;
}

From source file:Main.java

/**
 * Read the original picture to an array
 * /*from   ww  w  .  j a v  a2  s  .co  m*/
 * @param initialFile
 * @param size
 * @return an array with the address and values
 */
public static int[] readBaseFile(String initialFile, int size) {
    int[] imageTxt = new int[size];
    FileInputStream fin;
    try {
        // Open an input stream
        fin = new FileInputStream(initialFile);
        int k = 0;
        while (fin.available() != 0) {
            imageTxt[k++] = fin.read();
        }
        fin.close();
    }
    // Catches any error conditions
    catch (IOException e) {
        System.err.println("Unable to read image");
        System.exit(1);
    }
    return imageTxt;
}

From source file:info.papdt.blacklight.support.http.FeedbackUtility.java

private static String readLog() {
    String res = "";

    try {// w w w.  j  a v a  2 s .co m
        FileInputStream fin = new FileInputStream(CrashHandler.CRASH_LOG);

        int length = fin.available();

        byte[] buffer = new byte[length];

        fin.read(buffer);

        res = EncodingUtils.getString(buffer, "UTF-8");

        fin.close();

    } catch (Exception e) {

        e.printStackTrace();
        return "";

    }
    return res;
}

From source file:org.jbpm.designer.stencilset.Beautifier.java

protected static String getScriptFromFile(String filename) throws IOException {
    if (filename == null) {
        return null;
    }//  www  . j a  va  2 s  . c o m

    FileInputStream fis = null;
    byte[] b = null;

    try {
        // read the file contents into a byte array.
        fis = new FileInputStream(filename);
        int x = fis.available();
        b = new byte[x];
        fis.read(b);
    } finally {
        IOUtils.closeQuietly(fis);
    }

    // create the stencilset string
    return new String(b);
}

From source file:org.jasig.portlet.attachment.util.FileUtil.java

public static byte[] read(File file) throws IOException {
    if (file == null || !file.exists())
        return null;
    if (locks.contains(file.getAbsolutePath())) {
        try {/*from   ww w .j  a v  a2s . c o  m*/
            Thread.sleep(100);
        } catch (InterruptedException ie) {
        }
    }

    FileInputStream input = null;
    try {
        input = new FileInputStream(file);
        int available = input.available();
        FileChannel channel = input.getChannel();
        ByteBuffer bytes = ByteBuffer.allocate(available);
        channel.read(bytes);
        bytes.flip();
        return bytes.array();
    } finally {
        if (input != null) {
            input.close();
        }
    }

}