Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

In this page you can find the example usage for java.io BufferedInputStream BufferedInputStream.

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:Main.java

/**
 * Copys a document file from one directory to another
 * @param documentPath/*  w  w  w . j a  va  2 s.c o m*/
 * @param destPath
 * @param filename
 * @param entityId src file Entity ID
 * @param newEntityId dest file Entity ID
 * @throws IOException
 */
public static void copyDocumentFile(File documentDir, File destDir, String filename, Object entityId,
        Object newEntityId) throws IOException {
    File src = new File(documentDir, filename);
    File dest = new File(destDir, filename);
    if (dest.exists())
        dest.delete();

    byte[] buffer = new byte[4096];
    int read = 0;
    InputStream in = null;
    OutputStream out = null;
    try {
        in = new BufferedInputStream(new FileInputStream(src));
        out = new BufferedOutputStream(new FileOutputStream(dest));
        while (true) {
            read = in.read(buffer);
            if (read == -1) {
                // -1 bedeutet EOF
                break;
            }
            out.write(buffer, 0, read);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        }
    }
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;/*  www.j av  a 2 s  . c  o  m*/
    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();

    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    key = keygen.generateKey();
    ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser"));
    keyFileout.writeObject(key);
    keyFileout.close();
    Cipher cipher = null;
    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));

    CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher);
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i != -1);
    in.close();
    out.close();
}

From source file:Main.java

public static void writeUrlToFileNIO(String urlToRead, String folderToWrite, String fileName)
        throws MalformedURLException, IOException {
    URL urlIn = new URL(urlToRead);
    File folderOut = Paths.get(folderToWrite).toFile();
    if (!(folderOut.exists() || folderOut.mkdirs())) {
        throw new RuntimeException("could not create folder " + folderToWrite);
    }//ww w .jav  a2s.c  om
    Path pathOut = Paths.get(folderToWrite, fileName);
    try (ReadableByteChannel in = Channels.newChannel(new BufferedInputStream(urlIn.openStream()));
            WritableByteChannel out = Files.newByteChannel(pathOut, CREATE, WRITE);) {
        transfer(in, out);
    }
}

From source file:MainClass.java

static private String computeDigest(MessageDigest algorithm, String filename) {
    String returnValue = "";
    try {/*from   w  ww  .j a  v  a  2 s  .c  o  m*/
        algorithm.reset();
        FileInputStream fis = new FileInputStream(filename);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DigestInputStream dis = new DigestInputStream(bis, algorithm);
        int ch;
        while ((ch = dis.read()) != -1)
            ;
        StringBuffer hexString = new StringBuffer();
        byte digest[] = algorithm.digest();
        int digestLength = digest.length;
        for (int i = 0; i < digestLength; i++) {
            hexString.append(hexDigit(digest[i]));
            hexString.append(" ");
        }
        returnValue = hexString.toString();
    } catch (IOException e) {
        System.err.println("Error generating digest for: " + filename);
    }
    return returnValue;
}

From source file:jfix.zk.Medias.java

public static InputStream asStream(Media media) {
    return new BufferedInputStream(
            media.inMemory() ? new ByteArrayInputStream(media.getByteData()) : media.getStreamData());
}

From source file:Main.java

/**
 * Extract the error of an HTTP response as a string.
 * @param connection the connection to read from.
 * @return the string extracted from the connection's error stream.
 * @throws Exception if any error occurs.
 *//*ww w  .jav  a  2 s.  com*/
static String readErrorBody(HttpURLConnection connection) throws Exception {
    return readString(new BufferedInputStream(connection.getErrorStream()));
}

From source file:Main.java

/**
 * Extract the body of an HTTP response as a string.
 * @param connection the connection to read from.
 * @return the string extracted from the connection's input stream.
 * @throws Exception if any error occurs.
 *///from   www .  j  ava  2s . com
static String readResponseBody(HttpURLConnection connection) throws Exception {
    return readString(new BufferedInputStream(connection.getInputStream()));
}

From source file:Main.java

public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
    ZipFile zfile = new ZipFile(zipFile);
    Enumeration<? extends ZipEntry> zList = zfile.entries();
    ZipEntry ze = null;/* w  w w  . j a v  a2s  .  com*/
    byte[] buf = new byte[1024];
    while (zList.hasMoreElements()) {
        ze = (ZipEntry) zList.nextElement();
        if (ze.isDirectory()) {
            continue;
        }
        Log.d(TAG, "ze.getName() = " + ze.getName());
        OutputStream os = new BufferedOutputStream(
                new FileOutputStream(getRealFileName(folderPath, ze.getName())));
        InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
        int readLen = 0;
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            os.write(buf, 0, readLen);
        }
        is.close();
        os.close();
    }
    zfile.close();
    return true;
}

From source file:Main.java

/**
 * unzip some gzip compressed data/*from  w w w  .ja  v a  2  s .c  om*/
 * @param bytes the data to uncompress
 * @return the uncompressed data
 */
public static String gzipDecompress(byte[] bytes) {
    ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream gs = new GZIPInputStream(stream);
        BufferedInputStream bufis = new BufferedInputStream(gs);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        int len;
        while ((len = bufis.read(buf)) > 0) {
            bos.write(buf, 0, len);
        }
        return bos.toString();
    } catch (IOException e) {
        return null;
    }
}

From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java

public static <T> T loadData(Context context, String filename, Class<T> obj) {
    StringBuilder strContent = new StringBuilder("");
    try {//from ww w . ja v a 2s .c o  m
        BufferedInputStream in = new BufferedInputStream(context.openFileInput(filename));

        int ch;
        while ((ch = in.read()) != -1)
            strContent.append((char) ch);

        in.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (strContent.length() > 0) {
        try {
            Log.i("TAG", strContent.toString());
            JSONObject json = new JSONObject(strContent.toString());
            return Json.object_from_json(json, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return null;
}