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:at.tugraz.sss.serv.util.SSFileU.java

public static void readFileBytes(final OutputStream outStream, final FileInputStream fileIn,
        final Integer transmissionSize) throws SSErr {

    try {// w  w w  .j  a  v  a 2s. c  om
        final byte[] fileBytes = new byte[transmissionSize];
        int read;

        while ((read = fileIn.read(fileBytes)) != -1) {

            if (fileBytes.length == 0 || read <= 0) {

                outStream.write(new byte[0]);
                outStream.flush();
                break;
            }

            outStream.write(fileBytes, 0, read);
            outStream.flush();
        }
    } catch (Exception error) {
        SSServErrReg.regErrThrow(error);
    } finally {

        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }

        if (fileIn != null) {
            try {
                fileIn.close();
            } catch (IOException ex) {
                SSLogU.err(ex);
            }
        }
    }
}

From source file:com.haoqee.chatsdk.net.Utility.java

private static byte[] getFileByte(File file) {

    byte[] buffer = null;
    FileInputStream fin;
    try {//from   www .  j  a va 2s  .com
        fin = new FileInputStream(file.getPath());
        int length;
        try {
            length = fin.available();
            buffer = new byte[length];
            fin.read(buffer);
            fin.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return buffer;
}

From source file:MyClassLoader.java

public Class findClass(String name) {
    byte[] classData = null;
    try {//from  w  w  w  .  j av a 2  s.  co m
        FileInputStream f = new FileInputStream("C:\\" + name + ".class");
        int num = f.available();
        classData = new byte[num];
        f.read(classData);
    } catch (IOException e) {
        System.out.println(e);
    }
    Class x = defineClass(name, classData, 0, classData.length);
    return x;
}

From source file:com.wbtech.ums.UmsAgent.java

private static void uploadAllLog(Context context) {

    File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/mobclick_agent_cached_" + context.getPackageName());
    if (file1.exists()) {
        try {// www  . j a  v a  2 s  .c  om
            FileInputStream in = new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/mobclick_agent_cached_" + context.getPackageName());
            StringBuffer sb = new StringBuffer();

            int i = 0;
            byte[] s = new byte[1024 * 4];

            while ((i = in.read(s)) != -1) {

                sb.append(new String(s, 0, i));
            }
            if (CommonUtil.isNetworkAvailable(context)) {
                MyMessage message = NetworkUitlity.post(UmsConstants.preUrl + UmsConstants.uploadUrl, sb + "");
                if (message.isFlag()) {
                    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                            + "/mobclick_agent_cached_" + context.getPackageName());
                    file.delete();
                } else {
                    CommonUtil.printLog("uploadError", "uploadLog Error");
                }
            } else {
                CommonUtil.printLog("NetworkError", "Network, not work");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java

/**
 * ???//from  w  ww.j  a  va2  s  .  com
 * 
 * @param oldFile
 *            
 * @param newFile
 *            
 * @return
 * @throws IOException
 */
public static boolean copyFile(File oldFile, File newFile) {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    try {
        fileInputStream = new FileInputStream(oldFile);
        fileOutputStream = new FileOutputStream(newFile);
        byte[] buf = new byte[1024];
        int len = 0;
        // ??
        while ((len = fileInputStream.read(buf)) != -1) {
            fileOutputStream.write(buf, 0, len);
            fileOutputStream.flush();
        }
        return true;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
        return false;
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}

From source file:com.chargebee.Application.MappingHeaders.java

private JSONObject readJsonData(String fileName2) throws Exception { //processes the Json array and returns in the required format. 
    File f = new File(fileName2);
    FileInputStream fr = new FileInputStream(f);
    byte[] b = new byte[fr.available()];
    fr.read(b);
    //JSONArray jarr = new JSONArray(new String(b));
    JSONObject jobj = new JSONObject(new String(b));
    return jobj; // The required Json object

}

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

/** {@inheritDoc} */
protected byte[] getEncodedCRL(String certCRLContent) {
    try {//from   w w w  . j av a 2 s  .c o m
        FileInputStream ins = new FileInputStream(certCRLContent);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return encoded;
    } catch (IOException e) {
        throw new FatalBeanException("Unable to read CRL(s) from file " + certCRLContent, e);
    }
}

From source file:com.krawler.formbuilder.servlet.FileDownloadServlet.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from  w  ww .j  a v a  2 s  .co m*/

        WebApplicationContext applicationContext = WebApplicationContextUtils
                .getWebApplicationContext(getServletContext());
        ModuleBuilderService moduleBuilderService = (ModuleBuilderService) applicationContext
                .getBean("moduleBuilderService");
        mb_docs docsObj = (mb_docs) moduleBuilderService.getMBDocById(mb_docs.class,
                request.getParameter("docid"));
        String src = PropsValues.STORE_PATH + request.getParameter("url");

        File fp = new File(src);
        byte[] buff = new byte[(int) fp.length()];
        FileInputStream fis = new FileInputStream(fp);
        int read = fis.read(buff);
        javax.activation.FileTypeMap mmap = new javax.activation.MimetypesFileTypeMap();
        String fileName = docsObj.getDocname();
        response.setContentType(mmap.getContentType(fp));
        response.setContentLength((int) fp.length());
        String contentDisposition = "";
        if (request.getParameter("attachment") != null) {
            contentDisposition = "attachment";
        } else {
            contentDisposition = "inline";
        }

        response.setHeader("Content-Disposition", contentDisposition + "; filename=\"" + fileName + "\";");
        response.getOutputStream().write(buff, 0, buff.length);
        response.getOutputStream().flush();
    } catch (Exception ex) {
        logger.warn("Unable To Download File :" + ex.toString(), ex);
    }

}

From source file:com.jsmartframework.web.manager.WebContext.java

/**
 * Write response as file stream when you want to provide download functionality.
 * Note that by using this method the response as HTML will not be generated and
 * the response will be what you have defined.
 *
 * @param file - File to be written on response.
 * @param bufferSize - Buffer size to write the response. Example 2048 bytes.
 * @throws IOException/*from  ww  w.  j a  v  a  2 s  .  c o  m*/
 */
public static void writeResponseAsFileStream(File file, int bufferSize) throws IOException {
    WebContext context = getCurrentInstance();
    if (context != null && file != null && bufferSize > 0) {
        context.responseWritten = true;
        context.response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
        context.response.addHeader("Content-Length", Long.toString(file.length()));
        context.response.setContentLength((int) file.length());

        String mimetype = getApplication().getMimeType(file.getName());
        context.response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");

        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = context.response.getOutputStream();

        try {
            int i;
            byte[] buffer = new byte[bufferSize];
            while ((i = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, i);
            }
        } finally {
            outputStream.flush();
            fileInputStream.close();
        }
    }
}

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

/** {@inheritDoc} */
protected byte[] getEncodedCertificate(String certConfigContent) {
    try {/*from w w w . j  a  va 2  s.  c o m*/
        FileInputStream ins = new FileInputStream(certConfigContent);
        byte[] encoded = new byte[ins.available()];
        ins.read(encoded);
        return encoded;
    } catch (IOException e) {
        throw new FatalBeanException("Unable to read certificate(s) from file " + certConfigContent, e);
    }
}