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:eu.scape_project.bitwiser.utils.SSDeep.java

static boolean ssUpdate(ss_context ctx, File handle) throws IOException {
    int bytesRead = 0;
    byte[] buffer;

    if (null == ctx || null == handle) {
        return true;
    }//  ww w.  j a  va2  s .c o m

    buffer = new byte[BUFFER_SIZE];

    ctx.ret.blocksize = ctx.block_size;
    // ctx.p = ctx.ret + strlen(ctx.ret)  
    ctx.p = new char[SPAMSUM_LENGTH];

    //memset(ctx.p, 0, SPAMSUM_LENGTH+1)
    Arrays.fill(ctx.p, (char) 0);
    //memset(ctx.ret2, 0, sizeof(ctx.ret2.length))
    Arrays.fill(ctx.ret2, (char) 0);

    ctx.k = ctx.j = 0;
    ctx.h3 = ctx.h2 = HASH_INIT;
    ctx.h = 0;
    rollReset();

    FileInputStream in = new FileInputStream(handle);
    // while ((bytes_read = fread(buffer,sizeof(byte),BUFFER_SIZE,handle)) > 0)
    while (in.available() > 0) {
        bytesRead = in.read(buffer);
        ssEngine(ctx, buffer, bytesRead);
    }
    in.close();

    if (ctx.h != 0) {
        ctx.p[ctx.j] = b64[(int) ((ctx.h2 & 0xFFFF) % 64)];
        ctx.ret2[ctx.k] = b64[(int) ((ctx.h3 & 0xFFFF) % 64)];
    }

    ctx.ret.hash = new String(ctx.p);
    ctx.ret.hash2 = new String(ctx.ret2);

    return false;
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

private static void zipDir(File sourceDir, final File destFile, ZipOutputStream zos) throws IOException {
    File[] dirList = sourceDir.listFiles(new FileFilter() {
        public boolean accept(File f) {
            return !f.getName().endsWith(destFile.getName());
        }//w  w w  . j a v  a  2s.  c o m
    });
    for (int i = 0; i < dirList.length; i++) {
        File f = dirList[i];
        if (f.isDirectory()) {
            zipDir(f, destFile, zos);
        } else {
            int bytesIn = 0;
            byte[] readBuffer = new byte[2156];
            FileInputStream fis = new FileInputStream(f);
            ZipEntry entry = new ZipEntry(sourceDir.getName() + File.separator + f.getName());
            zos.putNextEntry(entry);
            while ((bytesIn = fis.read(readBuffer)) != -1) {
                zos.write(readBuffer, 0, bytesIn);
            }
            fis.close();
        }
    }
}

From source file:morphy.utils.FileUtils.java

/**
 * This code was obtained from:/*from   w  w  w  .j  a v  a2  s  .  c  o m*/
 * http://www.dreamincode.net/code/snippet1443.htm
 * 
 * This function will copy files or directories from one location to
 * another. note that the source and the destination must be mutually
 * exclusive. This function can not be used to copy a directory to a sub
 * directory of itself. The function will also have problems if the
 * destination files already exist.
 * 
 * @param src
 *            -- A File object that represents the source for the copy
 * @param dest
 *            -- A File object that represents the destination for the copy.
 * @throws IOException
 *             if unable to copy.
 */
public static void copyFiles(File src, File dest) throws IOException {
    if (src.getName().startsWith(".")) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Ignoring " + src.getAbsolutePath() + " because name started with .");
        }
        return;
    }

    // Check to ensure that the source is valid...
    if (!src.exists()) {
        throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath() + ".");

    } else if (!src.canRead()) { // check to ensure we have rights to the
        // source...
        throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath() + ".");
    }

    // is this a directory copy?

    if (src.isDirectory()) {
        if (!dest.exists()) { // does the destination already exist?
            // if not we need to make it exist if possible (note this is
            // mkdirs not mkdir)

            if (!dest.mkdirs()) {
                throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Created directory " + dest.getAbsolutePath());
            }
        }
        // get a listing of files...

        String list[] = src.list();

        // copy all the files in the list.
        for (String element : list) {
            File dest1 = new File(dest, element);
            File src1 = new File(src, element);
            copyFiles(src1, dest1);
        }

    } else {
        // This was not a directory, so lets just copy the file

        FileInputStream fin = null;
        FileOutputStream fout = null;
        byte[] buffer = new byte[4096]; // Buffer 4K at a time (you can
        // change this).
        int bytesRead;

        try {

            // open the files for input and output

            fin = new FileInputStream(src);
            fout = new FileOutputStream(dest);

            // while bytesRead indicates a successful read, lets write...

            while ((bytesRead = fin.read(buffer)) >= 0) {

                fout.write(buffer, 0, bytesRead);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Copied " + src.getAbsolutePath() + " to " + dest.getAbsolutePath());
            }

        } catch (IOException e) { // Error copying file...

            IOException wrapper = new IOException("copyFiles: Unable to copy file: " +

                    src.getAbsolutePath() + "to" + dest.getAbsolutePath() + ".");

            wrapper.initCause(e);
            wrapper.setStackTrace(e.getStackTrace());
            throw wrapper;

        } finally { // Ensure that the files are closed (if they were open).

            if (fin != null) {
                try {
                    fin.close();
                } catch (Throwable t) {
                }
            }

            if (fout != null) {
                try {
                    fout.close();
                } catch (Throwable t) {
                }
            }
        }
    }
}

From source file:net.duckling.ddl.util.FileUtil.java

public static void copyFolder(String oldPath, String newPath) {

    try {//from  ww  w.  ja  v  a 2 s .  c  om
        (new File(newPath)).mkdirs();
        File a = new File(oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        LOG.error(e);
    }

}

From source file:cn.edu.mju.Thriphoto.net.HttpManager.java

private static void fileToUpload(OutputStream out, String filepath) throws WeiboException {
    if (filepath == null) {
        return;/*from  w w w .jav  a2 s. c o  m*/
    }
    StringBuilder temp = new StringBuilder();

    temp.append(MP_BOUNDARY).append("\r\n");

    temp.append("content-disposition: form-data; name=\"file\"; filename=\"").append(filepath).append("\"\r\n");
    temp.append("Content-Type: application/octet-stream; charset=utf-8\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    FileInputStream input = null;
    try {
        out.write(res);
        input = new FileInputStream(filepath);
        byte[] buffer = new byte[1024 * 50];
        while (true) {
            int count = input.read(buffer);
            if (count == -1) {
                break;
            }
            out.write(buffer, 0, count);
        }
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new WeiboException(e);
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                throw new WeiboException(e);
            }
        }
    }
}

From source file:cn.edu.mju.Thriphoto.net.HttpManager.java

private static void imageContentToUpload(OutputStream out, String imgpath) throws WeiboException {
    if (imgpath == null) {
        return;/*from   w  w  w. ja  v a  2 s  .c om*/
    }
    StringBuilder temp = new StringBuilder();

    temp.append(MP_BOUNDARY).append("\r\n");
    temp.append("Content-Disposition: form-data; name=\"pic\"; filename=\"").append("news_image")
            .append("\"\r\n");
    String filetype = "image/png";
    temp.append("Content-Type: ").append(filetype).append("\r\n\r\n");
    // temp.append("content-disposition: form-data; name=\"file\"; filename=\"")
    // .append(imgpath).append("\"\r\n");
    // temp.append("Content-Type: application/octet-stream; charset=utf-8\r\n\r\n");
    byte[] res = temp.toString().getBytes();
    FileInputStream input = null;
    try {
        out.write(res);
        input = new FileInputStream(imgpath);
        byte[] buffer = new byte[1024 * 50];
        while (true) {
            int count = input.read(buffer);
            if (count == -1) {
                break;
            }
            out.write(buffer, 0, count);
        }
        out.write("\r\n".getBytes());
        out.write(("\r\n" + END_MP_BOUNDARY).getBytes());
    } catch (IOException e) {
        throw new WeiboException(e);
    } finally {
        if (null != input) {
            try {
                input.close();
            } catch (IOException e) {
                throw new WeiboException(e);
            }
        }
    }
}

From source file:CSVTools.CsvToolsApi.java

private static void hashFile(String fileName) throws NoSuchAlgorithmException, IOException {

    MessageDigest md = MessageDigest.getInstance("SHA1");
    FileInputStream fis;
    fis = new FileInputStream(fileName);
    byte[] dataBytes = new byte[1024];

    int nread = 0;

    while ((nread = fis.read(dataBytes)) != -1) {
        md.update(dataBytes, 0, nread);/*from w ww. j  a  v a 2 s  .  c o  m*/
    }
    ;

    byte[] mdbytes = md.digest();

    // convert the byte to hex format
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < mdbytes.length; i++) {
        sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1));
    }

    System.out.println("Digest(in hex format):: " + sb.toString());

}

From source file:com.iksgmbh.sql.pojomemodb.utils.FileUtil.java

/**
 * Uses streams to perform copy/* w w  w  .  j  ava 2s .  co m*/
 * @param fromFile
 * @param toFile
 * @throws IOException
 */
public static void copyBinaryFile(final File fromFile, File toFile) {
    if (!fromFile.exists())
        throw new RuntimeException("FileCopy: " + "no such source file: " + fromFile.getAbsolutePath());
    if (!fromFile.isFile())
        throw new RuntimeException("FileCopy: " + "can't copy directory: " + fromFile.getAbsolutePath());
    if (!fromFile.canRead())
        throw new RuntimeException("FileCopy: " + "source file is unreadable: " + fromFile.getAbsolutePath());

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new RuntimeException(
                    "FileCopy: " + "destination file is unwriteable: " + toFile.getAbsolutePath());
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new RuntimeException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new RuntimeException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new RuntimeException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input file into a ZIP file container.
 *
 * @param aInFileName The file name to be compressed.
 * @param aZipFileName The file name of the ZIP container.
 * @throws IOException Related to opening the file streams and
 * related read/write operations.//from  ww w  . j a va  2 s. c  o m
 */
static public void zipFile(String aInFileName, String aZipFileName) throws IOException {
    File inFile;
    int byteCount;
    byte[] ioBuf;
    FileInputStream fileIn;
    ZipOutputStream zipOut;
    FileOutputStream fileOut;

    inFile = new File(aInFileName);
    if (inFile.isDirectory())
        return;

    ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    fileIn = new FileInputStream(inFile);
    fileOut = new FileOutputStream(aZipFileName);
    zipOut = new ZipOutputStream(fileOut);
    zipOut.putNextEntry(new ZipEntry(inFile.getName()));
    byteCount = fileIn.read(ioBuf);
    while (byteCount > 0) {
        zipOut.write(ioBuf, 0, byteCount);
        byteCount = fileIn.read(ioBuf);
    }
    fileIn.close();
    zipOut.closeEntry();
    zipOut.close();
}

From source file:com.github.os72.protocjar.maven.ProtocJarMojo.java

static File copyFile(File srcFile, File destFile) throws IOException {
    FileInputStream is = null;
    FileOutputStream os = null;/*  www .j av a 2  s .c  o  m*/
    try {
        is = new FileInputStream(srcFile);
        os = new FileOutputStream(destFile);
        int read = 0;
        byte[] buf = new byte[4096];
        while ((read = is.read(buf)) > 0)
            os.write(buf, 0, read);
    } finally {
        if (is != null)
            is.close();
        if (os != null)
            os.close();
    }
    return destFile;
}