Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static String getFileContent(File file) {
    FileInputStream f = null;
    try {/* w  w w  . ja v  a 2s. c om*/
        byte[] buffer = new byte[(int) file.length()];

        f = new FileInputStream(file);
        f.read(buffer);
        return new String(buffer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        closeAll(f);
    }
}

From source file:de.suse.swamp.util.FileUtils.java

/**
 * Return the text content of a file//w w w  .  ja va2 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: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);
    inStream.close();//from  w  ww . ja  v  a2  s  . co m

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

public static void compressFile(File file, File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    FileInputStream fileInputStream = new FileInputStream(file);
    zipOutputStream.putNextEntry(new ZipEntry(file.getPath()));

    int size;//from  ww w.  j  a  va 2s . c  o m
    while ((size = fileInputStream.read(buffer)) > 0)
        zipOutputStream.write(buffer, 0, size);

    zipOutputStream.closeEntry();
    fileInputStream.close();
    zipOutputStream.close();
}

From source file:com.nineteendrops.tracdrops.client.core.Utils.java

public static byte[] getBinaryData(String fullFileName, TracProperties tracProperties) {

    File file = new File(fullFileName);
    if (!file.exists()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }//ww w  .  java2 s. c o  m

    if (!file.canRead()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }

    if (!file.isFile()) {
        throw new TracException(MessageUtils.getFullFileNameNotFoundMessage(fullFileName));
    }

    int maxUploadSize = tracProperties.getIntegerProperty(TracProperties.ATTACHMENT_UPLOAD_MAX_SIZE);
    if (file.length() > maxUploadSize) {
        throw new TracException(MessageUtils.getMessage("core.file.size.exceeded", fullFileName,
                String.valueOf(maxUploadSize)));
    }

    byte[] binaryData;
    try {
        binaryData = new byte[(int) file.length()];
        FileInputStream fis = new FileInputStream(file);
        fis.read(binaryData);
        fis.close();
    } catch (IOException e) {
        throw new TracException(MessageUtils.registerErrorLog(log, "core.unexpected.exception", e.getMessage()),
                e);
    }
    return binaryData;
}

From source file:Main.java

private static String loadBoard(File file) {
    String output = null;// w ww. j a v  a2 s .  co  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: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);
    is.close();/*from   ww  w  .ja va 2s . c om*/
    return new String(b);
}

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);
    fis.close();//  ww w . j av a2s  .  c  o m

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

From source file:S3ClientSideEncryptionWithSymmetricMasterKey.java

public static SecretKey loadSymmetricAESKey(String path, String algorithm)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
    // Read private key from file.
    File keyFile = new File(path + "/" + keyName);
    FileInputStream keyfis = new FileInputStream(keyFile);
    byte[] encodedPrivateKey = new byte[(int) keyFile.length()];
    keyfis.read(encodedPrivateKey);
    keyfis.close();//from w  w w  .ja va2 s.c o  m

    // Generate secret key.
    return new SecretKeySpec(encodedPrivateKey, "AES");
}

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);
    is.close();//from   ww  w .  j av  a  2  s.  c  om
    String s = new String(b);
    s = s.replaceAll(oldValue, newValue);
    FileOutputStream os = new FileOutputStream(file);
    os.write(s.getBytes());
    os.flush();
    os.close();
}