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:web.diva.server.model.JFreeImgGenerator.java

public synchronized String saveToFile(JFreeChart chart, double width, double height, String path,
        ChartRenderingInfo chartRenderingInfo, String imgName) {
    try {/* w  w  w  .ja v a2 s.  c om*/
        File f = new File(path, imgName + ".png");
        if (!f.exists()) {
            f.createNewFile();
        }
        try {
            ChartUtilities.saveChartAsPNG(f, chart, (int) width, (int) height, chartRenderingInfo);
        } catch (IOException e) {
            System.err.println(e.getMessage());
        }
        FileInputStream imageInFile = new FileInputStream(f);
        byte imageData[] = new byte[(int) f.length()];
        imageInFile.read(imageData);
        String base64 = Base64.encodeBase64String(imageData);
        base64 = "data:image/png;base64," + base64;
        f.delete();
        return base64;
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
    return "";

}

From source file:cn.org.citycloud.srdz.utils.FileUtils.java

/**
 * ZIP?//from  w  ww.  j  a v  a 2s.co  m
 * @param dirPath 
 * @param file 
 * @param zouts ?
 */
public static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) {
    FileInputStream fin = null;
    ZipEntry entry = null;
    // ?
    byte[] buf = new byte[4096];
    int readByte = 0;
    if (file.isFile()) {
        try {
            // ?
            fin = new FileInputStream(file);
            // ZipEntry
            entry = new ZipEntry(getEntryName(dirPath, file));
            // ?
            zouts.putNextEntry(entry);
            // ?
            while ((readByte = fin.read(buf)) != -1) {
                zouts.write(buf, 0, readByte);
            }
            zouts.closeEntry();
            fin.close();
            System.out.println(" " + file.getAbsolutePath() + " zip!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:css.controller.AdmissionResultsController.java

private void downloadFile(String file, HttpServletResponse httpServletResponse) throws Exception {
    OutputStream out = httpServletResponse.getOutputStream();
    FileInputStream in = new FileInputStream(file);
    byte[] buffer = new byte[4096];
    int length;//from w ww .  ja  va2s .c o  m
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
}

From source file:edu.internet2.middleware.shibboleth.common.config.security.FilesystemBasicCredentialBeanDefinitionParser.java

/** {@inheritDoc} */
protected byte[] getEncodedPrivateKey(String keyConfigContent) {
    try {/*from   www  . j  a va2  s .co m*/
        FileInputStream ins = new FileInputStream(keyConfigContent);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return encoded;
    } catch (IOException e) {
        throw new FatalBeanException("Unable to read private key from file " + keyConfigContent, e);
    }
}

From source file:edu.internet2.middleware.shibboleth.common.config.security.FilesystemBasicCredentialBeanDefinitionParser.java

/** {@inheritDoc} */
protected byte[] getEncodedSecretKey(String keyConfigContent) {
    try {/*from   ww w .  jav a  2 s  .com*/
        FileInputStream ins = new FileInputStream(keyConfigContent);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return encoded;
    } catch (IOException e) {
        throw new FatalBeanException("Unable to read secret key from file " + keyConfigContent, e);
    }
}

From source file:edu.internet2.middleware.shibboleth.common.config.security.FilesystemBasicCredentialBeanDefinitionParser.java

/** {@inheritDoc} */
protected byte[] getEncodedPublicKey(String keyConfigContent) {
    try {// ww w.ja  v  a  2 s.  c o  m
        FileInputStream ins = new FileInputStream(keyConfigContent);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return encoded;
    } catch (IOException e) {
        throw new FatalBeanException("Unable to read public key from file " + keyConfigContent, e);
    }
}

From source file:net.opentsdb.tsd.GraphHandler.java

/**
 * Reads a file into a byte array./*w  w w  . j  a  v  a 2 s  .  c  o  m*/
 * @param query The query being handled (for logging purposes).
 * @param file The file to read.
 * @param max_length The maximum number of bytes to read from the file.
 * @return {@code null} if the file doesn't exist or is empty or couldn't be
 * read, otherwise a byte array of up to {@code max_length} bytes.
 */
private static byte[] readFile(final HttpQuery query, final File file, final int max_length) {
    final int length = (int) file.length();
    if (length <= 0) {
        return null;
    }
    FileInputStream in;
    try {
        in = new FileInputStream(file.getPath());
    } catch (FileNotFoundException e) {
        return null;
    }
    try {
        final byte[] buf = new byte[Math.min(length, max_length)];
        final int read = in.read(buf);
        if (read != buf.length) {
            logError(query, "When reading " + file + ": read only " + read + " bytes instead of " + buf.length);
            return null;
        }
        return buf;
    } catch (IOException e) {
        logError(query, "Error while reading " + file, e);
        return null;
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            logError(query, "Error while closing " + file, e);
        }
    }
}