Example usage for java.io BufferedInputStream read

List of usage examples for java.io BufferedInputStream read

Introduction

In this page you can find the example usage for java.io BufferedInputStream 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:com.amazonaws.services.simpleworkflow.flow.examples.deployment.DeploymentInitiator.java

public static String loadFile(String fileName) throws IOException {
    int length = (int) new File(fileName).length();
    byte[] buffer = new byte[length];
    BufferedInputStream is = null;
    try {// w w w.ja v a2s .  c  o m
        is = new BufferedInputStream(new FileInputStream(fileName));
        is.read(buffer);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return new String(buffer);
}

From source file:Main.java

/** Reads the specified file into a string
 * @param filePath the path to the file that should be read
 * @return the content of the specified file as a string or an empty string if the file does not exist 
 * @throws IOException *///w w  w.  j  a v  a2 s  .  co  m
public static String readFileAsString(String filePath) throws IOException {
    File file = new File(filePath);
    if (!file.exists()) {
        return "";
    }

    byte[] buffer = new byte[(int) new File(filePath).length()];

    BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
    bufferedInputStream.read(buffer);
    bufferedInputStream.close();

    return new String(buffer);
}

From source file:Main.java

public static void doCompressFile(String inFileName) {
    try {//w w w  . j  a  v  a2  s.co  m
        File file = new File(inFileName);
        FileOutputStream fos = new FileOutputStream(file + ".gz");
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream in = new BufferedInputStream(fin);
        byte[] buffer = new byte[1024];
        int i;
        while ((i = in.read(buffer)) >= 0) {
            gzos.write(buffer, 0, i);
        }
        in.close();
        gzos.close();
    } catch (IOException e) {
        System.out.println("Exception is" + e);
    }
}

From source file:Main.java

public static void addEntryContent(ZipOutputStream zos, String entryFileName)
        throws IOException, FileNotFoundException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(entryFileName));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = bis.read(buffer)) != -1) {
        zos.write(buffer, 0, count);/*from  www .  j av a2 s.c  o  m*/
    }
    bis.close();
}

From source file:Util.java

public static byte[] readFile(File file) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
    int bytes = (int) file.length();
    byte[] buffer = new byte[bytes];

    int readBytes = bis.read(buffer);
    bis.close();//from  w  w  w  .  j a v a  2  s . c  om
    return buffer;
}

From source file:Main.java

/**
 * extracts a zip file to the given dir/*from  w  w w.j av  a2s.co m*/
 * @param archive
 * @param destDir
 * @throws IOException 
 * @throws ZipException 
 * @throws Exception
 */
public static void extractZipArchive(File archive, File destDir) throws ZipException, IOException {
    if (!destDir.exists()) {
        destDir.mkdir();
    }

    ZipFile zipFile = new ZipFile(archive);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    byte[] buffer = new byte[16384];
    int len;
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        String entryFileName = entry.getName();

        File dir = buildDirectoryHierarchyFor(entryFileName, destDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        if (!entry.isDirectory()) {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(new File(destDir, entryFileName)));

            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));

            while ((len = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, len);
            }

            bos.flush();
            bos.close();
            bis.close();
        }
    }
}

From source file:Main.java

public static void saveISToFile(InputStream is, String fileName) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();/*w w w.  ja  va  2s . c om*/
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] barr = new byte[32768];
    int read = 0;
    while ((read = bis.read(barr)) > 0) {
        bos.write(barr, 0, read);
    }
    bis.close();
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:Main.java

private static Reader getUTF8Reader(InputStream f) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(f);
    assert bis.markSupported();
    bis.mark(3);/*from w  ww .jav  a2s. com*/
    boolean reset = true;
    byte[] t = new byte[3];
    bis.read(t);
    if (t[0] == ((byte) 0xef) && t[1] == ((byte) 0xbb) && t[2] == ((byte) 0xbf)) {
        reset = false;
    }
    if (reset) {
        bis.reset();
    }
    return new InputStreamReader(bis, "UTF-8");
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();/*ww  w. j  a v a2 s  .c  om*/
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    int len = (int) file.length();
    if (len == 0) {
        return new byte[0];
    }/* ww w.  j  a v  a  2  s . c o  m*/

    byte[] data = null;
    BufferedInputStream bis = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        bis = new BufferedInputStream(fis);
        data = new byte[len];
        bis.read(data);
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }
        }
    }

    return data;
}