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:de.suse.swamp.util.FileUtils.java

/**
 * Return the text content of a file//w w  w  . j  a v  a 2 s. c o m
 */
public static String getText(File file) throws Exception {
    if (file != null && file.exists() && file.canRead()) {
        byte[] b = new byte[(int) file.length()];
        log.debug("Getting text from file: " + file);
        FileInputStream filestream = null;
        filestream = new FileInputStream(file);
        filestream.read(b);
        filestream.close();
        return new String(b);
    } else {
        throw new Exception("Could not read from file: " + file.getAbsolutePath());
    }
}

From source file:Main.java

static byte[] read(String fname) throws Exception {
    long offset = 0;
    File f = new File(fname);
    long length = f.length();
    byte[] image = new byte[(int) length];
    FileInputStream fis = new FileInputStream(f);
    while (offset < length) {
        offset += fis.read(image, (int) offset, (int) (length - offset));
    }//from   ww  w .jav  a  2 s. c  o  m
    fis.close();
    return image;
}

From source file:com.redsqirl.workflow.utils.FileStream.java

public static byte[] encryptFile(File in) throws Exception {
    int blockSize = 8;
    //Figure out how many bytes are padded
    int paddedCount = blockSize - ((int) in.length() % blockSize);

    //Figure out full size including padding
    int padded = (int) in.length() + paddedCount;

    byte[] decData = new byte[padded];
    FileInputStream inStream = new FileInputStream(in);
    inStream.read(decData);/*from  w  w w  .ja va 2 s  .c o m*/
    inStream.close();

    //Write out padding bytes as per PKCS5 algorithm
    for (int i = (int) in.length(); i < padded; ++i) {
        decData[i] = (byte) paddedCount;
    }

    return encrypt(decData);
}

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static String readFileContent(URI uri) throws IOException {
    File file = new File(uri);
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);//from  w  w w . j av  a2 s  .c  o  m
    is.close();
    return new String(b);
}

From source file:net.ontopia.utils.EncryptionUtils.java

/**
 * INTERNAL: Reads the file into memory, encrypting it in the
 * process, then writes the encrypted data back out to the file.
 *//* w w w.ja  v a  2  s  .c o  m*/
public static void encrypt(File file) throws IOException {
    FileInputStream in = new FileInputStream(file);
    ByteArrayOutputStream tmpout = new ByteArrayOutputStream();
    encrypt(in, tmpout);
    in.close();

    FileOutputStream out = new FileOutputStream(file);
    ByteArrayInputStream src = new ByteArrayInputStream(tmpout.toByteArray());
    IOUtils.copy(src, out);
    out.close();
}

From source file:be.vds.jtbdive.core.utils.FileUtilities.java

public static void replaceAllInFile(File file, String oldValue, String newValue) throws IOException {
    byte[] b = new byte[(int) file.length()];
    FileInputStream is = new FileInputStream(file);
    is.read(b);/* w  w w  .  jav a 2 s  . co  m*/
    is.close();
    String s = new String(b);
    s = s.replaceAll(oldValue, newValue);
    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();
}

From source file:com.codercowboy.photo.organizer.service.PhotoIndexer.java

public static Photo indexFile(File f) throws Exception {
    if (!f.exists()) {
        throw new IOException("Cannot index file, file does not exist: " + f.getAbsolutePath());
    }/*  w w w  . j a va2  s . c  o m*/
    if (!f.canRead()) {
        throw new IOException(
                "Cannot index file, file is not readable, permission denied:" + f.getAbsolutePath());
    }

    Photo p = new Photo();
    p.setName(f.getName());
    //TODO: this should be relative path to parent
    p.setFileRelativePath(f.getAbsolutePath());
    p.setFileSize(f.length());
    FileInputStream fis = new FileInputStream(f);
    try {
        p.setMd5Checksum(DigestUtils.md5Hex(fis));
    } finally {
        fis.close();
    }
    return p;
}

From source file:Main.java

private static String loadBoard(File file) {
    String output = null;/*ww  w.jav  a2 s  .  c  o  m*/
    try {
        FileInputStream is = new FileInputStream(file);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        output = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return output;
}

From source file:VoldemortStatusPage.java

private static Properties lookupProperties(String fileName) throws IOException, FileNotFoundException {
    Properties p = new Properties();
    File propertyFile = new File(fileName);
    FileInputStream is = null;
    is = new FileInputStream(propertyFile);
    p.load(is);//from  w w w .  j  a v  a  2 s . c  om
    is.close();
    return p;
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {
    File file = new File(path);
    response.setBufferSize((int) file.length());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    response.setContentType(contentType);
    response.setContentLength((int) file.length());

    byte[] bytes = new byte[(int) file.length()];
    FileInputStream fis = new FileInputStream(file);
    fis.read(bytes);/*w w w .  j  a  va 2s .  c o m*/
    fis.close();

    FileCopyUtils.copy(bytes, response.getOutputStream());
}