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:Compress.java

public static void gzipFile(String from, String to) throws IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
    in.close();/*from   w w  w. j a  va2s  .  c o  m*/
    out.close();
}

From source file:Main.java

public static boolean isEncode(File f) {
    try {/*from  w  w w  .j a va 2  s .c  o  m*/
        FileInputStream input = new FileInputStream(f);
        //            input = new BufferedInputStream(input);
        byte[] ecodeBytes = new byte[ecodeTag.getBytes().length];
        input.read(ecodeBytes);
        input.close();
        return new String(ecodeBytes).equals(ecodeTag);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:msec.org.GzipUtil.java

static public void zip(String srcFile) throws Exception {
    GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(srcFile + ".gz"));
    FileInputStream in = new FileInputStream(srcFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }//from   w  w w  .  j a v a  2  s.  c  o  m
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:msec.org.GzipUtil.java

static public void zip(String srcFile, String destFile) throws Exception {
    GzipCompressorOutputStream out = new GzipCompressorOutputStream(new FileOutputStream(destFile));
    FileInputStream in = new FileInputStream(srcFile);
    byte[] buf = new byte[10240];
    while (true) {
        int len = in.read(buf);
        if (len <= 0) {
            break;
        }/*from w ww  . j a  va2 s.  c o  m*/
        out.write(buf, 0, len);
    }
    out.flush();
    out.close();
    in.close();
}

From source file:Main.java

private static void writeToOutFile(File toFile, FileInputStream fosfrom) throws IOException {
    FileOutputStream fosto = null;
    fosto = new FileOutputStream(toFile);
    byte bt[] = new byte[1024];
    int c;/* ww  w .  j  a  va 2s . c  o  m*/
    while ((c = fosfrom.read(bt)) > 0) {
        fosto.write(bt, 0, c);
    }
    fosto.close();

}

From source file:Main.java

public static void compressFiles(File files[], File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    for (int i = 0; i < files.length; i++) {
        FileInputStream fileInputStream = new FileInputStream(files[i]);
        zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath()));

        int size;
        while ((size = fileInputStream.read(buffer)) > 0)
            zipOutputStream.write(buffer, 0, size);

        zipOutputStream.closeEntry();/*  w w w  . j  av a 2s  .  c om*/
        fileInputStream.close();
    }

    zipOutputStream.close();
}

From source file:org.apache.drill.exec.store.http.util.JsonConverter.java

@SuppressWarnings("resource")
public static String stringFromFile(String file) {
    try {//from w  w  w  . j  a v  a2 s .c  o m
        FileInputStream stream = new FileInputStream(file);
        int size = stream.available();
        byte[] bytes = new byte[size];
        stream.read(bytes);
        return new String(bytes, Charsets.UTF_8);
    } catch (IOException e) {
    }
    return "";
}

From source file:org.phpmaven.pear.library.impl.Helper.java

/**
 * Returns the binary file contents./*from  w  w w . j  av a 2 s . co m*/
 * @param uri URI of the resource.
 * @return the files content.
 * @throws IOException thrown on errors.
 */
public static byte[] getBinaryFileContents(String uri) throws IOException {
    // is it inside the local filesystem?
    if (uri.startsWith("file://")) {
        final File channelFile = new File(uri.substring(7));

        final byte[] result = new byte[(int) channelFile.length()];
        final FileInputStream fis = new FileInputStream(channelFile);
        fis.read(result);
        fis.close();
        return result;
    }

    // try http connection
    final HttpClient client = new DefaultHttpClient();
    final HttpGet httpget = new HttpGet(uri);
    final HttpResponse response = client.execute(httpget);
    if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new IOException("Invalid http status: " + response.getStatusLine().getStatusCode() + " / "
                + response.getStatusLine().getReasonPhrase());
    }
    final HttpEntity entity = response.getEntity();
    if (entity == null) {
        throw new IOException("Empty response.");
    }
    return EntityUtils.toByteArray(entity);
}

From source file:Main.java

public static byte[] getFileContent(String fileName) {
    FileInputStream fin = null;
    try {// w w w . j  a v  a  2  s. co  m
        fin = new FileInputStream(fileName);
        int length = fin.available();
        byte[] bytes = new byte[length];
        fin.read(bytes);
        return bytes;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fin != null) {
                fin.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Utils.java

/**
 * Zip a list of file into one zip file.
 * /*from ww  w.  jav a2 s  .  co  m*/
 * @param files
 *          files to zip
 * @param targetZipFile
 *          target zip file
 * @throws IOException
 *           IO error exception can be thrown when copying ...
 */
public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(targetZipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[128];
        for (int i = 0; i < files.length; i++) {
            File currentFile = files[i];
            if (!currentFile.isDirectory()) {
                ZipEntry entry = new ZipEntry(currentFile.getName());
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                fis.close();
            }
        }
        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found : " + e);
    }

}