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:net.dries007.coremod.Coremod.java

public static String getChecksum(File file) {
    try {//from   w  w  w  . j av a  2 s  . c  o m
        final MessageDigest md = MessageDigest.getInstance("SHA1");
        final FileInputStream fis = new FileInputStream(file);
        final byte[] dataBytes = new byte[1024];

        int nread = 0;

        while ((nread = fis.read(dataBytes)) != -1) {
            md.update(dataBytes, 0, nread);
        }

        final byte[] mdbytes = md.digest();

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

        return sb.toString();
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException {
    // if we reached here, the File object f was not a directory 
    // create a FileInputStream on top of f

    FileInputStream fis = new FileInputStream(zipfile);
    try {//from   w w  w  .j  a  va 2  s . c om
        // create a new zip entry 
        ZipEntry anEntry = new ZipEntry(name);
        if (DEBUG)
            System.out.println("Add file : " + name);
        // place the zip entry in the ZipOutputStream object
        zos.putNextEntry(anEntry);
        // now write the content of the file to the
        // ZipOutputStream
        byte[] readBuffer = new byte[BUFFER_SIZE];
        for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis.read(readBuffer)) {
            zos.write(readBuffer, 0, bytesIn);
        }
    } finally {
        // close the Stream
        fis.close();
    }
}

From source file:mtmo.test.mediadrm.Utils.java

public static byte[] readIPMPDataFromFile(boolean isVideo) {
    ByteArrayBuffer buffer = new ByteArrayBuffer(0);
    FileInputStream fis = null;
    byte[] buff = new byte[1024];
    int ret = 0;/* www  .j av  a2  s.  c om*/
    File file = null;
    String path;

    if (isVideo) {
        path = VIDEO_SINF;
    } else {
        path = AUDIO_SINF;
    }

    file = new File(path);
    if (!file.exists()) {
        return null;
    }

    try {
        fis = new FileInputStream(file);
        while ((ret = fis.read(buff)) > 0) {
            buffer.append(buff, 0, ret);
        }
    } catch (IOException e) {
        return null;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
            fis = null;
        }
    }
    return buffer.buffer();
}

From source file:mtmo.test.mediadrm.Utils.java

public static byte[] readPsshDataFromFile(boolean isVideo) {
    ByteArrayBuffer buffer = new ByteArrayBuffer(0);
    FileInputStream fis = null;
    byte[] buff = new byte[1024];
    int ret = 0;//from  w w  w  .ja v  a2  s. com
    File file = null;
    String path;

    if (isVideo) {
        path = VIDEO_PSSH;
    } else {
        path = AUDIO_PSSH;
    }

    file = new File(path);
    if (!file.exists()) {
        return null;
    }

    try {
        fis = new FileInputStream(file);
        while ((ret = fis.read(buff)) > 0) {
            buffer.append(buff, 0, ret);
        }
    } catch (IOException e) {
        return null;
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
            fis = null;
        }
    }
    return buffer.buffer();
}

From source file:packjacket.StaticUtils.java

/**
 * GZips the main log file//w w w  . j  ava 2  s  .c  o  m
 * @return the gzipped file
 * @throws IOException if any I/O error occurs
 */
public static File gzipLog() throws IOException {
    //Write out buffer of log file
    RunnerClass.nfh.flush();
    //Initialize log and gzip-log files
    File log = new File(RunnerClass.homedir + "pj.log");
    GZIPOutputStream out = new GZIPOutputStream(
            new FileOutputStream(new File(log.getCanonicalPath() + ".pjl")));
    FileInputStream in = new FileInputStream(log);

    //How many bytes to copy with each incrmental copy of file.
    int bufferSize = 4 * 1024;

    //Buffer into which data is read from source file
    byte[] buffer = new byte[bufferSize];
    //How many bytes read so far
    int bytesRead;
    //Runs until no bytes left to read from source
    while ((bytesRead = in.read(buffer)) >= 0)
        out.write(buffer, 0, bytesRead);
    //Close streams
    out.close();
    in.close();

    return new File(log.getCanonicalPath() + ".pjl");
}

From source file:com.lightbox.android.bitmap.BitmapUtils.java

private static String getFileMD5(File file) {
    try {/*  ww  w . j  av  a 2 s .  com*/
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.reset();

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int count;
            while ((count = fileInputStream.read(buffer)) != -1) {
                md5.update(buffer, 0, count);
            }

            return md5ToString(md5.digest()).toString();
        } finally {
            IOUtils.closeQuietly(fileInputStream);
        }
    } catch (NoSuchAlgorithmException e) {
        Log.w(TAG, e);
    } catch (FileNotFoundException e) {
        Log.w(TAG, e);
    } catch (IOException e) {
        Log.w(TAG, e);
    }

    return "";
}

From source file:loadTest.loadTestLib.LUtil.java

public static boolean startDockerBuild() throws IOException, InterruptedException {
    if (useDocker) {
        if (runInDockerCluster) {

            HashMap<String, Integer> dockerNodes = getDockerNodes();
            String dockerTar = LUtil.class.getClassLoader().getResource("docker/loadTest.tar").toString()
                    .substring(5);//from w  w  w .ja va 2 s . co  m
            ExecutorService exec = Executors.newFixedThreadPool(dockerNodes.size());

            dockerError = false;
            doneDockers = 0;

            for (Entry<String, Integer> entry : dockerNodes.entrySet()) {
                exec.submit(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String url = entry.getKey() + "/images/vauvenal5/loadtest";
                            URL obj = new URL(url);
                            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                            con.setRequestMethod("DELETE");
                            con.setRequestProperty("force", "true");
                            int responseCode = con.getResponseCode();
                            con.disconnect();

                            url = entry.getKey()
                                    + "/build?t=vauvenal5/loadtest&dockerfile=./loadTest/Dockerfile";
                            obj = new URL(url);
                            con = (HttpURLConnection) obj.openConnection();
                            con.setRequestMethod("POST");
                            con.setRequestProperty("Content-type", "application/tar");
                            con.setDoOutput(true);
                            File file = new File(dockerTar);
                            FileInputStream fileStr = new FileInputStream(file);
                            byte[] b = new byte[(int) file.length()];
                            fileStr.read(b);
                            con.getOutputStream().write(b);
                            con.getOutputStream().flush();
                            con.getOutputStream().close();

                            responseCode = con.getResponseCode();

                            if (responseCode != 200) {
                                dockerError = true;
                            }

                            String msg = "";

                            do {
                                Thread.sleep(5000);
                                msg = "";
                                url = entry.getKey() + "/images/json";
                                obj = new URL(url);
                                HttpURLConnection con2 = (HttpURLConnection) obj.openConnection();
                                con2.setRequestMethod("GET");
                                responseCode = con2.getResponseCode();

                                BufferedReader in = new BufferedReader(
                                        new InputStreamReader(con2.getInputStream()));
                                String line = null;

                                while ((line = in.readLine()) != null) {
                                    msg += line;
                                }

                                con2.disconnect();
                            } while (!msg.contains("vauvenal5/loadtest"));

                            con.disconnect();
                            doneDockers++;
                        } catch (MalformedURLException ex) {
                            dockerError = true;
                        } catch (FileNotFoundException ex) {
                            dockerError = true;
                        } catch (IOException ex) {
                            dockerError = true;
                        } catch (InterruptedException ex) {
                            dockerError = true;
                        }
                    }
                });
            }

            while (doneDockers < dockerNodes.size()) {
                Thread.sleep(5000);
            }

            return !dockerError;
        }

        //get the path and substring the 'file:' from the URI
        String dockerfile = LUtil.class.getClassLoader().getResource("docker/loadTest").toString().substring(5);

        ProcessBuilder processBuilder = new ProcessBuilder("docker", "rmi", "-f", "vauvenal5/loadtest");
        processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        Process docker = processBuilder.start();

        docker.waitFor();

        processBuilder = new ProcessBuilder("docker", "build", "-t", "vauvenal5/loadtest", dockerfile);
        processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        Process proc = processBuilder.start();
        return proc.waitFor() == 0;
    }
    return true;
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

private static void zip(File file, String zipPath, ZipOutputStream zos) throws IOException {
    FileInputStream is = null;
    try {/* www.  ja  v a2  s .co  m*/
        byte[] buf = new byte[1024];
        // Add ZIP entry to output stream.
        zos.putNextEntry(new ZipEntry(zipPath));
        // Transfer bytes from the file to the ZIP file
        int len;
        is = new FileInputStream(file);
        while ((len = is.read(buf)) > 0) {
            zos.write(buf, 0, len);
        }
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.eryansky.common.utils.io.FileUtil.java

/**
 * ?//from   w w  w.  j  av a  2  s.c om
 * 
 * @param src
 *            ?
 * @param dst
 *            
 */
public static void copyFile(File src, File dst) {
    try {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(dst);
            in = new FileInputStream(src);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
            String dstpath = dst.getAbsolutePath();
            if (dstpath.lastIndexOf("/") != -1) {
                dstpath = dstpath.subSequence(0, dstpath.lastIndexOf("/")).toString();
            } else {
                dstpath = dstpath.subSequence(0, dstpath.lastIndexOf("\\")).toString();
            }
            createDirectory(dstpath);
            copyFile(src, dst);
        } finally {
            if (null != in) {
                in.close();
            }
            if (null != out) {
                out.close();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.mosso.client.cloudfiles.sample.FilesCopy.java

/**
 *
 * @param folder//from  w  ww .j a  va  2s  . c  om
 * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped.
 * @throws IOException
 */
public static File zipFolder(File folder) throws IOException {
    byte[] buf = new byte[1024];
    int len;

    // Create the ZIP file
    String filenameWithZipExt = folder.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));

    if (folder.isDirectory()) {
        File[] files = folder.listFiles();

        for (File f : files) {
            if (!f.isDirectory()) {
                FileInputStream in = new FileInputStream(f);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(f.getName()));

                // Transfer bytes from the file to the ZIP file
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                // Complete the entry
                out.closeEntry();
                in.close();
            } else
                logger.warn("Skipping nested folder: " + f.getAbsoluteFile());
        }

        out.flush();
        out.close();
    } else {
        logger.warn("The folder name supplied is not a folder!");
        System.err.println("The folder name supplied is not a folder!");
        return null;
    }

    return zippedFile;
}